-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/Stanley/request into json…
…body
- Loading branch information
Showing
5 changed files
with
242 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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')) | ||
}); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters