Skip to content

Commit

Permalink
Merge 24bb077 into ea64fe3
Browse files Browse the repository at this point in the history
  • Loading branch information
hugoduraes committed Oct 6, 2016
2 parents ea64fe3 + 24bb077 commit 97bfee0
Show file tree
Hide file tree
Showing 7 changed files with 27,960 additions and 27,658 deletions.
16 changes: 15 additions & 1 deletion lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,20 @@ var headersFieldsArrayToLowerCase = function (headers) {
}));
};

var headersArrayToObject = function (rawHeaders) {
if(!_.isArray(rawHeaders)) {
return rawHeaders;
}

var headers = {};

for (var i=0, len=rawHeaders.length; i<len; i=i+2) {
headers[rawHeaders[i]] = rawHeaders[i+1];
}

return headers;
};

/**
* Deletes the given `fieldName` property from `headers` object by performing
* case-insensitive search through keys.
Expand Down Expand Up @@ -326,10 +340,10 @@ exports.isContentEncoded = isContentEncoded;
exports.isJSONContent = isJSONContent;
exports.headersFieldNamesToLowerCase = headersFieldNamesToLowerCase;
exports.headersFieldsArrayToLowerCase = headersFieldsArrayToLowerCase;
exports.headersArrayToObject = headersArrayToObject;
exports.deleteHeadersField = deleteHeadersField;
exports.percentEncode = percentEncode;
exports.percentDecode = percentDecode;
exports.matchStringOrRegexp = matchStringOrRegexp;
exports.formatQueryValue = formatQueryValue;
exports.isStream = isStream;

25 changes: 14 additions & 11 deletions lib/interceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ Interceptor.prototype.replyWithError = function replyWithError(errorMessage) {
return this.scope;
};

Interceptor.prototype.reply = function reply(statusCode, body, headers) {
Interceptor.prototype.reply = function reply(statusCode, body, rawHeaders) {
if (arguments.length <= 2 && _.isFunction(statusCode)) {
body = statusCode;
statusCode = 200;
}

this.statusCode = statusCode;

// We use lower-case headers throughout Nock.
headers = common.headersFieldNamesToLowerCase(headers);

_.defaults(this.options, this.scope.scopeOptions);

// convert rawHeaders from Array to Object
var headers = common.headersArrayToObject(rawHeaders);

if (this.scope._defaultReplyHeaders) {
headers = headers || {};
headers = mixin(this.scope._defaultReplyHeaders, headers);
Expand All @@ -75,23 +75,26 @@ Interceptor.prototype.reply = function reply(statusCode, body, headers) {
}

if (headers !== undefined) {
this.headers = {};
this.rawHeaders = [];

// makes sure all keys in headers are in lower case
for (var key2 in headers) {
if (headers.hasOwnProperty(key2)) {
this.headers[key2.toLowerCase()] = headers[key2];
this.rawHeaders.push(key2);
this.rawHeaders.push(headers[key2]);
for (var key in headers) {
if (headers.hasOwnProperty(key)) {
this.rawHeaders.push(key);
this.rawHeaders.push(headers[key]);
}
}

// We use lower-case headers throughout Nock.
this.headers = common.headersFieldNamesToLowerCase(headers);

debug('reply.headers:', this.headers);
debug('reply.rawHeaders:', this.rawHeaders);
}

// If the content is not encoded we may need to transform the response body.
// Otherwise we leave it as it is.
if (!common.isContentEncoded(headers)) {
if (!common.isContentEncoded(this.headers)) {
if (body && typeof(body) !== 'string' &&
typeof(body) !== 'function' &&
!Buffer.isBuffer(body) &&
Expand Down
9 changes: 6 additions & 3 deletions lib/recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ function generateRequestAndResponseObject(req, bodyChunks, options, res, dataChu
body: getBodyFromChunks(bodyChunks),
status: res.statusCode,
response: getBodyFromChunks(dataChunks, res.headers),
headers: res.headers,
reqheaders: req._headers
rawHeaders: res.rawHeaders || res.headers,
reqheaders: req._headers
};

}
Expand Down Expand Up @@ -153,7 +153,10 @@ function generateRequestAndResponse(req, bodyChunks, options, res, dataChunks) {
ret.push(res.statusCode.toString());
ret.push(', ');
ret.push(JSON.stringify(responseBody));
if (res.headers) {
if (res.rawHeaders) {
ret.push(', ');
ret.push(inspect(res.rawHeaders));
} else if (res.headers) {
ret.push(', ');
ret.push(inspect(res.headers));
}
Expand Down
2 changes: 1 addition & 1 deletion lib/request_overrider.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
var key = response.rawHeaders[rawHeaderIndex];
var value = response.rawHeaders[rawHeaderIndex + 1];
if (typeof value === "function") {
response.rawHeaders[rawHeaderIndex + 1] = evaluatedHeaders[key];
response.rawHeaders[rawHeaderIndex + 1] = evaluatedHeaders[key.toLowerCase()];
}
}

Expand Down
6 changes: 3 additions & 3 deletions lib/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ function define(nockDefs) {
, npath = nockDef.path
, method = nockDef.method.toLowerCase() || "get"
, status = getStatusFromDefinition(nockDef)
, headers = nockDef.headers || {}
, rawHeaders = nockDef.rawHeaders || []
, reqheaders = nockDef.reqheaders || {}
, body = nockDef.body || ''
, options = nockDef.options || {};
Expand All @@ -318,7 +318,7 @@ function define(nockDefs) {
if (body==="*") {
nock = startScope(nscope, options).filteringRequestBody(function() {
return "*";
})[method](npath, "*").reply(status, response, headers);
})[method](npath, "*").reply(status, response, rawHeaders);
} else {
nock = startScope(nscope, options);
// If request headers were specified filter by them.
Expand All @@ -330,7 +330,7 @@ function define(nockDefs) {
if (nockDef.filteringRequestBody) {
nock.filteringRequestBody(nockDef.filteringRequestBody);
}
nock.intercept(npath, method, body).reply(status, response, headers);
nock.intercept(npath, method, body).reply(status, response, rawHeaders);
}

nocks.push(nock);
Expand Down
Loading

0 comments on commit 97bfee0

Please sign in to comment.