Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding the ability to specify port 80 explicitly

  • Loading branch information...
commit 2c8c25924c892756a27156404ac1c374c25b18b5 1 parent 5b1cd45
Mac Angell authored
View
2  README.md
@@ -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')
...
View
18 lib/intercept.js
@@ -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] || [];
}
View
4 lib/request_overrider.js
@@ -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);
View
7 lib/scope.js
@@ -3,6 +3,7 @@ var path = require('path')
, globalIntercept = require('./intercept')
, mixin = require('./mixin')
, assert = require('assert')
+ , url = require('url');
var noop = function() {};
@@ -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)) {
View
15 tests/test_intercept.js
@@ -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();
+});
Please sign in to comment.
Something went wrong with that request. Please try again.