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

serebrov opened this Issue Nov 25, 2013 · 3 comments

3 participants


Requests with query string like "" 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');

      .reply(200, {yes: 'it works !'});

        if (res.text !== '{"yes":"it works !"}') {
          done(new Error('Text is: ' + res.text));
        } else {

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

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

@pgte pgte closed this in 93d4c77 Dec 3, 2013
Node Nock member

Fix landed on v0.24.2.


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) {});
Node Nock member

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