Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Can not test requests with query string (superagent) #130

Closed
serebrov opened this Issue · 3 comments

3 participants

@serebrov

Requests with query string like "http://myhost.com/path?query=string" can not be tested and nock rasies an error "No match for HTTP request GET /testapi.html".

Here is the testcase (mocha):

    it('should allow to test requests with query part', function(done) {
      var agent = require('superagent');
      var nock = require('nock');

      nock('http://localhost')
      .get('/testapi.html?query=string')
      .reply(200, {yes: 'it works !'});

      agent
      .get('http://localhost/testapi.html?query=string')
      .buffer()
      .end(function(res){
        if (res.text !== '{"yes":"it works !"}') {
          done(new Error('Text is: ' + res.text));
        } else {
          done();
        }
      });
    });

As I understand from documentation this should work. But instead nock does not take query string into account and raises an error:

Error: Nock: No match for HTTP request GET /testapi.html".

So it expects for "/testapi.html" instead of "/testapi.html?query=string".

For me it seems to be related to the way the superagent initializes the request.
It does something like this:

     //(1) create the request 
     var req = http.request({
        hostname: 'localhost',
        port: 80,
        path: '/testapi.html',
        method: 'GET'
      });

      //(2) configure reqest object, including query string
      req.path = req.path + '?query=string';

      //(3) set handlers
      req.on('response', function() {
        ....
      });
      //(4) do request
      req.end();

And mock searches for interceptor on step (1) when path does not contain the query string yet.

@pgte pgte closed this in 93d4c77
@pgte
Owner

Fix landed on v0.24.2.

@apptous-seb

Thanks, now it works for me.

By the way (regarding the comment for the fix) I don't think superagent is bad behaving - they do this to allow chainable calls like

   request.get('/shoes').query('size=10').query({ color: 'blue' }).end(function(response) {});
@pgte
Owner

Yeah, but it breaks node API doing so, hence the "bad behaving" :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.