Giter VIP home page Giter VIP logo

Comments (23)

doublerebel avatar doublerebel commented on July 21, 2024 1

The way I was imagining, was an extension to the API. Similar to the above proposal for .allowAnyQueryString():

.query() // Allow any query string
.query(true) // Allow any query string
.query(false) // Disallow any query string
.query({key: "value"}) // Match query object

This matches up with the superagent syntax used in nock's test suite.

In the best case, we could parse the query string from the existing path, and treat that path as a shorthand for .query, enabling backwards compatibility. i.e.:

.get('/users/1?password=XXX')
.get('/users/1').query({password: "XXX"}) // same result

And also imagining filteringQuery() as in:

.filteringQuery(function(queryObj) {
    if (queryObj.password == ...);
});

EDIT: added .query() and .query(false)

from nock.

pgte avatar pgte commented on July 21, 2024 1

Fixed by #341

from nock.

pgte avatar pgte commented on July 21, 2024

You can use a path filtering regexp or function that will be called on every request by using .filteringPath():
https://github.com/flatiron/nock#path-filtering

You can then take that chance to reorder the query string if you will.

from nock.

em-cliqz avatar em-cliqz commented on July 21, 2024

That is what I ended up doing. I created a filtering function like below.

function normalize_get_path( str ) {
    var u = url.parse( str );
    var query = querystring.parse( u.query );

    var params = [];
    for( var param in query )
        params.push( param );
    params.sort();

    var oqs = '';
    for( var param in params ) {
        if( oqs.length )
            oqs += '&';

        oqs += encodeURIComponent( params[param] );
        oqs += "=";
        oqs += encodeURIComponent( query[params[param]] );
    }

    var c = url.format( { pathname: u.pathname, search: oqs } ); //NODE: 'query' is ignored, must use search
    return c;
}

from nock.

pgte avatar pgte commented on July 21, 2024

Closing #82.

from nock.

jtremback avatar jtremback commented on July 21, 2024

Shouldn't this be in the library? Query strings are pretty common things.

from nock.

wejendorp avatar wejendorp commented on July 21, 2024

Agree with @jtremback.

from nock.

wejendorp avatar wejendorp commented on July 21, 2024

Something like the body matcher code would be awesome for query params as well.

from nock.

samccone avatar samccone commented on July 21, 2024

πŸ‘ to this idea / problem

from nock.

samccone avatar samccone commented on July 21, 2024

@pgte would you be open to something like

.get("/x/x")
.allowAnyQueryString()
.reply(..............)

If so I can do this

from nock.

djensen47 avatar djensen47 commented on July 21, 2024

πŸ‘

from nock.

mtford90 avatar mtford90 commented on July 21, 2024

πŸ‘

from nock.

LoicMahieu avatar LoicMahieu commented on July 21, 2024

I'm mocking an external ugly API which has a lot of query string stuffs...
At this time we write that:

var data =Β { foo: 'bar' };
nock.get('/some/path' + querystring.stringify(data))

which is quite ugly but now, ordering is the problem.

As @wejendorp said, marcher like body would be great! πŸ‘

from nock.

doublerebel avatar doublerebel commented on July 21, 2024

πŸ‘

from nock.

floatdrop avatar floatdrop commented on July 21, 2024

πŸ’― Any plans on this?

from nock.

pgte avatar pgte commented on July 21, 2024

Nock is currently not query-string aware, but I agree this is annoying.

We can either
a) make a scope query-string aware by defau (making it match the query object instead of the string). Here an option would be needed to switch this off.
b) make it easy for the programmer to create a query string filter that they can activate with something like this:

scope.filteringPath(nock.orderQueryString);

I'm inclined to support the first option since it's more friendly and I suspect covers most of the cases.
@ierceg @svnlto thoughts?

from nock.

svnlto avatar svnlto commented on July 21, 2024

I also think the first option fits nicer into nock's api.

from nock.

ierceg avatar ierceg commented on July 21, 2024

@pgte I agree.

from nock.

kevinhodges avatar kevinhodges commented on July 21, 2024

Has anyone started a branch for this? I'm happy to give it a crack, from a first look at the codebase we'd want to extend the scope => intercept function with a .query(), but then it should probably use a lot of the .match() functionality, any thoughts/ideas on the best solution let me know.

In terms of the interface, the best solutions are not gonna be backwards compatible, something like

var google = nock('http://www.google.com')
.get( '/', {
  body: {
    foo: 'bar'
  },
  query: {
    foo: 'bar'
  }
}

feels quite nice.

from nock.

pgte avatar pgte commented on July 21, 2024

@kevinhodges I wouldn't want to break backwards compatibility.

One way I see this working is if, instead of a string with the path you provide an object like this:

var google = nock('http://www.google.com')
.get({
  path:  '/',
  query: {
    foo: 'bar',
    bar: 'foo'
  }
});

this should also work:

var google = nock('http://www.google.com')
.get({
  path:  '/',
  query: 'foo=bar&bar=foo'
});

Care to give it a go?

from nock.

jezeniel avatar jezeniel commented on July 21, 2024

@pgte what is the news about this issue? Is this implemented?

from nock.

pgte avatar pgte commented on July 21, 2024

It's not implemented. Would appreciate some help here.

from nock.

lock avatar lock commented on July 21, 2024

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue and add a reference to this one if it’s related. Thank you!

from nock.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.