Skip to content

Commit

Permalink
fix: URLs Ending with Comma Cause "No match for HTTP request" Error (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Roldan authored and gr2m committed Dec 1, 2017
1 parent fa58493 commit a2d04c3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/request_overrider.js
Expand Up @@ -266,7 +266,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
}
}

var err = new Error("Nock: No match for request " + common.stringifyRequest(options, requestBody));
var err = new Error('Nock: No match for request ' + common.stringifyRequest(options, requestBody) + ' Got instead ' + common.stringifyRequest(options));
err.statusCode = err.status = 404;
emitError(err);
return;
Expand Down
40 changes: 32 additions & 8 deletions tests/test_intercept.js
Expand Up @@ -1874,7 +1874,10 @@ test("emits error if https route is missing", function(t) {
// This listener is intentionally after the end call so make sure that
// listeners added after the end will catch the error
req.on('error', function (err) {
t.equal(err.message.trim(), 'Nock: No match for request ' + JSON.stringify({"method":"GET","url":"https://google.com/abcdef892932"}, null, 2));
var headerOptions = JSON.stringify({"method":"GET","url":"https://google.com/abcdef892932"}, null, 2);
var expectedError = 'Nock: No match for request ' + headerOptions + ' Got instead ' + headerOptions

t.equal(err.message.trim(), expectedError);
t.end();
});
});
Expand All @@ -1897,7 +1900,10 @@ test("emits error if https route is missing", function(t) {
// This listener is intentionally after the end call so make sure that
// listeners added after the end will catch the error
req.on('error', function (err) {
t.equal(err.message.trim(), 'Nock: No match for request ' + JSON.stringify({"method":"GET","url":"https://google.com:123/dsadsads"}, null, 2));
var headerOptions = JSON.stringify({"method":"GET","url":"https://google.com:123/dsadsads"}, null, 2);
var expectedError = 'Nock: No match for request ' + headerOptions + ' Got instead ' + headerOptions

t.equal(err.message.trim(), expectedError);
t.end();
});
});
Expand Down Expand Up @@ -4656,7 +4662,10 @@ test('you must setup an interceptor for each request', function(t) {
t.equal(body, 'First match', 'should match first request response body');

mikealRequest.get('http://www.example.com/hey', function(error, res, body) {
t.equal(error && error.toString(), 'Error: Nock: No match for request ' + JSON.stringify({"method":"GET","url":"http://www.example.com/hey","headers":{"host":"www.example.com"}}, null, 2));
var headerOptions = JSON.stringify({"method":"GET","url":"http://www.example.com/hey","headers":{"host":"www.example.com"}}, null, 2);
var expectedError = 'Error: Nock: No match for request ' + headerOptions + ' Got instead ' + headerOptions

t.equal(error && error.toString(), expectedError);
scope.done();
t.end();
});
Expand Down Expand Up @@ -4997,7 +5006,10 @@ test('query() with a function, function return false the query treat as Un-match
.reply(200);

mikealRequest('http://google.com/?i=should&pass=?', function(err, res) {
t.equal(err.message.trim(), 'Nock: No match for request ' + JSON.stringify({"method":"GET","url":"http://google.com/?i=should&pass=?","headers":{"host":"google.com"}}, null, 2));
var headerOptions = JSON.stringify({"method":"GET","url":"http://google.com/?i=should&pass=?","headers":{"host":"google.com"}}, null, 2);
var expectedError = 'Nock: No match for request ' + headerOptions + ' Got instead ' + headerOptions

t.equal(err.message.trim(), expectedError);
t.end();
})
});
Expand All @@ -5009,7 +5021,10 @@ test('query() will not match when a query string does not match name=value', fun
.reply(200);

mikealRequest('https://c.com/b?foo=baz', function(err, res) {
t.equal(err.message.trim(), 'Nock: No match for request ' + JSON.stringify({"method":"GET","url":"https://c.com/b?foo=baz","headers":{"host":"c.com"}}, null, 2));
var headerOptions = JSON.stringify({"method":"GET","url":"https://c.com/b?foo=baz","headers":{"host":"c.com"}}, null, 2);
var expectedError = 'Nock: No match for request ' + headerOptions + ' Got instead ' + headerOptions

t.equal(err.message.trim(), expectedError);
t.end();
})
});
Expand All @@ -5021,7 +5036,10 @@ test('query() will not match when a query string is present that was not registe
.reply(200);

mikealRequest('https://b.com/c?foo=bar&baz=foz', function(err, res) {
t.equal(err.message.trim(), 'Nock: No match for request ' + JSON.stringify({"method":"GET","url":"https://b.com/c?foo=bar&baz=foz","headers":{"host":"b.com"}}, null, 2));
var headerOptions = JSON.stringify({"method":"GET","url":"https://b.com/c?foo=bar&baz=foz","headers":{"host":"b.com"}}, null, 2);
var expectedError = 'Nock: No match for request ' + headerOptions + ' Got instead ' + headerOptions

t.equal(err.message.trim(), expectedError);
t.end();
})
});
Expand All @@ -5033,7 +5051,10 @@ test('query() will not match when a query string is malformed', function (t) {
.reply(200);

mikealRequest('https://a.com/d?foobar', function(err, res) {
t.equal(err.message.trim(), 'Nock: No match for request ' + JSON.stringify({"method":"GET","url":"https://a.com/d?foobar","headers":{"host":"a.com"}}, null, 2));
var headerOptions = JSON.stringify({"method":"GET","url":"https://a.com/d?foobar","headers":{"host":"a.com"}}, null, 2);
var expectedError = 'Nock: No match for request ' + headerOptions + ' Got instead ' + headerOptions

t.equal(err.message.trim(), expectedError);
t.end();
})
});
Expand All @@ -5050,7 +5071,10 @@ test('query() will not match when a query string has fewer correct values than e
.reply(200);

mikealRequest('http://google.com/?num=1str=fou', function(err, res) {
t.equal(err.message.trim(), 'Nock: No match for request ' + JSON.stringify({"method":"GET","url":"http://google.com/?num=1str=fou","headers":{"host":"google.com"}}, null, 2));
var headerOptions = JSON.stringify({"method":"GET","url":"http://google.com/?num=1str=fou","headers":{"host":"google.com"}}, null, 2);
var expectedError = 'Nock: No match for request ' + headerOptions + ' Got instead ' + headerOptions

t.equal(err.message.trim(), expectedError);
t.end();
})
});
Expand Down

1 comment on commit a2d04c3

@jpsecher
Copy link

@jpsecher jpsecher commented on a2d04c3 Jan 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'Nock: No match for request ' + ... + ' Got instead ' + common.stringifyRequest(options)) produces confusing error messages, like

Error: Nock: No match for request {
  "method": "PUT",
  "url": "http://localhost:3210/v1/community/1/entity/5433",
  "body": "{\"modified_by\":124}"
} Got instead {
  "method": "PUT",
  "url": "http://localhost:3210/v1/community/1/entity/5433"
}

The problem is that the Got instead part seems to say that there indeed is a match (except for the body). But there is no such match.

Please sign in to comment.