diff --git a/Readme.md b/Readme.md index 9a471b414..097bfadb8 100644 --- a/Readme.md +++ b/Readme.md @@ -19,7 +19,7 @@ Works with [browserify](https://github.com/substack/node-browserify) and should ```js request .post('/api/pet') - .send({ name: 'Manny', species: 'cat' }) + .send({ name: 'Manny', species: 'cat' }) // sends a JSON post body .set('X-API-Key', 'foobar') .set('Accept', 'application/json') .end(function(err, res){ diff --git a/docs/index.md b/docs/index.md index 874851e60..58829f074 100644 --- a/docs/index.md +++ b/docs/index.md @@ -150,7 +150,7 @@ A typical JSON __POST__ request might look a little like the following, where we .send('{"name":"tj","pet":"tobi"}') .end(callback) -Since JSON is undoubtably the most common, it's the _default_! The following example is equivalent to the previous. +Since JSON is undoubtedly the most common, it's the _default_! The following example is equivalent to the previous. request.post('/user') .send({ name: 'tj', pet: 'tobi' }) diff --git a/docs/test.html b/docs/test.html index c31657b80..c7319b42e 100644 --- a/docs/test.html +++ b/docs/test.html @@ -190,14 +190,15 @@
try {
+ - should only set headers for ownProperties of header
+ try {
request
.get(uri + '/echo')
.end(done);
} catch (e) {
done(e)
}
+ - Error: socket hang up
request
+.get(uri + '/querystring', {foo: 'FOO', bar: 'BAR'}, function(err, res){
+ try {
+ assert.deepEqual(res.body, { foo: 'FOO', bar: 'BAR' });
+ next();
+ } catch(e) { next(e); }
+});
request
+.head(uri + '/querystring-in-header', {foo: 'FOO', bar: 'BAR'}, function(err, res){
+ try {
+ assert.deepEqual(JSON.parse(res.headers.query), { foo: 'FOO', bar: 'BAR' });
+ next();
+ } catch(e) { next(e); }
});
request('GET', uri + '/foo').end(function(err, res){
@@ -2692,6 +2709,18 @@ request
request
.get(testEndpoint)
.pfx(pfx)
+.end(function(err, res){
+ assert(res.ok);
+ assert.strictEqual('Safe and secure!', res.text);
+ done();
+})
+ should give a good response with client pfx with passphrase
+ request
+.get(testEndpoint)
+.pfx({
+ pfx: passpfx,
+ passphrase: 'test'
+})
.end(function(err, res){
assert(res.ok);
assert.strictEqual('Safe and secure!', res.text);
diff --git a/index.html b/index.html
index 2e0da52b2..489a26b3b 100644
--- a/index.html
+++ b/index.html
@@ -119,7 +119,7 @@ Request basics
.set('Content-Type', 'application/json')
.send('{"name":"tj","pet":"tobi"}')
.end(callback)
-
Since JSON is undoubtably the most common, it's the default! The following example is equivalent to the previous.
+
Since JSON is undoubtedly the most common, it's the default! The following example is equivalent to the previous.
request.post('/user')
.send({ name: 'tj', pet: 'tobi' })
.end(callback)
@@ -140,6 +140,10 @@ Request basics
.send({ name: 'tj' })
.send({ pet: 'tobi' })
.end(callback)
+
Sending a FormData
object is also supported. The following example will POST the content of the HTML form identified by id="myForm":
request.post('/user')
+ .send(new FormData(document.getElementById('myForm')))
+ .end(callback)
Content-Type
The obvious solution is to use the .set()
method:
request.post('/user')
diff --git a/lib/client.js b/lib/client.js
index 38809ffba..3a683ec43 100644
--- a/lib/client.js
+++ b/lib/client.js
@@ -59,7 +59,7 @@ request.getXHR = function () {
try { return new ActiveXObject('Msxml2.XMLHTTP.3.0'); } catch(e) {}
try { return new ActiveXObject('Msxml2.XMLHTTP'); } catch(e) {}
}
- throw Error("Browser-only verison of superagent could not find XHR");
+ throw Error("Browser-only version of superagent could not find XHR");
};
/**
@@ -169,7 +169,7 @@ request.parseString = parseString;
request.types = {
html: 'text/html',
json: 'application/json',
- xml: 'application/xml',
+ xml: 'text/xml',
urlencoded: 'application/x-www-form-urlencoded',
'form': 'application/x-www-form-urlencoded',
'form-data': 'application/x-www-form-urlencoded'
@@ -525,10 +525,10 @@ Request.prototype.auth = function(user, pass, options){
this.username = user;
this.password = pass;
break;
-
+
case 'bearer': // usage would be .auth(accessToken, { type: 'bearer' })
this.set('Authorization', 'Bearer ' + user);
- break;
+ break;
}
return this;
};
@@ -807,7 +807,7 @@ request.get = function(url, data, fn){
request.head = function(url, data, fn){
var req = request('HEAD', url);
if ('function' == typeof data) fn = data, data = null;
- if (data) req.send(data);
+ if (data) req.query(data);
if (fn) req.end(fn);
return req;
};
diff --git a/lib/node/index.js b/lib/node/index.js
index a947631e0..11e16e657 100644
--- a/lib/node/index.js
+++ b/lib/node/index.js
@@ -528,7 +528,12 @@ Request.prototype.key = function(cert){
*/
Request.prototype.pfx = function(cert){
- this._pfx = cert;
+ if(typeof cert === 'object' && !Buffer.isBuffer(cert)){
+ this._pfx = cert.pfx;
+ this._passphrase = cert.passphrase;
+ } else {
+ this._pfx = cert;
+ }
return this;
};
@@ -596,6 +601,7 @@ Request.prototype.request = function(){
options.key = this._key;
options.pfx = this._pfx;
options.cert = this._cert;
+ options.passphrase = this._passphrase;
options.agent = this._agent;
// initiate request
@@ -603,6 +609,10 @@ Request.prototype.request = function(){
// request
var req = this.req = mod.request(options);
+
+ // set tcp no delay
+ req.setNoDelay(true);
+
if ('HEAD' != options.method) {
req.setHeader('Accept-Encoding', 'gzip, deflate');
}
@@ -974,7 +984,13 @@ methods.forEach(function(method){
request[name] = function(url, data, fn){
var req = request(method, url);
if ('function' == typeof data) fn = data, data = null;
- if (data) req.send(data);
+ if (data) {
+ if (method === 'GET' || method === 'HEAD') {
+ req.query(data);
+ } else {
+ req.send(data);
+ }
+ }
fn && req.end(fn);
return req;
};
diff --git a/lib/request-base.js b/lib/request-base.js
index e1d2a87b8..f24b3e105 100644
--- a/lib/request-base.js
+++ b/lib/request-base.js
@@ -108,7 +108,7 @@ RequestBase.prototype.serialize = function serialize(fn){
*
* Value of 0 or false means no timeout.
*
- * @param {Number|Object} ms or {response, read, deadline}
+ * @param {Number|Object} ms or {response, deadline}
* @return {Request} for chaining
* @api public
*/
diff --git a/package.json b/package.json
index 1dbe7a917..44ee6dbd2 100644
--- a/package.json
+++ b/package.json
@@ -31,7 +31,7 @@
"form-data": "^2.1.1",
"formidable": "^1.1.1",
"methods": "^1.1.1",
- "mime": "^1.3.4",
+ "mime": "^1.3.6",
"qs": "^6.4.0",
"readable-stream": "^2.0.5"
},
@@ -61,6 +61,6 @@
},
"main": "./lib/node/index.js",
"engines": {
- "node": ">= 0.12"
+ "node": ">= 4.0"
}
}
diff --git a/test/basic.js b/test/basic.js
index 4c32fe037..3142f6fb3 100644
--- a/test/basic.js
+++ b/test/basic.js
@@ -360,7 +360,7 @@ describe('request', function(){
.accept('xml')
.end(function(err, res){
try {
- res.header['accept'].should.equal('application/xml');
+ res.header['accept'].should.equal('text/xml');
done();
} catch(e) { done(e); }
});
diff --git a/test/node/fixtures/passcert.pfx b/test/node/fixtures/passcert.pfx
new file mode 100644
index 000000000..01ffb905c
Binary files /dev/null and b/test/node/fixtures/passcert.pfx differ
diff --git a/test/node/https.js b/test/node/https.js
index 3d3a74983..7afe8661e 100644
--- a/test/node/https.js
+++ b/test/node/https.js
@@ -10,6 +10,7 @@ var request = require('../..'),
key = fs.readFileSync(__dirname + '/fixtures/key.pem'),
pfx = fs.readFileSync(__dirname + '/fixtures/cert.pfx'),
cert = fs.readFileSync(__dirname + '/fixtures/cert.pem'),
+ passpfx = fs.readFileSync(__dirname + '/fixtures/passcert.pfx'),
server;
@@ -77,7 +78,6 @@ describe('https', function(){
})
})
-
describe('client certificates', function() {
before(function listen(done) {
server = https.createServer({
@@ -120,6 +120,19 @@ describe('https', function(){
done();
})
})
+ it('should give a good response with client pfx with passphrase', function(done){
+ request
+ .get(testEndpoint)
+ .pfx({
+ pfx: passpfx,
+ passphrase: 'test'
+ })
+ .end(function(err, res){
+ assert(res.ok);
+ assert.strictEqual('Safe and secure!', res.text);
+ done();
+ })
+ })
})
describe('.agent', function () {
diff --git a/test/request.js b/test/request.js
index 6f3a691aa..06a4af603 100644
--- a/test/request.js
+++ b/test/request.js
@@ -295,7 +295,7 @@ it('request .accept() with xml', function(next){
.accept('xml')
.end(function(err, res){
try {
- assert.equal('application/xml', res.text, res.text);
+ assert.equal('text/xml', res.text, res.text);
next();
} catch(e) { next(e); }
});
@@ -656,6 +656,26 @@ it('GET querystring with strings and objects', function(next){
});
});
+it('GET shorthand payload goes to querystring', function(next){
+ request
+ .get(uri + '/querystring', {foo: 'FOO', bar: 'BAR'}, function(err, res){
+ try {
+ assert.deepEqual(res.body, { foo: 'FOO', bar: 'BAR' });
+ next();
+ } catch(e) { next(e); }
+ });
+});
+
+it('HEAD shorthand payload goes to querystring', function(next){
+ request
+ .head(uri + '/querystring-in-header', {foo: 'FOO', bar: 'BAR'}, function(err, res){
+ try {
+ assert.deepEqual(JSON.parse(res.headers.query), { foo: 'FOO', bar: 'BAR' });
+ next();
+ } catch(e) { next(e); }
+ });
+});
+
it('request(method, url)', function(next){
request('GET', uri + '/foo').end(function(err, res){
try {
diff --git a/test/support/server.js b/test/support/server.js
index 5c49cf4f3..fbe6d0d65 100644
--- a/test/support/server.js
+++ b/test/support/server.js
@@ -227,6 +227,11 @@ app.get('/querystring', function(req, res){
res.send(req.query);
});
+app.get('/querystring-in-header', function(req, res){
+ res.set('query', JSON.stringify(req.query));
+ res.send();
+});
+
app.get('/echo-header/:field', function(req, res){
res.send(req.headers[req.params.field]);
});