Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'json_content_type' of github.com:spenceralger/nock into…

… spenceralger-json_content_type

Conflicts:
	lib/request_overrider.js
  • Loading branch information...
commit 3dae1edc37457d7471e6b12bb2e190a3c1a1ba8e 2 parents 03c659e + 879d029
Pedro Teixeira authored
Showing with 66 additions and 9 deletions.
  1. +7 −0 lib/request_overrider.js
  2. +14 −8 lib/scope.js
  3. +45 −1 tests/test_intercept.js
7 lib/request_overrider.js
View
@@ -199,6 +199,13 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
process.nextTick(function() {
responseBody.resume();
});
+ } else if (responseBody && !Buffer.isBuffer(responseBody)) {
+ if (typeof responseBody === 'string') {
+ responseBody = new Buffer(responseBody);
+ } else {
+ responseBody = JSON.stringify(responseBody);
+ response.headers['content-type'] = 'application/json';
+ }
}
response.setEncoding = function(newEncoding) {
22 lib/scope.js
View
@@ -60,14 +60,6 @@ function startScope(basePath, options) {
}
}
- if (typeof(body) !== 'string' && typeof(body) !== 'function' && !Buffer.isBuffer(body) && !isStream(body)) {
- try {
- body = JSON.stringify(body);
- } catch(err) {
- throw new Error('Error encoding response body into JSON');
- }
- }
-
if (scope._defaultReplyHeaders) {
headers || (headers = {});
headers = mixin(scope._defaultReplyHeaders, headers);
@@ -84,6 +76,20 @@ function startScope(basePath, options) {
}
}
+ if (typeof(body) !== 'string' && typeof(body) !== 'function' && !Buffer.isBuffer(body) && !isStream(body)) {
+ try {
+ body = JSON.stringify(body);
+ if (!this.headers) {
+ this.headers = {};
+ }
+ if (!this.headers['content-type']) {
+ this.headers['content-type'] = 'application/json';
+ }
+ } catch(err) {
+ throw new Error('Error encoding response body into JSON');
+ }
+ }
+
this.body = body;
add(key, this, scope);
46 tests/test_intercept.js
View
@@ -842,6 +842,7 @@ test("reply with JSON", function(t) {
res.setEncoding('utf8');
t.equal(res.statusCode, 200);
+ t.equal(res.headers['content-type'], 'application/json');
res.on('end', function() {
t.ok(dataCalled);
scope.done();
@@ -1523,6 +1524,49 @@ test("default reply headers work", function(t) {
}, done).end();
});
+test("JSON encoded replies set the content-type header", function(t) {
+ var scope = nock('http://localhost')
+ .get('/')
+ .reply(200, {
+ A: 'b'
+ });
+
+ function done(res) {
+ scope.done();
+ t.equal(res.statusCode, 200);
+ t.equal(res.headers['content-type'], 'application/json');
+ t.end();
+ }
+
+ http.request({
+ host: 'localhost'
+ , path: '/'
+ }, done).end();
+});
+
+
+test("JSON encoded replies does not overwrite existing content-type header", function(t) {
+ var scope = nock('http://localhost')
+ .get('/')
+ .reply(200, {
+ A: 'b'
+ }, {
+ 'Content-Type': 'unicorns'
+ });
+
+ function done(res) {
+ scope.done();
+ t.equal(res.statusCode, 200);
+ t.equal(res.headers['content-type'], 'unicorns');
+ t.end();
+ }
+
+ http.request({
+ host: 'localhost'
+ , path: '/'
+ }, done).end();
+});
+
test('clean all works', function(t) {
var scope = nock('http://amazon.com')
.get('/nonexistent')
@@ -2175,4 +2219,4 @@ test('delay works with when you return a generic stream from the reply callback'
t.end();
});
}).end('OK');
-});
+});
Please sign in to comment.
Something went wrong with that request. Please try again.