Comparing version 6.4.0 to 6.5.0
@@ -381,2 +381,15 @@ var foldHeaderLine = require('./foldHeaderLine'), | ||
Headers.prototype.toJSON = function () { | ||
var obj = {}; | ||
this.getNames().forEach(function (lowerCaseHeaderName) { | ||
var values = this.getAll(lowerCaseHeaderName); | ||
if (values.length === 1) { | ||
obj[formatHeaderName(lowerCaseHeaderName)] = this.get(lowerCaseHeaderName); | ||
} else { | ||
obj[formatHeaderName(lowerCaseHeaderName)] = this.getAll(lowerCaseHeaderName); | ||
} | ||
}, this); | ||
return obj; | ||
}; | ||
module.exports = Headers; |
@@ -40,2 +40,10 @@ var HttpExchange = require('./HttpExchange'); | ||
HttpConversation.prototype.toJSON = function () { | ||
return { | ||
exchanges: this.exchanges.map(function (exchange) { | ||
return exchange.toJSON(); | ||
}) | ||
}; | ||
}; | ||
module.exports = HttpConversation; |
@@ -38,2 +38,13 @@ var HttpRequest = require('./HttpRequest'), | ||
HttpExchange.prototype.toJSON = function () { | ||
var obj = {}; | ||
if (this.request) { | ||
obj.request = this.request.toJSON(); | ||
} | ||
if (this.response) { | ||
obj.response = this.response.toJSON(); | ||
} | ||
return obj; | ||
}; | ||
module.exports = HttpExchange; |
@@ -173,2 +173,6 @@ /*global btoa*/ | ||
HttpRequest.prototype.toJSON = function () { | ||
return _.extend(Message.prototype.toJSON.call(this), this.requestLine.toJSON()); | ||
}; | ||
module.exports = HttpRequest; |
var Message = require('./Message'), | ||
StatusLine = require('./StatusLine'), | ||
util = require('util'); | ||
util = require('util'), | ||
_ = require('underscore'); | ||
@@ -101,2 +102,6 @@ function HttpResponse(obj) { | ||
HttpResponse.prototype.toJSON = function () { | ||
return _.extend(Message.prototype.toJSON.call(this), this.statusLine.toJSON()); | ||
}; | ||
module.exports = HttpResponse; |
@@ -26,4 +26,8 @@ /*global unescape, btoa, atob, JSON*/ | ||
Message.propertyNames = ['headers', 'body', 'unchunkedBody', 'rawBody', 'parts', 'fileName', 'isJson', 'isMultipart', 'boundary', 'charset']; | ||
// Descending priority: | ||
var bodyPropertyNames = ['parts', 'body', 'unchunkedBody', 'rawBody']; | ||
Message.propertyNames = ['headers', 'fileName', 'isJson', 'isMultipart', 'boundary', 'charset'].concat(bodyPropertyNames); | ||
Message.prototype.isMessyMessage = true; | ||
@@ -480,2 +484,22 @@ | ||
Message.prototype.toJSON = function () { | ||
var obj = {}; | ||
if (this.headers.getNames().length > 0) { | ||
obj.headers = this.headers.toJSON(); | ||
} | ||
bodyPropertyNames.some(function (bodyPropertyName) { | ||
var propertyValue = this['_' + bodyPropertyName]; | ||
if (propertyValue !== null && typeof propertyValue !== 'undefined') { // An empty string is OK, but we use both null and undefined | ||
if (bodyPropertyName === 'parts') { | ||
propertyValue = propertyValue.map(function (part) { | ||
return part.toJSON(); | ||
}); | ||
} | ||
obj[bodyPropertyName] = propertyValue; | ||
return true; | ||
} | ||
}, this); | ||
return obj; | ||
}; | ||
module.exports = Message; |
@@ -5,3 +5,4 @@ function RequestLine(obj) { | ||
RequestLine.propertyNames = ['method', 'url', 'path', 'search', 'query', 'protocol', 'protocolName', 'protocolVersion']; | ||
RequestLine.nonComputedPropertyNames = ['method', 'url', 'protocolName', 'protocolVersion']; | ||
RequestLine.propertyNames = ['protocol', 'path', 'search', 'query'].concat(RequestLine.nonComputedPropertyNames); | ||
@@ -126,2 +127,12 @@ RequestLine.prototype.isMessyRequestLine = true; | ||
RequestLine.prototype.toJSON = function () { | ||
var obj = {}; | ||
RequestLine.nonComputedPropertyNames.forEach(function (propertyName) { | ||
if (typeof this[propertyName] !== 'undefined') { | ||
obj[propertyName] = this[propertyName]; | ||
} | ||
}, this); | ||
return obj; | ||
}; | ||
module.exports = RequestLine; |
@@ -5,3 +5,4 @@ function StatusLine(obj) { | ||
StatusLine.propertyNames = ['protocol', 'protocolName', 'protocolVersion', 'statusCode', 'statusMessage']; | ||
StatusLine.nonComputedPropertyNames = ['protocolName', 'protocolVersion', 'statusCode', 'statusMessage']; | ||
StatusLine.propertyNames = ['protocol'].concat(StatusLine.nonComputedPropertyNames); | ||
@@ -94,2 +95,12 @@ StatusLine.prototype.isMessyStatusLine = true; | ||
StatusLine.prototype.toJSON = function () { | ||
var obj = {}; | ||
StatusLine.nonComputedPropertyNames.forEach(function (propertyName) { | ||
if (typeof this[propertyName] !== 'undefined') { | ||
obj[propertyName] = this[propertyName]; | ||
} | ||
}, this); | ||
return obj; | ||
}; | ||
module.exports = StatusLine; |
{ | ||
"name": "messy", | ||
"version": "6.4.0", | ||
"version": "6.5.0", | ||
"description": "Object model for HTTP and RFC822 messages", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -235,2 +235,12 @@ /*global describe, it*/ | ||
}); | ||
describe('#toJSON', function () { | ||
it('should output a single-value header as a string', function () { | ||
expect(new Headers('Foo: bar').toJSON(), 'to equal', { Foo: 'bar' }); | ||
}); | ||
it('should output a multi-value header as an array of strings', function () { | ||
expect(new Headers('Foo: bar\r\nFoo: quux').toJSON(), 'to equal', { Foo: [ 'bar', 'quux' ] }); | ||
}); | ||
}); | ||
}); |
@@ -138,2 +138,66 @@ /*global describe, it*/ | ||
}); | ||
describe('#toJSON', function () { | ||
it('should return an object with the exchanges JSONified', function () { | ||
expect(new HttpConversation({ | ||
exchanges: [ | ||
{ | ||
request: new HttpRequest('GET / HTTP/1.1\nFoo: Bar\n\nblah'), | ||
response: new HttpResponse('HTTP/1.1 200 OK\nQuux: Baz\n\nblaf') | ||
}, | ||
{ | ||
request: new HttpRequest('GET /foo HTTP/1.1\nFoo: Barrr\n\nblahhh'), | ||
response: new HttpResponse('HTTP/1.1 412 Precondition Failed\nQuux: Bazzz\n\nblafff') | ||
} | ||
] | ||
}).toJSON(), 'to equal', { | ||
exchanges: [ | ||
{ | ||
request: { | ||
method: 'GET', | ||
url: '/', | ||
protocolName: 'HTTP', | ||
protocolVersion: '1.1', | ||
headers: { | ||
Foo: 'Bar' | ||
}, | ||
rawBody: 'blah' | ||
}, | ||
response: { | ||
statusCode: 200, | ||
statusMessage: 'OK', | ||
protocolName: 'HTTP', | ||
protocolVersion: '1.1', | ||
headers: { | ||
Quux: 'Baz' | ||
}, | ||
rawBody: 'blaf' | ||
} | ||
}, | ||
{ | ||
request: { | ||
method: 'GET', | ||
url: '/foo', | ||
protocolName: 'HTTP', | ||
protocolVersion: '1.1', | ||
headers: { | ||
Foo: 'Barrr' | ||
}, | ||
rawBody: 'blahhh' | ||
}, | ||
response: { | ||
statusCode: 412, | ||
statusMessage: 'Precondition Failed', | ||
protocolName: 'HTTP', | ||
protocolVersion: '1.1', | ||
headers: { | ||
Quux: 'Bazzz' | ||
}, | ||
rawBody: 'blafff' | ||
} | ||
} | ||
] | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -119,2 +119,45 @@ /*global describe, it*/ | ||
}); | ||
describe('#toJSON', function () { | ||
it('should return an object with the request and response JSONified', function () { | ||
expect(new HttpExchange({ | ||
request: new HttpRequest('GET / HTTP/1.1\nFoo: Bar\n\nblah'), | ||
response: new HttpResponse('HTTP/1.1 200 OK\nQuux: Baz\n\nblaf') | ||
}).toJSON(), 'to equal', { | ||
request: { | ||
method: 'GET', | ||
url: '/', | ||
protocolName: 'HTTP', | ||
protocolVersion: '1.1', | ||
headers: { | ||
Foo: 'Bar' | ||
}, | ||
rawBody: 'blah' | ||
}, | ||
response: { | ||
statusCode: 200, | ||
statusMessage: 'OK', | ||
protocolName: 'HTTP', | ||
protocolVersion: '1.1', | ||
headers: { | ||
Quux: 'Baz' | ||
}, | ||
rawBody: 'blaf' | ||
} | ||
}); | ||
}); | ||
// Makes it possible to use statusLine.toJSON() as the RHS of a 'to satisfy' assertion in Unexpected | ||
// where undefined means that the property must not be present: | ||
it('should not include the keys that have undefined values', function () { | ||
var httpExchange = new HttpExchange({ | ||
request: new HttpRequest('GET / HTTP/1.1\nFoo: Bar\n\nblah'), | ||
response: undefined | ||
}); | ||
httpExchange.request = undefined; | ||
expect(httpExchange.toJSON(), 'not to have keys', ['request', 'response']); | ||
}); | ||
}); | ||
}); |
@@ -295,2 +295,17 @@ /*global describe, it*/ | ||
}); | ||
describe('#toJSON', function () { | ||
it('should include the messy.Message properties and put the requestLine properties at the top level', function () { | ||
expect(new HttpRequest('GET / HTTP/1.1\r\nFoo: bar\r\n\r\nblabla').toJSON(), 'to equal', { | ||
method: 'GET', | ||
url: '/', | ||
protocolName: 'HTTP', | ||
protocolVersion: '1.1', | ||
headers: { | ||
Foo: 'bar' | ||
}, | ||
rawBody: 'blabla' | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -157,2 +157,17 @@ /*global describe, it*/ | ||
}); | ||
describe('#toJSON', function () { | ||
it('should include the messy.Message properties and put the statusLine properties at the top level', function () { | ||
expect(new HttpResponse('HTTP/1.1 200 OK\r\nFoo: bar\r\n\r\nblabla').toJSON(), 'to equal', { | ||
protocolName: 'HTTP', | ||
protocolVersion: '1.1', | ||
statusCode: 200, | ||
statusMessage: 'OK', | ||
headers: { | ||
Foo: 'bar' | ||
}, | ||
rawBody: 'blabla' | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -747,2 +747,85 @@ /*global describe, it*/ | ||
}); | ||
describe('#toJSON', function () { | ||
it('should include headers if any are present', function () { | ||
expect(new Message('Foo: bar').toJSON(), 'to equal', { | ||
headers: { | ||
Foo: 'bar' | ||
} | ||
}); | ||
}); | ||
it('should not include an empty headers object', function () { | ||
expect(new Message('\r\nhey').toJSON(), 'to equal', { | ||
rawBody: 'hey' | ||
}); | ||
}); | ||
it('should include the parsed representation of the body if it has been touched', function () { | ||
var message = new Message('Content-type: application/json\r\n\r\n{"foo":"bar"}'); | ||
message.body.blah = 123; | ||
expect(message.toJSON(), 'to equal', { | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: { | ||
foo: 'bar', | ||
blah: 123 | ||
} | ||
}); | ||
}); | ||
describe('with a multipart message', function () { | ||
it('should not include the parts if they have not been touched', function () { | ||
expect(new Message( | ||
'Content-Type: multipart/mixed; boundary=foo\r\n' + | ||
'\r\n' + | ||
'--foo\r\n' + | ||
'Content-Type: text/html; charset=UTF-8\r\n' + | ||
'\r\n' + | ||
'<h1>Hello, world!</h1>\r\n' + | ||
'--foo--\r\n' | ||
).toJSON(), 'to equal', { | ||
headers: { | ||
'Content-Type': 'multipart/mixed; boundary=foo' | ||
}, | ||
rawBody: | ||
'--foo\r\n' + | ||
'Content-Type: text/html; charset=UTF-8\r\n' + | ||
'\r\n' + | ||
'<h1>Hello, world!</h1>\r\n' + | ||
'--foo--\r\n' | ||
}); | ||
}); | ||
}); | ||
it('should include parts if they have been touched', function () { | ||
var message = new Message( | ||
'Content-Type: multipart/mixed; boundary=foo\r\n' + | ||
'\r\n' + | ||
'--foo\r\n' + | ||
'Content-Type: text/html; charset=UTF-8\r\n' + | ||
'\r\n' + | ||
'<h1>Hello, world!</h1>\r\n' + | ||
'--foo--\r\n' | ||
); | ||
message.parts[0].headers.set('Hey', 'there'); | ||
expect(message.toJSON(), 'to equal', { | ||
headers: { | ||
'Content-Type': 'multipart/mixed; boundary=foo' | ||
}, | ||
parts: [ | ||
{ | ||
headers: { | ||
'Content-Type': 'text/html; charset=UTF-8', | ||
Hey: 'there' | ||
}, | ||
rawBody: '<h1>Hello, world!</h1>' | ||
} | ||
] | ||
}); | ||
}); | ||
}); | ||
}); |
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
160608
33
3401