Permalink
Browse files

Moved spec tests to normal node script tests. Style changes to code a…

…nd docs.
  • Loading branch information...
1 parent 641ec05 commit ab4c96be1c002c10806d967a4b266543f8b0267c @mikeal mikeal committed Jan 30, 2011
Showing with 80 additions and 186 deletions.
  1. +19 βˆ’22 README.md
  2. +12 βˆ’11 main.js
  3. +0 βˆ’122 specs/body-spec.js
  4. +0 βˆ’31 tests/couch.js
  5. +21 βˆ’0 tests/{tests.js β†’ test-body.js}
  6. +28 βˆ’0 tests/test-errors.js
View
@@ -14,12 +14,6 @@ Or from source:
npm link .
</pre>
-Running specs:
-<pre>
- node specs.js
-</pre>
-Note: `jasmine-node` package is required to run tests.
-
## Super simple to use
Request is designed to be the simplest way possible to make http calls. It support HTTPS and follows redirects by default.
@@ -57,22 +51,25 @@ Examples:
<pre>
var request = require('request');
var rand = Math.floor(Math.random()*100000000).toString();
- request({
- method: 'PUT',
- uri: 'http://mikeal.couchone.com/testjs/'+ rand,
- multipart: [
- { 'content-type': 'application/json',
- body: JSON.stringify({foo: 'bar', _attachments: {'message.txt': {follows: true, length: 18, 'content_type': 'text/plain' }}})},
- { body: 'I am an attachment' }
- ]
- }, function (error, response, body) {
- if(response.statusCode == 201){
- console.log('document saved as: http://mikeal.couchone.com/testjs/'+ rand);
- } else {
- console.log('error: '+ response.statusCode);
- console.log(body)
+ request(
+ { method: 'PUT'
+ , uri: 'http://mikeal.couchone.com/testjs/' + rand
+ , multipart:
+ [ { 'content-type': 'application/json'
+ , body: JSON.stringify({foo: 'bar', _attachments: {'message.txt': {follows: true, length: 18, 'content_type': 'text/plain' }}})
+ }
+ , { body: 'I am an attachment' }
+ ]
}
- })
+ , function (error, response, body) {
+ if(response.statusCode == 201){
+ console.log('document saved as: http://mikeal.couchone.com/testjs/'+ rand);
+ } else {
+ console.log('error: '+ response.statusCode);
+ console.log(body);
+ }
+ }
+ )
</pre>
-It's also worth noting that the options argument will mutate. When following a redirect the uri values will change. After setting up a client options it will set the client property.
+It's also worth noting that the options argument will mutate. When following a redirect the uri values will change. After setting up client options it will set options.client.
View
@@ -101,17 +101,17 @@ function request (options, callback) {
if (options.proxy) options.fullpath = (options.uri.protocol + '//' + options.uri.host + options.fullpath);
- if(options.json){
+ if (options.json) {
options.headers['content-type'] = 'application/json';
options.body = JSON.stringify(options.json);
- } else if(options.multipart){
+ } else if (options.multipart) {
options.body = '';
options.headers['content-type'] = 'multipart/related;boundary="frontier"';
- if(!options.multipart.forEach) throw new Error('Argument error');
- options.multipart.forEach(function(part){
+ if (!options.multipart.forEach) throw new Error('Argument error, options.multipart.');
+ options.multipart.forEach(function (part) {
var body = part.body;
- if(!body) throw Error('Body attribute missing')
+ if(!body) throw Error('Body attribute missing in multipart.');
delete part.body;
options.body += '--frontier\r\n';
Object.keys(part).forEach(function(key){
@@ -126,10 +126,11 @@ function request (options, callback) {
if (!Buffer.isBuffer(options.body)) {
options.body = new Buffer(options.body);
}
- if(options.body.length)
+ if (options.body.length) {
options.headers['content-length'] = options.body.length;
- else
- throw new Error('Argument error')
+ } else {
+ throw new Error('Argument error, options.body.');
+ }
}
options.request = options.client.request(options.method, options.fullpath, options.headers);
@@ -194,21 +195,21 @@ module.exports = request;
request.get = request;
request.post = function (options, callback) {
options.method = 'POST';
- if (!options.body && !options.requestBodyStream) {
+ if (!options.body && !options.requestBodyStream && !options.json && !options.multipart) {
sys.error("HTTP POST requests need a body or requestBodyStream");
}
request(options, callback);
};
request.put = function (options, callback) {
options.method = 'PUT';
- if (!options.body && !options.requestBodyStream) {
+ if (!options.body && !options.requestBodyStream && !options.json && !options.multipart) {
sys.error("HTTP PUT requests need a body or requestBodyStream");
}
request(options, callback);
};
request.head = function (options, callback) {
options.method = 'HEAD';
- if (options.body || options.requestBodyStream) {
+ if (options.body || options.requestBodyStream || options.json || options.multipart) {
throw new Error("HTTP HEAD requests MUST NOT include a request body.");
}
request(options, callback);
View
@@ -1,122 +0,0 @@
-var http = require('http');
- request = require('../main');
-
-describe('raw body', function(){
-
- var fakeClient = new http.Client();
- fakeRequest = new http.ClientRequest({});
-
- beforeEach(function(){
- spyOn(http, 'createClient').andReturn(fakeClient);
- spyOn(fakeClient, 'request').andReturn(fakeRequest);
- });
-
- it('should accept string', function(){
-
- request({
- method: 'POST',
- uri: 'http://nodejs.org',
- body: 'Oh hi.'
- });
-
- //expect(http.createClient).toHaveBeenCalledWith(80, 'nodejs.org', false); // TODO: move to basic-spec
- expect(fakeClient.request).toHaveBeenCalledWith('POST', '/', { host: 'nodejs.org', 'content-length': 'Oh hi.'.length });
- expect(fakeRequest.output[1].toString()).toEqual('Oh hi.');
- });
-
- it('should accept buffer', function(){
- request({
- method: 'POST',
- uri: 'http://nodejs.org',
- body: new Buffer('Oh hi.')
- });
-
- expect(fakeClient.request).toHaveBeenCalledWith('POST', '/', { host: 'nodejs.org', 'content-length': 'Oh hi.'.length });
- expect(fakeRequest.output[1].toString()).toEqual('Oh hi.');
- });
-});
-
-describe('json', function(){
-
- it('should be converted to json string', function(){
-
- var fakeClient = new http.Client();
- fakeRequest = new http.ClientRequest({});
-
- spyOn(http, 'createClient').andReturn(fakeClient);
- spyOn(fakeClient, 'request').andReturn(fakeRequest);
-
- request({
- method: 'POST',
- uri: 'http://nodejs.org',
- json: {foo: 'bar'}
- });
-
- expect(fakeClient.request).toHaveBeenCalledWith('POST', '/', {
- 'host': 'nodejs.org',
- 'content-length': JSON.stringify({foo: 'bar'}).length,
- 'content-type': 'application/json' });
- expect(fakeRequest.output[1].toString()).toEqual('{"foo":"bar"}');
- });
-});
-
-describe('multipart', function(){
- it('should be joined', function(){
-
- var fakeClient = new http.Client();
- fakeRequest = new http.ClientRequest({});
-
- spyOn(http, 'createClient').andReturn(fakeClient);
- spyOn(fakeClient, 'request').andReturn(fakeRequest);
-
- request({
- method: 'POST',
- uri: 'http://nodejs.org',
- multipart: [{'content-type': 'text/html', 'body': '<html><body>Oh hi.</body></html>'}, {'body': 'Oh hi.'}]
- });
-
- var body = '--frontier\r\n' +
- 'content-type: text/html\r\n' +
- '\r\n' +
- '<html><body>Oh hi.</body></html>' +
- '\r\n--frontier\r\n\r\n' +
- 'Oh hi.' +
- '\r\n--frontier--'
-
- expect(fakeClient.request).toHaveBeenCalledWith('POST', '/', {
- 'host': 'nodejs.org',
- 'content-length': new Buffer(body).length,
- 'content-type': 'multipart/related;boundary="frontier"' });
- expect(fakeRequest.output[1].toString()).toEqual(body);
- });
-});
-
-describe('exception', function(){
- it('should be thrown on non PUT and POST requests with body, json or multipart')
-
- it('should be thrown on non comaptibile body types', function(){
- expect(function(){
- request({
- method: 'POST',
- uri: 'http://nodejs.org',
- body: {foo: 'bar'}
- });
- }).toThrow(new Error('Argument error'))
-
- expect(function(){
- request({
- method: 'POST',
- uri: 'http://nodejs.org',
- multipart: 'foo'
- });
- }).toThrow(new Error('Argument error'))
-
- expect(function(){
- request({
- method: 'POST',
- uri: 'http://nodejs.org',
- multipart: [{}]
- });
- }).toThrow(new Error('Body attribute missing'))
- });
-})
View
@@ -1,31 +0,0 @@
-var request = require('../main')
- , sys = require('sys')
- , assert = require('assert')
- , h = {'content-type': 'application/json', 'accept': 'application/json'}
- ;
-
-function testports (port) {
- var uri = port ? 'http://mikeal.couchone.com' + ":" + port : 'http://mikeal.couchone.com';
- sys.puts(uri)
- request({uri:uri}, function (error, response, body) {
- if (error) {throw new Error(error)};
- assert.equal(response.statusCode, 200);
- assert.equal(body.slice(0, '{"couchdb":"Welcome",'.length), '{"couchdb":"Welcome",');
- })
-}
-testports();
-testports(80)
-testports(5984)
-
-function testportsStream (port) {
- var uri = port ? 'http://mikeal.couchone.com' + ":" + port : 'http://mikeal.couchone.com';
- sys.puts(uri)
- var body = ''
- var bs = {write:function (chunk) {body += chunk}}
- request({uri:uri}, function (error, response) {
- if (error) {throw new Error(error)};
- assert.equal(response.statusCode, 200);
- assert.equal(body.slice(0, '{"couchdb":"Welcome",'.length), '{"couchdb":"Welcome",');
- })
-}
-testports();
@@ -56,6 +56,27 @@ var tests =
, method : "PUT"
, requestBodyStream : createPostStream("PUTTINGDATA")
}
+ , testPutJSON :
+ { resp : createPostValidator(JSON.stringify({foo: 'bar'}))
+ , method: "PUT"
+ , json: {foo: 'bar'}
+ }
+ , testPutMultipart :
+ { resp: createPostValidator(
+ '--frontier\r\n' +
+ 'content-type: text/html\r\n' +
+ '\r\n' +
+ '<html><body>Oh hi.</body></html>' +
+ '\r\n--frontier\r\n\r\n' +
+ 'Oh hi.' +
+ '\r\n--frontier--'
+ )
+ , method: "PUT"
+ , multipart:
+ [ {'content-type': 'text/html', 'body': '<html><body>Oh hi.</body></html>'}
+ , {'body': 'Oh hi.'}
+ ]
+ }
}
var counter = 0;
View
@@ -0,0 +1,28 @@
+var server = require('./server')
+ , events = require('events')
+ , assert = require('assert')
+ , request = require('../main.js')
+ ;
+
+var local = 'http://localhost:8888/asdf'
+
+try {
+ request({uri:local, body:{}})
+ assert.fail("Should have throw")
+} catch(e) {
+ assert.equal(e.message, 'Argument error, options.body.')
+}
+
+try {
+ request({uri:local, multipart: 'foo'})
+ assert.fail("Should have throw")
+} catch(e) {
+ assert.equal(e.message, 'Argument error, options.multipart.')
+}
+
+try {
+ request({uri:local, multipart: [{}]})
+ assert.fail("Should have throw")
+} catch(e) {
+ assert.equal(e.message, 'Body attribute missing in multipart.')
+}

0 comments on commit ab4c96b

Please sign in to comment.