Skip to content

Commit

Permalink
feat: support URL objects
Browse files Browse the repository at this point in the history
`url.URL` was added in Node 7 and functions like `http.get()`,
`http.request()` accept URL objects as well as strings as their first
argument. Add support for that to nock.
  • Loading branch information
jinwoo authored and gr2m committed May 30, 2018
1 parent 7582da0 commit de0cb79
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/intercept.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var RequestOverrider = require('./request_overrider'),
Interceptor = require('./interceptor'),
http = require('http'),
parse = require('url').parse,
URL = require('url').URL,
_ = require('lodash'),
debug = require('debug')('nock.intercept'),
timers = require('timers'),
Expand Down Expand Up @@ -357,6 +358,8 @@ function activate() {

if (typeof options === 'string') {
options = parse(options);
} else if (URL && options instanceof URL) {
options = parse(options.toString());
}
options.proto = proto;

Expand Down
25 changes: 25 additions & 0 deletions tests/test_intercept.js
Original file line number Diff line number Diff line change
Expand Up @@ -2600,6 +2600,31 @@ test('accept string as request target', function(t) {
});
});

if (url.URL) {
test('accept URL as request target', function(t) {
var dataCalled = false;
var scope = nock('http://www.example.com')
.get('/')
.reply(200, "Hello World!");

http.get(new url.URL('http://www.example.com'), function(res) {
t.equal(res.statusCode, 200);

res.on('data', function(data) {
dataCalled = true;
t.ok(data instanceof Buffer, "data should be buffer");
t.equal(data.toString(), "Hello World!", "response should match");
});

res.on('end', function() {
t.ok(dataCalled);
scope.done();
t.end();
});
});
});
}

test('request has path', function(t) {
var scope = nock('http://haspath.com')
.get('/the/path/to/infinity')
Expand Down

0 comments on commit de0cb79

Please sign in to comment.