diff --git a/README.md b/README.md index 33930c94e..2fa8aa3a9 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,20 @@ npm install request +Or from source: + +
+  git clone git://github.com/mikeal/request.git 
+  cd request
+  npm link .
+
+ +Running specs: +
+  node specs.js
+
+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. diff --git a/specs.js b/specs.js new file mode 100644 index 000000000..e6f170804 --- /dev/null +++ b/specs.js @@ -0,0 +1,50 @@ +var jasmine = require('jasmine-node'); +var sys = require('sys'), + Path= require('path'); + +var SPEC_FOLDER= Path.join(process.cwd(), 'specs'), + SPEC_MATCHER_REGEX= "spec\.js$", + HELPER_MATCHER_REGEX= "helper\.js$"; + +for (var key in jasmine) + global[key] = jasmine[key]; + +var isVerbose = false; +var showColors = true; +var spec = SPEC_MATCHER_REGEX; + +function escapeRegex(text) { + return text.replace(escapeRegex._escapeRegex, '\\$1'); +} + +/** The special characters in a string that need escaping for regular expressions. */ +escapeRegex.specialCharacters= ['/', '.', '*', '+', '?', '|', + '(', ')', '[', ']', '{', '}', '\\']; + +/** A regular expression that will match any special characters that need to be + escaped to create a valid regular expression. */ +escapeRegex._escapeRegex= new RegExp('(\\'+ escapeRegex.specialCharacters.join("|\\") + ')', 'g'); + +process.argv.forEach(function(arg, index){ + switch(arg){ + case '--color': + showColors = true; + break; + case '--noColor': + showColors = false; + break; + case '--verbose': + isVerbose = true; + break; + + default: + if (index>1) + spec= "^.*/" + escapeRegex(arg) + "$"; + break; + } +}); + +jasmine.loadHelpersInFolder(SPEC_FOLDER, HELPER_MATCHER_REGEX); +jasmine.executeSpecsInFolder(SPEC_FOLDER, function(runner, log){ + process.exit(runner.results().failedCount); +}, isVerbose, showColors, spec); diff --git a/specs/body-spec.js b/specs/body-spec.js new file mode 100644 index 000000000..528002f25 --- /dev/null +++ b/specs/body-spec.js @@ -0,0 +1,122 @@ +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': 'Oh hi.'}, {'body': 'Oh hi.'}] + }); + + var body = '--frontier\r\n' + + 'content-type: text/html\r\n' + + '\r\n' + + 'Oh hi.' + + '\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')) + }); +}) diff --git a/tests/couch.js b/tests/couch.js index 4540c8b09..689e9fdc8 100644 --- a/tests/couch.js +++ b/tests/couch.js @@ -29,24 +29,3 @@ function testportsStream (port) { }) } testports(); -var randomnumber=Math.floor(Math.random()*100000000).toString(); -request({uri:'http://mikeal.couchone.com/testjs', method:'POST', headers: h, body:'{"_id":"'+randomnumber+'"}'}, - function (error, response, body) { - if (error) {throw new Error(error)}; - assert.equal(response.statusCode, 201, body); - }); - -var randomnumber=Math.floor(Math.random()*100000000).toString(); -request({uri:'http://mikeal.couchone.com/testjs', method:'POST', body:{"_id": 'randomnumber'}}, - function (error, response, body) { - if (error) {throw new Error(error)}; - assert.equal(response.statusCode, 201, body); - }); - -var options = {uri:'http://gmail.com'}; -request(options, function (error, response, body) { - if (error) throw error; - assert.equal(response.statusCode, 200); - assert.equal(options.uri.host, 'www.google.com'); - assert.equal(response.socket.port, 443); -})