Skip to content

Commit

Permalink
Adding the ability to specify port 80 explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
Mac Angell committed May 7, 2012
1 parent 5b1cd45 commit 2c8c259
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
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
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
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) {
hostHeader = hostHeader.split(':')[0];
}

setHeader(req, 'Host', hostHeader);
Expand Down
7 changes: 6 additions & 1 deletion lib/scope.js
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
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();
});

0 comments on commit 2c8c259

Please sign in to comment.