Comparing version 0.6.0 to 0.6.1
@@ -149,2 +149,42 @@ /*global unescape*/ | ||
Message.prototype.upgradeOrDowngradeBodyToMatchSpec = function (spec) { | ||
if (typeof this.body !== 'undefined') { | ||
var isJson = /^application\/json\b/i.test(this.headers.get('Content-Type')); | ||
if (isNonBufferNonRegExpObject(this.body) && isJson) { | ||
if (typeof spec.body === 'string' || Buffer.isBuffer(spec.body)) { | ||
var parsedSpecBody; | ||
try { | ||
parsedSpecBody = JSON.parse(spec.body); | ||
} catch (e) {} | ||
if (typeof parsedSpecBody !== 'undefined') { | ||
spec.body = JSON.stringify(canonicalizeObject(parsedSpecBody), undefined, ' '); | ||
this.body = JSON.stringify(this.body, undefined, ' '); | ||
} | ||
} else if (isRegExp(spec.body)) { | ||
this.body = JSON.stringify(this.body, undefined, ' '); | ||
} | ||
} | ||
if (Buffer.isBuffer(this.body) && (typeof spec.body === 'string' || isRegExp(spec.body) || isNonBufferNonRegExpObject(spec.body)) || (typeof spec.body === 'undefined' && bufferCanBeInterpretedAsUtf8(this.body) && isTextualContentType(this.headers.get('Content-Type')))) { | ||
try { | ||
this.body = this.body.toString('utf-8'); | ||
} catch (e) { | ||
// The body cannot be intepreted as utf-8, keep it as a Buffer instance | ||
} | ||
} | ||
if (isJson && typeof this.body === 'string' && (typeof spec.body === 'undefined' || isNonBufferNonRegExpObject(spec.body))) { | ||
try { | ||
this.body = JSON.parse(this.body); | ||
} catch (e) { | ||
// The body cannot be parsed as JSON, kepe as a string instance | ||
} | ||
} else if (Buffer.isBuffer(spec.body) && (!this.body || typeof this.body === 'string')) { | ||
this.body = new Buffer(this.body, 'utf-8'); | ||
} | ||
} else if (Buffer.isBuffer(spec.body) && spec.body.length === 0) { | ||
this.body = new Buffer([]); | ||
} else if (spec.body === '') { | ||
this.body = ''; | ||
} | ||
}; | ||
// Exploratory work wrt. https://github.com/sunesimonsen/unexpected/issues/40 | ||
@@ -163,62 +203,22 @@ Message.prototype.satisfies = function (spec, mustBeExhaustive) { | ||
var thisBody = this.body; | ||
this.upgradeOrDowngradeBodyToMatchSpec(spec); | ||
if ('body' in spec) { | ||
var specBody = spec.body; | ||
if (typeof thisBody !== 'undefined') { | ||
if (isNonBufferNonRegExpObject(thisBody) && /^application\/json\b/i.test(this.headers.get('Content-Type'))) { | ||
if (typeof specBody === 'string' || Buffer.isBuffer(specBody)) { | ||
var parsedSpecBody; | ||
try { | ||
parsedSpecBody = JSON.parse(specBody); | ||
} catch (e) {} | ||
if (typeof parsedSpecBody !== 'undefined') { | ||
specBody = JSON.stringify(canonicalizeObject(parsedSpecBody), undefined, ' '); | ||
thisBody = JSON.stringify(thisBody, undefined, ' '); | ||
} | ||
} else if (isRegExp(specBody)) { | ||
thisBody = JSON.stringify(thisBody, undefined, ' '); | ||
} | ||
} | ||
if (Buffer.isBuffer(thisBody) && (typeof specBody === 'string' || isRegExp(specBody) || isNonBufferNonRegExpObject(specBody)) || (typeof specBody === 'undefined' && bufferCanBeInterpretedAsUtf8(thisBody) && isTextualContentType(this.headers.get('Content-Type')))) { | ||
try { | ||
thisBody = thisBody.toString('utf-8'); | ||
} catch (e) { | ||
// The body cannot be intepreted as utf-8, keep it as a Buffer instance | ||
} | ||
} | ||
if (/^application\/json\b/i.test(this.headers.get('Content-Type')) && typeof thisBody === 'string' && (typeof specBody === 'undefined' || isNonBufferNonRegExpObject(specBody))) { | ||
try { | ||
thisBody = JSON.parse(thisBody); | ||
} catch (e) { | ||
// The body cannot be parsed as JSON, kepe as a string instance | ||
} | ||
} else if (Buffer.isBuffer(specBody) && (!thisBody || typeof thisBody === 'string')) { | ||
thisBody = new Buffer(thisBody, 'utf-8'); | ||
} | ||
} else if (Buffer.isBuffer(specBody) && specBody.length === 0) { | ||
thisBody = new Buffer([]); | ||
} else if (specBody === '') { | ||
thisBody = ''; | ||
} | ||
if (Buffer.isBuffer(specBody) && Buffer.isBuffer(thisBody)) { | ||
if (!buffersEqual(specBody, thisBody)) { | ||
if (Buffer.isBuffer(spec.body) && Buffer.isBuffer(this.body)) { | ||
if (!buffersEqual(spec.body, this.body)) { | ||
return false; | ||
} | ||
} else if (isRegExp(specBody) && typeof thisBody === 'string') { | ||
if (!specBody.test(thisBody)) { | ||
} else if (isRegExp(spec.body) && typeof this.body === 'string') { | ||
if (!spec.body.test(this.body)) { | ||
return false; | ||
} | ||
} else if (typeof specBody === 'string' && typeof thisBody === 'string') { | ||
if (specBody !== thisBody) { | ||
} else if (typeof spec.body === 'string' && typeof this.body === 'string') { | ||
if (spec.body !== this.body) { | ||
return false; | ||
} | ||
} else if (typeof specBody === 'undefined') { | ||
if (typeof thisBody !== 'undefined') { | ||
} else if (typeof spec.body === 'undefined') { | ||
if (typeof this.body !== 'undefined') { | ||
return false; | ||
} | ||
} else if (isNonBufferNonRegExpObject(specBody) && isNonBufferNonRegExpObject(thisBody)) { | ||
if (JSON.stringify(canonicalizeObject(specBody)) !== JSON.stringify(canonicalizeObject(thisBody))) { | ||
} else if (isNonBufferNonRegExpObject(spec.body) && isNonBufferNonRegExpObject(this.body)) { | ||
if (JSON.stringify(canonicalizeObject(spec.body)) !== JSON.stringify(canonicalizeObject(this.body))) { | ||
return false; | ||
@@ -229,3 +229,3 @@ } | ||
} | ||
} else if (mustBeExhaustive && typeof thisBody !== 'undefined') { | ||
} else if (mustBeExhaustive && typeof this.body !== 'undefined') { | ||
return false; | ||
@@ -232,0 +232,0 @@ } |
{ | ||
"name": "messy", | ||
"version": "0.6.0", | ||
"version": "0.6.1", | ||
"description": "Object model for HTTP and RFC822 messages", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
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
1504
75378