Forbes Lindesay
10 years ago
commit
2532bb7634
7 changed files with 140 additions and 0 deletions
@ -0,0 +1,13 @@ |
|||
lib-cov |
|||
*.seed |
|||
*.log |
|||
*.csv |
|||
*.dat |
|||
*.out |
|||
*.pid |
|||
*.gz |
|||
pids |
|||
logs |
|||
results |
|||
npm-debug.log |
|||
node_modules |
@ -0,0 +1,4 @@ |
|||
language: node_js |
|||
node_js: |
|||
- "0.8" |
|||
- "0.10" |
@ -0,0 +1,19 @@ |
|||
Copyright (c) 2014 Forbes Lindesay |
|||
|
|||
Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
of this software and associated documentation files (the "Software"), to deal |
|||
in the Software without restriction, including without limitation the rights |
|||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
copies of the Software, and to permit persons to whom the Software is |
|||
furnished to do so, subject to the following conditions: |
|||
|
|||
The above copyright notice and this permission notice shall be included in |
|||
all copies or substantial portions of the Software. |
|||
|
|||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|||
THE SOFTWARE. |
@ -0,0 +1,15 @@ |
|||
# http-response-object |
|||
|
|||
A simple object to represent an http response |
|||
|
|||
[![Build Status](https://travis-ci.org/ForbesLindesay/http-response-object.png?branch=master)](https://travis-ci.org/ForbesLindesay/http-response-object) |
|||
[![Dependency Status](https://gemnasium.com/ForbesLindesay/http-response-object.png)](https://gemnasium.com/ForbesLindesay/http-response-object) |
|||
[![NPM version](https://badge.fury.io/js/http-response-object.png)](http://badge.fury.io/js/http-response-object) |
|||
|
|||
## Installation |
|||
|
|||
npm install http-response-object |
|||
|
|||
## License |
|||
|
|||
MIT |
@ -0,0 +1,40 @@ |
|||
'use strict'; |
|||
|
|||
module.exports = Response; |
|||
|
|||
/** |
|||
* A response from a web request |
|||
* |
|||
* @param {Number} statusCode |
|||
* @param {Object} headers |
|||
* @param {Buffer} body |
|||
*/ |
|||
function Response(statusCode, headers, body) { |
|||
if (typeof statusCode !== 'number') { |
|||
throw new TypeError('statusCode must be a number but was ' + (typeof statusCode)); |
|||
} |
|||
if (headers === null) { |
|||
throw new TypeError('headers cannot be null'); |
|||
} |
|||
if (typeof headers !== 'object') { |
|||
throw new TypeError('headers must be an object but was ' + (typeof headers)); |
|||
} |
|||
this.statusCode = statusCode; |
|||
this.headers = {}; |
|||
for (var key in headers) { |
|||
this.headers[key.toLowerCase()] = headers[key]; |
|||
} |
|||
this.body = body; |
|||
} |
|||
|
|||
Response.prototype.getBody = function () { |
|||
if (this.statusCode >= 300) { |
|||
var err = new Error('Server responded with status code ' |
|||
+ this.statusCode + ':\n' + this.body.toString()); |
|||
err.statusCode = this.statusCode; |
|||
err.headers = this.headers; |
|||
err.body = this.body; |
|||
throw err; |
|||
} |
|||
return this.body; |
|||
}; |
@ -0,0 +1,15 @@ |
|||
{ |
|||
"name": "http-response-object", |
|||
"version": "0.0.0", |
|||
"description": "A simple object to represent an http response", |
|||
"keywords": ["http", "https", "response", "request"], |
|||
"scripts": { |
|||
"test": "node test" |
|||
}, |
|||
"repository": { |
|||
"type": "git", |
|||
"url": "https://github.com/ForbesLindesay/http-response-object.git" |
|||
}, |
|||
"author": "ForbesLindesay", |
|||
"license": "MIT" |
|||
} |
@ -0,0 +1,34 @@ |
|||
'use strict'; |
|||
|
|||
var assert = require('assert'); |
|||
var Response = require('../'); |
|||
|
|||
var res = new Response(200, { |
|||
'Foo-Bar': 'baz-Bosh', |
|||
'bar-foo': 'bish-Bosh' |
|||
}, 'foo bar baz'); |
|||
assert(res.statusCode = 200); |
|||
assert(res.headers['foo-bar'] === 'baz-Bosh'); |
|||
assert(res.headers['bar-foo'] === 'bish-Bosh'); |
|||
assert(res.body === 'foo bar baz'); |
|||
assert(res.getBody() === 'foo bar baz'); |
|||
|
|||
res = new Response(404, { |
|||
'Foo-Bar': 'baz-Bosh' |
|||
}, 'Could not find page'); |
|||
assert(res.statusCode = 404); |
|||
assert(res.headers['foo-bar'] === 'baz-Bosh'); |
|||
assert(res.body === 'Could not find page'); |
|||
var errored = false; |
|||
try { |
|||
res.getBody(); |
|||
} catch (ex) { |
|||
assert(ex.statusCode === 404); |
|||
assert(ex.headers['foo-bar'] === 'baz-Bosh'); |
|||
assert(res.body === 'Could not find page'); |
|||
errored = true; |
|||
} |
|||
if (!errored) { |
|||
throw new Error('res.getBody() should throw an error when the status code is 404'); |
|||
} |
|||
console.log('tests passed'); |
Loading…
Reference in new issue