Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for specifying port 80/443 in the scope #63

Merged
merged 2 commits into from
May 11, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ By default nock assumes HTTP. If you need to use HTTPS you can specify the `http

## Non-standard ports

If you use HTTPS and port 80 or HTTPS and port 443, don't specify the port in the scope. Otherwise, you should specify the port like this:
You are able to specify a non-standard port like this:

var scope = nock('http://my.server.com:8081')
...
Expand Down
18 changes: 5 additions & 13 deletions lib/intercept.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,11 @@ function interceptorsFor(options) {
var basePath;

options.proto = options.proto || 'http';

if (!options.host) {
options.host = options.hostname;
}

basePath = options.proto + '://' + options.host;
if (
options.port && options.host.indexOf(':') < 0 &&
(options.port !== 80 || options.proto !== 'http') &&
(options.port !== 443 || options.proto !== 'https')
) {
basePath += ":" + options.port;
}
options.port = options.port || ((options.proto === 'http') ? 80 : 443);
options.hostname = options.hostname || options.host.split(':')[0];
options.host = options.hostname + ':' + options.port;

basePath = options.proto + '://' + options.host;

return allInterceptors[basePath] || [];
}
Expand Down
4 changes: 2 additions & 2 deletions lib/request_overrider.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ function RequestOverrider(req, options, interceptors, remove) {
if (options.host && !getHeader(req, 'host')) {
var hostHeader = options.host;

if (options.host && +options.port !== 80) {
hostHeader += ':' + options.port;
if (options.port === 80 || options.port === 443) {
hostHeader = hostHeader.split(':')[0];
}

setHeader(req, 'Host', hostHeader);
Expand Down
7 changes: 6 additions & 1 deletion lib/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var path = require('path')
, globalIntercept = require('./intercept')
, mixin = require('./mixin')
, assert = require('assert')
, url = require('url');

var noop = function() {};

Expand All @@ -17,7 +18,11 @@ function startScope(basePath, options) {
transformRequestBodyFunction,
matchHeaders = [],
logger = noop,
scopeOptions = options || {};
scopeOptions = options || {},
urlParts = url.parse(basePath),
port = urlParts.port || ((urlParts.protocol === 'http:') ? 80 : 443);

basePath = urlParts.protocol + '//' + urlParts.hostname + ':' + port;

function add(key, interceptor) {
if (! interceptors.hasOwnProperty(key)) {
Expand Down
15 changes: 15 additions & 0 deletions tests/test_intercept.js
Original file line number Diff line number Diff line change
Expand Up @@ -1481,3 +1481,18 @@ tap.test('different ports work work with Mikeal request', function(t) {
t.end();
});
});

tap.test('explicitly specifiying port 80 works', function(t) {
var scope = nock('http://abc.portyyyy.com:80')
.get('/pathhh')
.reply(200, "Welcome, username");

http.request({
hostname: 'abc.portyyyy.com',
port: 80,
path: '/pathhh'
}, function(res) {
scope.done();
t.end();
}).end();
});