Comparing version 0.6.1 to 0.7.0
var Message = require('./Message'), | ||
RequestLine = require('./RequestLine'), | ||
util = require('util'), | ||
_ = require('underscore'), | ||
isRegExp = require('./isRegExp'); | ||
function HttpRequest(obj) { | ||
this.requestLine = new RequestLine(); | ||
Message.call(this, obj); | ||
} | ||
HttpRequest.propertyNames = ['requestLine', 'method', 'url', 'path', 'search', 'query', 'protocol', 'protocolName', 'protocolVersion']; | ||
util.inherits(HttpRequest, Message); | ||
@@ -22,76 +23,8 @@ | ||
Message.prototype.populateFromObject.call(this, obj); | ||
HttpRequest.propertyNames.forEach(function (propertyName) { | ||
if (typeof obj[propertyName] !== 'undefined') { | ||
this[propertyName] = obj[propertyName]; | ||
} | ||
}, this); | ||
}; | ||
HttpRequest.prototype.populateProtocolFromString = function (protocol) { | ||
var protocolFragments = protocol.split('/'); | ||
if (protocolFragments.length === 2) { | ||
this.protocolName = protocolFragments[0]; | ||
this.protocolVersion = protocolFragments[1]; | ||
} else { | ||
throw new Error('Could not parse protocol: ' + protocol); | ||
if (typeof obj.requestLine !== 'undefined') { | ||
this.requestLine.populate(obj.requestLine); | ||
} | ||
this.requestLine.populateFromObject(obj); | ||
}; | ||
HttpRequest.prototype.populateRequestLineFromString = function (requestLine) { | ||
var requestLineFragments = requestLine.split(/\s+/); | ||
if (requestLineFragments.length === 3) { | ||
this.method = requestLineFragments[0].toUpperCase(); | ||
this.url = requestLineFragments[1]; | ||
this.populateProtocolFromString(requestLineFragments[2]); | ||
} else { | ||
throw new Error('Could not parse request line: ' + requestLine); | ||
} | ||
}; | ||
HttpRequest.prototype.populateUrlFromString = function (url) { | ||
var matchUrl = url.match(/^([^?]*)(\?.*)?$/); | ||
this.path = matchUrl[1] || ''; | ||
this.search = matchUrl[2] || ''; | ||
}; | ||
Object.defineProperty(HttpRequest.prototype, 'requestLine', { | ||
enumerable: true, | ||
get: function () { | ||
return String(this.method).toUpperCase() + ' ' + this.url + ' ' + this.protocol; | ||
}, | ||
set: function (requestLine) { | ||
this.populateRequestLineFromString(requestLine); | ||
} | ||
}); | ||
Object.defineProperty(HttpRequest.prototype, 'protocol', { | ||
enumerable: true, | ||
get: function () { | ||
return String(this.protocolName).toUpperCase() + '/' + this.protocolVersion; | ||
}, | ||
set: function (protocol) { | ||
this.populateProtocolFromString(protocol); | ||
} | ||
}); | ||
Object.defineProperty(HttpRequest.prototype, 'url', { | ||
enumerable: true, | ||
get: function () { | ||
return (this.path || '') + (this.search || ''); | ||
}, | ||
set: function (url) { | ||
this.populateUrlFromString(url); | ||
} | ||
}); | ||
Object.defineProperty(HttpRequest.prototype, 'query', { | ||
enumerable: true, | ||
get: function () { | ||
return (this.search || '').replace(/^\?/, ''); | ||
}, | ||
set: function (query) { | ||
this.url = this.url.replace(/(?:\?.*)?$/, '?' + query); | ||
} | ||
}); | ||
HttpRequest.prototype.populateFromString = function (str) { | ||
@@ -101,3 +34,3 @@ var matchRequestLine = str.match(/^([^\r\n]*)(\r\n?|\n\r?|$)/); | ||
if (matchRequestLine) { | ||
this.populateRequestLineFromString(matchRequestLine[1]); | ||
this.requestLine.populateFromString(matchRequestLine[1]); | ||
Message.prototype.populateFromString.call(this, str.substr(matchRequestLine[0].length)); | ||
@@ -111,3 +44,3 @@ } else { | ||
return ( | ||
this.requestLine + '\r\n' + | ||
this.requestLine.toString() + '\r\n' + | ||
Message.prototype.toString.call(this, maxLineLength) | ||
@@ -120,7 +53,3 @@ ); | ||
other instanceof HttpRequest && | ||
this.method === other.method && | ||
(this.path || '') === (other.path || '') && | ||
(this.search || '') === (other.search || '') && | ||
this.protocolName === other.protocolName && | ||
this.protocolVersion === other.protocolVersion && | ||
this.requestLine.equals(other.requestLine) && | ||
Message.prototype.equals.call(this, other) | ||
@@ -130,2 +59,14 @@ ); | ||
RequestLine.propertyNames.forEach(function (requestLinePropertyName) { | ||
Object.defineProperty(HttpRequest.prototype, requestLinePropertyName, { | ||
enumerable: true, | ||
get: function () { | ||
return this.requestLine[requestLinePropertyName]; | ||
}, | ||
set: function (value) { | ||
this.requestLine[requestLinePropertyName] = value; | ||
} | ||
}); | ||
}); | ||
// Exploratory work wrt. https://github.com/sunesimonsen/unexpected/issues/40 | ||
@@ -138,11 +79,22 @@ // FIXME: Makes no sense that you have to specify every property, | ||
} | ||
return HttpRequest.propertyNames.every(function (propertyName) { | ||
return (propertyName in spec) ? | ||
(isRegExp(spec[propertyName]) ? | ||
spec[propertyName].test(this[propertyName]) : | ||
this[propertyName] === spec[propertyName]) : | ||
!mustBeExhaustive || typeof this[propertyName] === 'undefined'; | ||
}, this); | ||
if ('requestLine' in spec) { | ||
if (spec.requestLine && typeof spec.requestLine === 'object') { | ||
if (!this.requestLine.satisfies(spec.requestLine, mustBeExhaustive)) { | ||
return false; | ||
} | ||
} else if (isRegExp(spec.requestLine)) { | ||
if (!spec.requestLine.test(this.requestLine.toString())) { | ||
return false; | ||
} | ||
} else if (this.requestLine.toString() !== spec.requestLine) { | ||
return false; | ||
} | ||
} | ||
// Make the RequestLine properties available for matching: | ||
if (!this.requestLine.satisfies(_.pick(spec, RequestLine.propertyNames), mustBeExhaustive)) { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
module.exports = HttpRequest; |
var Message = require('./Message'), | ||
StatusLine = require('./StatusLine'), | ||
util = require('util'), | ||
_ = require('underscore'), | ||
isRegExp = require('./isRegExp'); | ||
function HttpResponse(obj) { | ||
this.statusLine = new StatusLine(); | ||
Message.call(this, obj); | ||
} | ||
HttpResponse.propertyNames = ['statusLine', 'protocol', 'protocolName', 'protocolVersion', 'statusCode', 'statusMessage']; | ||
util.inherits(HttpResponse, Message); | ||
@@ -22,50 +23,8 @@ | ||
Message.prototype.populateFromObject.call(this, obj); | ||
HttpResponse.propertyNames.forEach(function (propertyName) { | ||
if (typeof obj[propertyName] !== 'undefined') { | ||
this[propertyName] = obj[propertyName]; | ||
} | ||
}, this); | ||
}; | ||
HttpResponse.prototype.populateProtocolFromString = function (protocol) { | ||
var protocolFragments = protocol.split('/'); | ||
if (protocolFragments.length === 2) { | ||
this.protocolName = protocolFragments[0]; | ||
this.protocolVersion = protocolFragments[1]; | ||
} else { | ||
throw new Error('Could not parse protocol: ' + protocol); | ||
if (typeof obj.statusLine !== 'undefined') { | ||
this.statusLine.populate(obj.statusLine); | ||
} | ||
this.statusLine.populateFromObject(obj); | ||
}; | ||
HttpResponse.prototype.populateStatusLineFromString = function (statusLine) { | ||
var matchStatusLine = statusLine.match(/^(\S+) (\d+) (.+)$/); | ||
if (matchStatusLine) { | ||
this.populateProtocolFromString(matchStatusLine[1]); | ||
this.statusCode = parseInt(matchStatusLine[2], 10); | ||
this.statusMessage = matchStatusLine[3]; | ||
} else { | ||
throw new Error('Could not parse status line: ' + statusLine); | ||
} | ||
}; | ||
Object.defineProperty(HttpResponse.prototype, 'protocol', { | ||
enumerable: true, | ||
get: function () { | ||
return String(this.protocolName).toUpperCase() + '/' + this.protocolVersion; | ||
}, | ||
set: function (protocol) { | ||
this.populateProtocolFromString(protocol); | ||
} | ||
}); | ||
Object.defineProperty(HttpResponse.prototype, 'statusLine', { | ||
enumerable: true, | ||
get: function () { | ||
return this.protocol + ' ' + this.statusCode + ' ' + this.statusMessage; | ||
}, | ||
set: function (statusLine) { | ||
this.populateStatusLineFromString(statusLine); | ||
} | ||
}); | ||
HttpResponse.prototype.populateFromString = function (str) { | ||
@@ -75,6 +34,6 @@ var matchStatusLine = str.match(/^([^\r\n]*)(\r\n?|\n\r?|$)/); | ||
if (matchStatusLine) { | ||
this.populateStatusLineFromString(matchStatusLine[1]); | ||
this.statusLine.populateFromString(matchStatusLine[1]); | ||
Message.prototype.populateFromString.call(this, str.substr(matchStatusLine[0].length)); | ||
} else { | ||
throw new Error('Could not find request line'); | ||
throw new Error('Could not find status line'); | ||
} | ||
@@ -85,3 +44,3 @@ }; | ||
return ( | ||
this.statusLine + '\r\n' + | ||
this.statusLine.toString() + '\r\n' + | ||
Message.prototype.toString.call(this, maxLineLength) | ||
@@ -94,6 +53,3 @@ ); | ||
other instanceof HttpResponse && | ||
this.protocolName === other.protocolName && | ||
this.protocolVersion === other.protocolVersion && | ||
this.statusCode === other.statusCode && | ||
this.statusMessage === other.statusMessage && | ||
this.statusLine.equals(other.statusLine) && | ||
Message.prototype.equals.call(this, other) | ||
@@ -103,2 +59,14 @@ ); | ||
StatusLine.propertyNames.forEach(function (statusLinePropertyName) { | ||
Object.defineProperty(HttpResponse.prototype, statusLinePropertyName, { | ||
enumerable: true, | ||
get: function () { | ||
return this.statusLine[statusLinePropertyName]; | ||
}, | ||
set: function (value) { | ||
this.statusLine[statusLinePropertyName] = value; | ||
} | ||
}); | ||
}); | ||
// Exploratory work wrt. https://github.com/sunesimonsen/unexpected/issues/40 | ||
@@ -111,11 +79,22 @@ // FIXME: Makes no sense that you have to specify every property, | ||
} | ||
return HttpResponse.propertyNames.every(function (propertyName) { | ||
return (propertyName in spec) ? | ||
(isRegExp(spec[propertyName]) ? | ||
spec[propertyName].test(this[propertyName]) : | ||
this[propertyName] === spec[propertyName]) : | ||
!mustBeExhaustive || typeof this[propertyName] === 'undefined'; | ||
}, this); | ||
if ('statusLine' in spec) { | ||
if (spec.statusLine && typeof spec.statusLine === 'object') { | ||
if (!this.statusLine.satisfies(spec.statusLine, mustBeExhaustive)) { | ||
return false; | ||
} | ||
} else if (isRegExp(spec.statusLine)) { | ||
if (!spec.statusLine.test(this.statusLine.toString())) { | ||
return false; | ||
} | ||
} else if (this.statusLine.toString() !== spec.statusLine) { | ||
return false; | ||
} | ||
} | ||
// Make the StatusLine properties available for matching: | ||
if (!this.statusLine.satisfies(_.pick(spec, StatusLine.propertyNames), mustBeExhaustive)) { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
module.exports = HttpResponse; |
module.exports = { | ||
Headers: require('./Headers'), | ||
Message: require('./Message'), | ||
RequestLine: require('./RequestLine'), | ||
HttpRequest: require('./HttpRequest'), | ||
StatusLine: require('./StatusLine'), | ||
HttpResponse: require('./HttpResponse'), | ||
@@ -6,0 +8,0 @@ formatHeaderName: require('./formatHeaderName'), |
{ | ||
"name": "messy", | ||
"version": "0.6.1", | ||
"version": "0.7.0", | ||
"description": "Object model for HTTP and RFC822 messages", | ||
@@ -32,3 +32,6 @@ "main": "lib/index.js", | ||
"unexpected": "=4.0.5" | ||
}, | ||
"dependencies": { | ||
"underscore": "^1.6.0" | ||
} | ||
} |
@@ -35,3 +35,3 @@ /*global describe, it*/ | ||
it('should make the request line available as a getter', function () { | ||
it('should make the request line available', function () { | ||
expect(new HttpRequest({ | ||
@@ -41,3 +41,3 @@ method: 'GET', | ||
protocol: 'HTTP/1.1' | ||
}).requestLine, 'to equal', 'GET /foo HTTP/1.1'); | ||
}).requestLine.toString(), 'to equal', 'GET /foo HTTP/1.1'); | ||
}); | ||
@@ -51,3 +51,3 @@ | ||
}); | ||
httpRequest.requestLine = 'PUT /bar HTTP/1.0'; | ||
httpRequest.requestLine.populateFromString('PUT /bar HTTP/1.0'); | ||
expect(httpRequest, 'to have properties', { | ||
@@ -90,3 +90,3 @@ method: 'PUT', | ||
protocol: 'http/1.1' | ||
}).requestLine, 'to equal', 'GET /foo HTTP/1.1'); | ||
}).requestLine.toString(), 'to equal', 'GET /foo HTTP/1.1'); | ||
}); | ||
@@ -93,0 +93,0 @@ |
@@ -43,3 +43,3 @@ /*global describe, it*/ | ||
it('should make the status line available as a getter', function () { | ||
it('should expose the StatusLine instance', function () { | ||
expect(new HttpResponse({ | ||
@@ -49,6 +49,6 @@ protocol: 'HTTP/1.1', | ||
statusMessage: 'OK' | ||
}).statusLine, 'to equal', 'HTTP/1.1 200 OK'); | ||
}).statusLine.toString(), 'to equal', 'HTTP/1.1 200 OK'); | ||
}); | ||
it('should allow updating the status line via a setter', function () { | ||
it('should allow updating the status line', function () { | ||
var httpResponse = new HttpResponse({ | ||
@@ -59,3 +59,3 @@ protocol: 'HTTP/1.1', | ||
}); | ||
httpResponse.statusLine = 'HTTP/1.0 400 Bad Request'; | ||
httpResponse.statusLine.populateFromString('HTTP/1.0 400 Bad Request'); | ||
expect(httpResponse, 'to have properties', { | ||
@@ -62,0 +62,0 @@ protocol: 'HTTP/1.0', |
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
79642
21
1616
1
+ Addedunderscore@^1.6.0
+ Addedunderscore@1.13.7(transitive)