Comments (23)
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.
Fixed by #341
from nock.
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.
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.
Closing #82.
from nock.
Shouldn't this be in the library? Query strings are pretty common things.
from nock.
Agree with @jtremback.
from nock.
Something like the body matcher code would be awesome for query params as well.
from nock.
π to this idea / problem
from nock.
@pgte would you be open to something like
.get("/x/x")
.allowAnyQueryString()
.reply(..............)
If so I can do this
from nock.
π
from nock.
π
from nock.
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.
π
from nock.
π― Any plans on this?
from nock.
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.
I also think the first option fits nicer into nock
's api.
from nock.
@pgte I agree.
from nock.
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.
@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.
@pgte what is the news about this issue? Is this implemented?
from nock.
It's not implemented. Would appreciate some help here.
from nock.
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)
- [Node 18.12] Class extends value undefined is not a constructor or null HOT 2
- incompatible behaviour introduced with v13.3.2 HOT 17
- Interceptors not being removed in 13.3.2 HOT 2
- Replace npm debug with require('node:util').debuglog HOT 2
- Remove lodash HOT 1
- Application to become a nock maintainer. HOT 5
- Expand function fails on weird GET query params HOT 4
- reqheaders does not work when using an undefined requestBody HOT 3
- Add interceptor at the beginning of list
- Return 500 instead of 404 HOT 4
- Bun support for nock.disableNetConnect() HOT 1
- DNS resolve errors and not mocking a lot of requests HOT 7
- POST request with ReadStream request body does not return a response HOT 10
- Disabled discussions HOT 6
- Improve debug logging HOT 2
- Support Abort Signals HOT 1
- Add JSDoc HOT 8
- Using nock inside a stream pipeline with got causes it to hang forever since Node 20.10.0 HOT 2
- Debug logging broken in beta (14.0.0-beta.5) HOT 1
- Error when code cancels fetch response body stream (14.0.0-beta.6) HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nock.