Permalink
Browse files

Added specs for body option

Removed old body test and failing gmail.com request
  • Loading branch information...
1 parent 86895b9 commit 02f6b38c1697a55ed43940d1fd0bef6225d4faa2 @Stanley Stanley committed Jan 30, 2011
Showing with 186 additions and 21 deletions.
  1. +14 โˆ’0 README.md
  2. +50 โˆ’0 specs.js
  3. +122 โˆ’0 specs/body-spec.js
  4. +0 โˆ’21 tests/couch.js
View
@@ -6,6 +6,20 @@
npm install request
</pre>
+Or from source:
+
+<pre>
+ git clone git://github.com/mikeal/request.git
+ cd request
+ 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.
View
@@ -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);
View
@@ -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': '<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
@@ -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);
-})

0 comments on commit 02f6b38

Please sign in to comment.