Skip to content

Commit

Permalink
Use pretty JSON and no longer stringify body
Browse files Browse the repository at this point in the history
  • Loading branch information
ianwsperber committed Jan 19, 2017
1 parent 4e0f62d commit ca9f1cc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
8 changes: 2 additions & 6 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,6 @@ var restoreOverriddenRequests = function() {
function stringifyRequest(options, body) {
var method = options.method || 'GET';

if (body && typeof(body) !== 'string') {
body = body.toString();
}

var port = options.port;
if (! port) port = (options.proto == 'https' ? '443' : '80');

Expand All @@ -196,11 +192,11 @@ function stringifyRequest(options, body) {
headers: options.headers
};

if (method !== 'GET') {
if (body) {
log.body = body;
}

return JSON.stringify(log);
return JSON.stringify(log, null, 2);
}

function isContentEncoded(headers) {
Expand Down
28 changes: 19 additions & 9 deletions tests/test_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,23 +172,33 @@ tap.test('stringifyRequest', function (t) {
}
};
}
var body = '{"foo": "bar"}';
var body = {"foo": "bar"};
var postReqOptions = getMockOptions();

t.equal(common.stringifyRequest(postReqOptions, body),
'{"method":"POST",'
+ '"url":"http://www.example.com:81/path/1",'
+ '"headers":{"cookie":"fiz=baz"},'
+ '"body":"{\\"foo\\": \\"bar\\"}"}'
JSON.stringify({
"method":"POST",
"url":"http://www.example.com:81/path/1",
"headers":{
"cookie": "fiz=baz"
},
"body": {
"foo": "bar"
}
}, null, 2)
);

var getReqOptions = getMockOptions();
getReqOptions.method = "GET";

t.equal(common.stringifyRequest(getReqOptions, body),
'{"method":"GET",'
+ '"url":"http://www.example.com:81/path/1",'
+ '"headers":{"cookie":"fiz=baz"}}'
t.equal(common.stringifyRequest(getReqOptions, null),
JSON.stringify({
"method":"GET",
"url":"http://www.example.com:81/path/1",
"headers":{
"cookie": "fiz=baz"
}
}, null, 2)
);

t.end();
Expand Down
16 changes: 8 additions & 8 deletions tests/test_intercept.js
Original file line number Diff line number Diff line change
Expand Up @@ -1868,7 +1868,7 @@ test("emits error if https route is missing", function(t) {
// This listener is intentionally after the end call so make sure that
// listeners added after the end will catch the error
req.on('error', function (err) {
t.equal(err.message.trim(), 'Nock: No match for request {"method":"GET","url":"https://google.com/abcdef892932"}');
t.equal(err.message.trim(), 'Nock: No match for request ' + JSON.stringify({"method":"GET","url":"https://google.com/abcdef892932"}, null, 2));
t.end();
});
});
Expand All @@ -1893,7 +1893,7 @@ test("emits error if https route is missing", function(t) {
// This listener is intentionally after the end call so make sure that
// listeners added after the end will catch the error
req.on('error', function (err) {
t.equal(err.message.trim(), 'Nock: No match for request {"method":"GET","url":"https://google.com:123/dsadsads"}');
t.equal(err.message.trim(), 'Nock: No match for request ' + JSON.stringify({"method":"GET","url":"https://google.com:123/dsadsads"}, null, 2));
t.end();
});
});
Expand Down Expand Up @@ -4574,7 +4574,7 @@ test('you must setup an interceptor for each request', function(t) {
t.equal(body, 'First match', 'should match first request response body');

mikealRequest.get('http://www.example.com/hey', function(error, res, body) {
t.equal(error && error.toString(), 'Error: Nock: No match for request {"method":"GET","url":"http://www.example.com/hey","headers":{"host":"www.example.com"}}');
t.equal(error && error.toString(), 'Error: Nock: No match for request ' + JSON.stringify({"method":"GET","url":"http://www.example.com/hey","headers":{"host":"www.example.com"}}, null, 2));
scope.done();
t.end();
});
Expand Down Expand Up @@ -4905,7 +4905,7 @@ test('query() with a function, function return false the query treat as Un-match
.reply(200);

mikealRequest('http://google.com/?i=should&pass=?', function(err, res) {
t.equal(err.message.trim(), 'Nock: No match for request {"method":"GET","url":"http://google.com/?i=should&pass=?","headers":{"host":"google.com"}}');
t.equal(err.message.trim(), 'Nock: No match for request ' + JSON.stringify({"method":"GET","url":"http://google.com/?i=should&pass=?","headers":{"host":"google.com"}}, null, 2));
t.end();
})
});
Expand All @@ -4917,7 +4917,7 @@ test('query() will not match when a query string does not match name=value', fun
.reply(200);

mikealRequest('https://c.com/b?foo=baz', function(err, res) {
t.equal(err.message.trim(), 'Nock: No match for request {"method":"GET","url":"https://c.com/b?foo=baz","headers":{"host":"c.com"}}');
t.equal(err.message.trim(), 'Nock: No match for request ' + JSON.stringify({"method":"GET","url":"https://c.com/b?foo=baz","headers":{"host":"c.com"}}, null, 2));
t.end();
})
});
Expand All @@ -4929,7 +4929,7 @@ test('query() will not match when a query string is present that was not registe
.reply(200);

mikealRequest('https://b.com/c?foo=bar&baz=foz', function(err, res) {
t.equal(err.message.trim(), 'Nock: No match for request {"method":"GET","url":"https://b.com/c?foo=bar&baz=foz","headers":{"host":"b.com"}}');
t.equal(err.message.trim(), 'Nock: No match for request ' + JSON.stringify({"method":"GET","url":"https://b.com/c?foo=bar&baz=foz","headers":{"host":"b.com"}}, null, 2));
t.end();
})
});
Expand All @@ -4941,7 +4941,7 @@ test('query() will not match when a query string is malformed', function (t) {
.reply(200);

mikealRequest('https://a.com/d?foobar', function(err, res) {
t.equal(err.message.trim(), 'Nock: No match for request {"method":"GET","url":"https://a.com/d?foobar","headers":{"host":"a.com"}}');
t.equal(err.message.trim(), 'Nock: No match for request ' + JSON.stringify({"method":"GET","url":"https://a.com/d?foobar","headers":{"host":"a.com"}}, null, 2));
t.end();
})
});
Expand All @@ -4958,7 +4958,7 @@ test('query() will not match when a query string has fewer correct values than e
.reply(200);

mikealRequest('http://google.com/?num=1str=fou', function(err, res) {
t.equal(err.message.trim(), 'Nock: No match for request {"method":"GET","url":"http://google.com/?num=1str=fou","headers":{"host":"google.com"}}');
t.equal(err.message.trim(), 'Nock: No match for request ' + JSON.stringify({"method":"GET","url":"http://google.com/?num=1str=fou","headers":{"host":"google.com"}}, null, 2));
t.end();
})
});
Expand Down

0 comments on commit ca9f1cc

Please sign in to comment.