Skip to content

Commit

Permalink
fix(#1203): handle statusCode without payload
Browse files Browse the repository at this point in the history
  • Loading branch information
StarpTech authored and gr2m committed Sep 9, 2018
1 parent d794d35 commit 21c255f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
36 changes: 20 additions & 16 deletions lib/request_overrider.js
Expand Up @@ -402,23 +402,27 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
if (responseBody) {
debug('transform the response body');

if (Array.isArray(responseBody) &&
responseBody.length >= 2 &&
responseBody.length <= 3 &&
typeof responseBody[0] == 'number')
{
if (Array.isArray(responseBody)) {
debug('response body is array: %j', responseBody);
response.statusCode = Number(responseBody[0]);
debug('new headers: %j', responseBody[2]);
if (! response.headers) response.headers = {};
_.assign(response.headers, responseBody[2] || {});
debug('response.headers after: %j', response.headers);
responseBody = responseBody[1];

response.rawHeaders = response.rawHeaders || [];
Object.keys(response.headers).forEach(function(key) {
response.rawHeaders.push(key, response.headers[key]);
});

if (!isNaN(Number(responseBody[0])))
{
response.statusCode = Number(responseBody[0]);
}

if (responseBody.length >= 2 && responseBody.length <= 3)
{
debug('new headers: %j', responseBody[2]);
if (!response.headers) response.headers = {};
_.assign(response.headers, responseBody[2] || {});
debug('response.headers after: %j', response.headers);
responseBody = responseBody[1];

response.rawHeaders = response.rawHeaders || [];
Object.keys(response.headers).forEach(function(key) {
response.rawHeaders.push(key, response.headers[key]);
});
}
}

if (interceptor.delayInMs) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -201,7 +201,7 @@
"test": "npm run -s unit",
"coverage": "nyc tap --harmony ./tests/test_*.js",
"coveralls": "cat ./coverage/lcov.info | coveralls",
"lint": "eslint '**/*.js'",
"lint": "eslint \"**/*.js\"",
"toc": "markdown-toc -i README.md && markdown-toc -i CONTRIBUTING.md ",
"semantic-release": "semantic-release"
},
Expand Down
27 changes: 27 additions & 0 deletions tests/test_intercept.js
Expand Up @@ -183,6 +183,33 @@ test("reply can take a callback", function(t) {
req.end();
});

test("reply should send correct statusCode with array-notation and without body", function(t) {
t.plan(1);

var statusCode = 202;

var scope = nock("http://www.google.com")
.get("/test-path/")
.reply(function(path, requestBody) {
return [statusCode]
});

var req = http.request({
host: "www.google.com",
path: "/test-path/",
port: 80
}, function(res) {

t.equal(res.statusCode, statusCode, "sends status code");
res.on('end', function() {
scope.done();
});

});

req.end();
});

test("reply takes a callback for status code", function(t) {
t.plan(3);

Expand Down

0 comments on commit 21c255f

Please sign in to comment.