unexpected-mitm
Advanced tools
Comparing version
@@ -12,3 +12,5 @@ var messy = require('messy'), | ||
var cb = this.args.pop(); | ||
this.errorMode = 'nested'; | ||
expect(cb, 'to be a function'); // We need a cb | ||
this.errorMode = 'default'; | ||
@@ -38,3 +40,5 @@ var mitm = require('mitm-papandreou')(), | ||
var nextRequestDescriptionIndex = 0; | ||
var nextRequestDescriptionIndex = 0, | ||
httpConversation = new messy.HttpConversation(), | ||
httpConversationSatisfySpec = {exchanges: []}; | ||
@@ -121,4 +125,16 @@ mitm.on('request', function (req, res) { | ||
var expectedRequestBody = expectedRequestProperties.body; | ||
if (Array.isArray(expectedRequestBody) || (expectedRequestBody && typeof expectedRequestBody === 'object' && (typeof Buffer === 'undefined' || !Buffer.isBuffer(expectedRequestBody)))) { | ||
expectedRequestProperties.headers = expectedRequestProperties.headers || {}; | ||
if (Object.keys(expectedRequestProperties.headers).every(function (key) { | ||
return key.toLowerCase !== 'content-type'; | ||
})) { | ||
expectedRequestProperties.headers['Content-Type'] = 'application/json'; | ||
} | ||
} | ||
var httpExchange = new messy.HttpExchange({request: actualRequest, response: mockResponse}), | ||
requestBodyChunks = []; | ||
httpConversation.exchanges.push(httpExchange); | ||
httpConversationSatisfySpec.exchanges.push({request: expectedRequestProperties}); | ||
req.on('data', function (chunk) { | ||
@@ -129,5 +145,11 @@ requestBodyChunks.push(chunk); | ||
function assertAndDeliverMockResponse() { | ||
if (Array.isArray(mockResponse.body) || (mockResponse.body && typeof mockResponse.body === 'object' && (typeof Buffer === 'undefined' || !Buffer.isBuffer(mockResponse.body)))) { | ||
if (!mockResponse.headers.has('Content-Type')) { | ||
mockResponse.headers.set('Content-Type', 'application/json'); | ||
} | ||
mockResponse.body = JSON.stringify(mockResponse.body); | ||
} | ||
if (expectedRequestProperties) { | ||
try { | ||
expect(httpExchange, 'to satisfy', { request: expectedRequestProperties }); | ||
expect(httpConversation, 'to satisfy', httpConversationSatisfySpec); | ||
} catch (e) { | ||
@@ -137,2 +159,3 @@ return handleError(e); | ||
} | ||
res.statusCode = responseProperties.statusCode; | ||
mockResponse.headers.getNames().forEach(function (headerName) { | ||
@@ -143,6 +166,4 @@ mockResponse.headers.getAll(headerName).forEach(function (value) { | ||
}); | ||
res.statusCode = responseProperties.statusCode; | ||
var mockResponseBody = mockResponse.body; | ||
if (typeof mockResponseBody !== 'undefined') { | ||
res.write(mockResponseBody); | ||
if (typeof mockResponse.body !== 'undefined') { | ||
res.write(mockResponse.body); | ||
} | ||
@@ -163,9 +184,14 @@ res.end(); | ||
this.args.push(function (err) { | ||
cleanUp(); | ||
var numberOfRemainingExchanges = requestDescriptions.length - nextRequestDescriptionIndex; | ||
if (!err && numberOfRemainingExchanges > 0) { | ||
return handleError(new Error('unexpected-mitm: The test ended with ' + numberOfRemainingExchanges + | ||
' unused mocked out exchange' + (numberOfRemainingExchanges !== 1 ? 's' : ''))); | ||
} else { | ||
return cb.apply(this, arguments); | ||
if (!callbackCalled) { | ||
cleanUp(); | ||
var numberOfRemainingExchanges = requestDescriptions.length - nextRequestDescriptionIndex; | ||
setImmediate(function () { | ||
if (!err && numberOfRemainingExchanges > 0) { | ||
return handleError(new Error('unexpected-mitm: The test ended with ' + numberOfRemainingExchanges + | ||
' unused mocked out exchange' + (numberOfRemainingExchanges !== 1 ? 's' : ''))); | ||
} else { | ||
callbackCalled = true; | ||
return cb.apply(this, arguments); | ||
} | ||
}); | ||
} | ||
@@ -172,0 +198,0 @@ }); |
{ | ||
"name": "unexpected-mitm", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Unexpected plugin for the mitm library", | ||
@@ -18,3 +18,4 @@ "main": "lib/unexpectedMitm.js", | ||
"unexpected": "5.8.1", | ||
"unexpected-http": "1.5.0" | ||
"unexpected-http": "1.7.0", | ||
"unexpected-messy": "2.10.0" | ||
}, | ||
@@ -25,6 +26,4 @@ "dependencies": { | ||
"mocha": "2.1.0", | ||
"underscore": "1.7.0", | ||
"unexpected-http": "1.6.0", | ||
"unexpected-messy": "2.9.0" | ||
"underscore": "1.7.0" | ||
} | ||
} |
@@ -165,2 +165,88 @@ /*global describe, it, __dirname*/ | ||
describe('with the expected request body given as an object (shorthand for JSON)', function () { | ||
it('should succeed the match', function (done) { | ||
expect({ | ||
url: 'POST http://www.google.com/', | ||
body: { foo: 123 } | ||
}, 'with http mocked out', { | ||
request: { | ||
url: 'POST /', | ||
body: { foo: 123 } | ||
}, | ||
response: 200 | ||
}, 'to yield response', 200, done); | ||
}); | ||
it('should fail with a diff', function (done) { | ||
expect({ | ||
url: 'POST http://www.google.com/', | ||
body: { foo: 123 } | ||
}, 'with http mocked out', { | ||
request: { | ||
url: 'POST /', | ||
body: { foo: 456 } | ||
}, | ||
response: 200 | ||
}, 'to yield response', 200, function (err) { | ||
expect(err, 'to be an', Error); | ||
expect(err.output.toString(), 'to equal', | ||
'expected\n' + | ||
'{\n' + | ||
" url: 'POST http://www.google.com/',\n" + | ||
' body: { foo: 123 }\n' + | ||
'}\n' + | ||
'with http mocked out\n' + | ||
'{\n' + | ||
' request: {\n' + | ||
" url: '/',\n" + | ||
' body: { foo: 456 },\n' + | ||
" method: 'POST',\n" + | ||
" headers: { 'Content-Type': 'application/json' }\n" + | ||
' },\n' + | ||
' response: 200\n' + | ||
'} to yield response 200\n' + | ||
'\n' + | ||
'POST / HTTP/1.1\n' + | ||
'Host: www.google.com\n' + | ||
'Content-Type: application/json\n' + | ||
'Connection: keep-alive\n' + | ||
'Transfer-Encoding: chunked\n' + | ||
'\n' + | ||
'{\n' + | ||
' foo: 123 // should equal 456\n' + | ||
'}\n' + | ||
'\n' + | ||
'HTTP/1.1 200 OK' | ||
); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should produce a JSON response if the response body is given as an object', function (done) { | ||
expect('http://www.google.com/', 'with http mocked out', { | ||
request: 'GET /', | ||
response: { body: { foo: 123 } } | ||
}, 'to yield response', { | ||
statusCode: 200, | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: {foo: 123} | ||
}, done); | ||
}); | ||
it('should produce a JSON response if the response body is given as an array', function (done) { | ||
expect('http://www.google.com/', 'with http mocked out', { | ||
request: 'GET /', | ||
response: { body: [ { foo: 123 } ] } | ||
}, 'to yield response', { | ||
statusCode: 200, | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: [ { foo: 123 } ] | ||
}, done); | ||
}); | ||
it('should produce an error if the request conditions are not satisfied', function (done) { | ||
@@ -167,0 +253,0 @@ expect('http://www.google.com/foo', 'with http mocked out', { |
85761
6.16%4
-33.33%457
30.57%7
16.67%- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed