unexpected-messy
Advanced tools
Comparing version 2.0.2 to 2.1.0
@@ -90,4 +90,119 @@ var messy = require('messy'), | ||
function isStringOrBufferThatCanBeInterpretedAsUtf8(obj) { | ||
return typeof obj === 'string' || (typeof Buffer === 'function' && Buffer.isBuffer(obj) && bufferCanBeInterpretedAsUtf8(obj)); | ||
} | ||
function isTextualMessage(message) { | ||
var contentType = message.headers.get('Content-Type'); | ||
if (contentType) { | ||
return isTextualContentType(contentType) && isStringOrBufferThatCanBeInterpretedAsUtf8(message.body); | ||
} else if (/^form-data(?:;|$)/.test(message.headers.get('Content-Disposition'))) { | ||
// Content-Disposition: form-data without Content-Type | ||
// Say OK if the value can be decoded as text | ||
return isStringOrBufferThatCanBeInterpretedAsUtf8(message.body); | ||
} | ||
return false; | ||
} | ||
function isNonBufferNonRegExpObject(obj) { | ||
return obj && typeof obj === 'object' && (typeof Buffer === 'undefined' || !Buffer.isBuffer(obj)) && !isRegExp(obj); | ||
} | ||
function canonicalizeObject(obj, stack) { | ||
stack = stack || []; | ||
if (stack.indexOf(obj) !== -1) return '[Circular]'; | ||
var canonicalizedObject; | ||
if ({}.toString.call(obj) === '[object Array]') { | ||
stack.push(obj); | ||
canonicalizedObject = obj.map(function (item) { | ||
return canonicalizeObject(item, stack); | ||
}); | ||
stack.pop(); | ||
} else if (typeof obj === 'object' && obj !== null) { | ||
stack.push(obj); | ||
canonicalizedObject = {}; | ||
Object.keys(obj).sort().forEach(function (key) { | ||
canonicalizedObject[key] = canonicalizeObject(obj[key], stack); | ||
}); | ||
stack.pop(); | ||
} else { | ||
canonicalizedObject = obj; | ||
} | ||
return canonicalizedObject; | ||
} | ||
function getUpgradedBody(message) { | ||
var body = message.body; | ||
if (typeof body !== 'undefined') { | ||
var contentType = message.headers.get('Content-Type'), | ||
isJson = /^application\/json\b/i.test(contentType); | ||
if (typeof Buffer === 'function' && Buffer.isBuffer(body) && isTextualMessage(message)) { | ||
body = body.toString('utf-8'); | ||
} | ||
if (isJson && typeof body === 'string') { | ||
try { | ||
body = JSON.parse(body); | ||
} catch (e) { | ||
// The body cannot be parsed as JSON, keep as a string instance | ||
} | ||
} | ||
} | ||
return body; | ||
} | ||
function upgradeOrDowngradeMessageBodyToMatchSpecBody(message, spec) { | ||
var messageBody = message.body, | ||
specBody = spec.body; | ||
if (typeof messageBody !== 'undefined') { | ||
var isJson = /^application\/json\b/i.test(message.headers.get('Content-Type')); | ||
if (isNonBufferNonRegExpObject(messageBody) && isJson) { | ||
if (typeof specBody === 'string' || (typeof Buffer === 'function' && Buffer.isBuffer(specBody))) { | ||
var parsedSpecBody; | ||
try { | ||
parsedSpecBody = JSON.parse(specBody); | ||
} catch (e) {} | ||
if (typeof parsedSpecBody !== 'undefined') { | ||
specBody = JSON.stringify(canonicalizeObject(parsedSpecBody), undefined, ' '); | ||
messageBody = JSON.stringify(messageBody, undefined, ' '); | ||
} | ||
} else if (isRegExp(specBody)) { | ||
messageBody = JSON.stringify(messageBody, undefined, ' '); | ||
} | ||
} | ||
if (typeof Buffer === 'function' && Buffer.isBuffer(messageBody) && ((typeof specBody === 'string' || isRegExp(specBody) || isNonBufferNonRegExpObject(specBody)) || isTextualMessage(message))) { | ||
try { | ||
messageBody = messageBody.toString('utf-8'); | ||
} catch (e) { | ||
// The body cannot be intepreted as utf-8, keep it as a Buffer instance | ||
} | ||
} | ||
if (isJson && typeof messageBody === 'string' && (typeof specBody === 'undefined' || typeof specBody === 'function' || isNonBufferNonRegExpObject(specBody))) { | ||
try { | ||
messageBody = JSON.parse(messageBody); | ||
} catch (e) { | ||
// The body cannot be parsed as JSON, keep as a string instance | ||
} | ||
} else if (typeof Buffer === 'function' && Buffer.isBuffer(specBody) && (!messageBody || typeof messageBody === 'string')) { | ||
messageBody = new Buffer(messageBody, 'utf-8'); | ||
} | ||
} else if (typeof Buffer === 'function' && Buffer.isBuffer(specBody) && specBody.length === 0) { | ||
messageBody = new Buffer([]); | ||
} else if (specBody === '') { | ||
messageBody = ''; | ||
} | ||
return { | ||
messageBody: messageBody, | ||
specBody: specBody | ||
}; | ||
} | ||
function expectMessageToSatisfy(expect, subject, value) { | ||
subject.upgradeOrDowngradeBodyToMatchSpec(value); | ||
var upgradedOrDowngradedMessageBodyAndSpecBody = upgradeOrDowngradeMessageBodyToMatchSpecBody(subject, value), | ||
subjectBody = upgradedOrDowngradedMessageBodyAndSpecBody.messageBody, | ||
expectedBody = upgradedOrDowngradedMessageBodyAndSpecBody.specBody; | ||
try { | ||
@@ -99,6 +214,9 @@ if (typeof value === 'string') { | ||
subject.body = subjectBody; // Not cool, but unexpected-express depends on it | ||
if ('body' in value) { | ||
expect(subject.body, 'to satisfy', value.body); | ||
} else if (this.flags.exhaustively) { | ||
expect(subject.hasEmptyBody(), 'to be true'); | ||
expect(subjectBody, 'to satisfy', expectedBody); | ||
} else { | ||
if (this.flags.exhaustively) { | ||
expect(subject.hasEmptyBody(), 'to be true'); | ||
} | ||
} | ||
@@ -119,3 +237,3 @@ } catch (e) { | ||
try { | ||
expect(subject.body, 'to satisfy', value.body); | ||
expect(subjectBody, 'to satisfy', expectedBody); | ||
} catch (e) { | ||
@@ -130,17 +248,17 @@ bodyDiffError = e; | ||
output.nl(2); | ||
if (typeof subject.body === 'string') { | ||
output.text(subject.body); | ||
if (typeof subjectBody === 'string') { | ||
output.text(subjectBody); | ||
} else { | ||
// Buffer instance or parsed JSON | ||
output.append(inspect(subject.body)); | ||
output.append(inspect(subjectBody)); | ||
} | ||
if (bodyDiffError) { | ||
output.sp().error('// ' + (bodyDiffError.label || 'should satisfy') + ' ').append(inspect(value.body)); | ||
output.sp().error('// ' + (bodyDiffError.label || 'should satisfy') + ' ').append(inspect(expectedBody)); | ||
} | ||
} else if (flags.exhaustively && !subject.hasEmptyBody()) { | ||
if (typeof subject.body === 'string') { | ||
output.text(subject.body); | ||
if (typeof subjectBody === 'string') { | ||
output.text(subjectBody); | ||
} else { | ||
// Buffer instance or parsed JSON | ||
output.append(inspect(subject.body)); | ||
output.append(inspect(subjectBody)); | ||
} | ||
@@ -174,2 +292,3 @@ output.sp().error('// should be removed'); | ||
name: 'messy.Headers', | ||
base: 'object', | ||
identify: function (obj) { | ||
@@ -353,2 +472,3 @@ return obj && obj.isMessyHeaders; | ||
name: 'messy.Message', | ||
base: 'object', | ||
identify: function (obj) { | ||
@@ -363,11 +483,11 @@ return obj && obj.isMessyMessage; | ||
if (!message.hasEmptyBody()) { | ||
message.upgradeBody(); | ||
var upgradedBody = getUpgradedBody(message); | ||
if (message.headers.getNames().length > 0) { | ||
output.nl(2); | ||
} | ||
if (typeof message.body === 'string') { | ||
output.text(message.body); | ||
if (typeof upgradedBody === 'string') { | ||
output.text(upgradedBody); | ||
} else { | ||
// Buffer instance or parsed JSON | ||
output.append(inspect(message.body, depth)); | ||
output.append(inspect(upgradedBody, depth)); | ||
} | ||
@@ -383,20 +503,8 @@ } | ||
if (!expected.hasEmptyBody()) { | ||
var isJson = /^application\/json\b|\+json/i.test(expected.headers.get('Content-Type')); | ||
if ((typeof Buffer === 'function' && Buffer.isBuffer(expected.body)) && isTextualContentType(expected.headers.get('Content-Type')) && bufferCanBeInterpretedAsUtf8(expected.body)) { | ||
// TODO: Avoid mutating? | ||
expected.body = expected.body.toString('utf-8'); | ||
} | ||
if (isJson && typeof expected.body === 'string') { | ||
try { | ||
expected.body = JSON.parse(expected.body); | ||
} catch (e) { | ||
// The body cannot be parsed as JSON, keep as a string instance | ||
} | ||
} | ||
expected.body = getUpgradedBody(expected); | ||
} | ||
expected.upgradeBody(); | ||
actual.upgradeOrDowngradeBodyToMatchSpec(expected); | ||
var bodyDiff = diff(actual.body, expected.body); | ||
var upgradedOrDowngradedMessageBodyAndSpecBody = upgradeOrDowngradeMessageBodyToMatchSpecBody(actual, expected), | ||
actualBody = upgradedOrDowngradedMessageBodyAndSpecBody.messageBody, | ||
expectedBody = upgradedOrDowngradedMessageBodyAndSpecBody.specBody, | ||
bodyDiff = diff(actualBody, expectedBody); | ||
if (bodyDiff) { | ||
@@ -409,9 +517,9 @@ output.append(bodyDiff.diff); | ||
var language = contentType.replace(/\s*;.*$/, ''); // Strip charset etc. | ||
output.code(actual.body, language); | ||
output.code(actualBody, language); | ||
} else { | ||
output.text(actual.body); | ||
output.text(actualBody); | ||
} | ||
} else { | ||
// Buffer instance or parsed JSON | ||
output.append(inspect(actual.body)); | ||
output.append(inspect(actualBody)); | ||
} | ||
@@ -428,2 +536,3 @@ } | ||
name: 'messy.RequestLine', | ||
base: 'object', | ||
identify: function (obj) { | ||
@@ -579,2 +688,3 @@ return obj && obj.isMessyRequestLine; | ||
name: 'messy.StatusLine', | ||
base: 'object', | ||
identify: function (obj) { | ||
@@ -741,2 +851,3 @@ return obj && obj.isMessyStatusLine; | ||
name: 'messy.HttpExchange', | ||
base: 'object', | ||
equal: function (httpExchange1, httpExchange2) { | ||
@@ -840,2 +951,3 @@ return httpExchange1.equals(httpExchange2); | ||
name: 'messy.HttpConversation', | ||
base: 'object', | ||
equal: function (httpConversation1, httpConversation2) { | ||
@@ -842,0 +954,0 @@ return httpConversation1.equals(httpConversation2); |
{ | ||
"name": "unexpected-messy", | ||
"version": "2.0.2", | ||
"version": "2.1.0", | ||
"description": "Unexpected plugin for the messy library", | ||
@@ -35,3 +35,3 @@ "main": "lib/unexpectedMessy.js", | ||
"peerDependencies": { | ||
"messy": "~1.4.0", | ||
"messy": "^2.0.0", | ||
"unexpected": ">=5.0.0-beta14" | ||
@@ -43,3 +43,3 @@ }, | ||
"jshint": "2.5.5", | ||
"messy": "=1.4.0", | ||
"messy": "=2.0.0", | ||
"mocha": "=1.21.4", | ||
@@ -46,0 +46,0 @@ "unexpected": ">=5.0.0-beta14" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
114928
2192