commit 2532bb76344051dd77538cbc052e832cd40f08bd Author: Forbes Lindesay Date: Fri Aug 1 13:34:06 2014 +0100 Initial code diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b83202d --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +lib-cov +*.seed +*.log +*.csv +*.dat +*.out +*.pid +*.gz +pids +logs +results +npm-debug.log +node_modules diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..a12e3f0 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,4 @@ +language: node_js +node_js: + - "0.8" + - "0.10" \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..27cc9f3 --- /dev/null +++ b/LICENSE @@ -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. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..be9a1d5 --- /dev/null +++ b/README.md @@ -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 \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..223e0e6 --- /dev/null +++ b/index.js @@ -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; +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..91d13c4 --- /dev/null +++ b/package.json @@ -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" +} diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..0585a84 --- /dev/null +++ b/test/index.js @@ -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');