Skip to content
Browse files

Merge branch 'master' of github.com:mikeal/request

  • Loading branch information...
2 parents 1f21b17 + 07135d5 commit 827e950500746eb9d3a3fa6f174416b194c9dedf @mikeal mikeal committed Aug 29, 2012
Showing with 95 additions and 8 deletions.
  1. +25 −8 main.js
  2. +5 −0 tests/test-body.js
  3. +30 −0 tests/test-follow-all-303.js
  4. +35 −0 tests/test-follow-all.js
View
33 main.js
@@ -224,6 +224,18 @@ Request.prototype.init = function (options) {
self.emit('error', error)
}
+ self._parserErrorHandler = function (error) {
+ if (this.res) {
+ if (this.res.request) {
+ this.res.request.emit('error', error)
+ } else {
+ this.res.emit('error', error)
+ }
+ } else {
+ this._httpMessage.emit('error', error)
+ }
+ }
+
if (options.form) {
self.form(options.form)
}
@@ -486,25 +498,28 @@ Request.prototype.getAgent = function () {
Request.prototype.start = function () {
var self = this
-
+
if (self._aborted) return
-
+
self._started = true
self.method = self.method || 'GET'
self.href = self.uri.href
if (log) log('%method %href', self)
-
+
if (self.src && self.src.stat && self.src.stat.size) {
self.headers['content-length'] = self.src.stat.size
}
if (self._aws) {
self.aws(self._aws, true)
}
-
+
self.req = self.httpModule.request(self, function (response) {
+ if (response.connection.listeners('error').indexOf(self._parserErrorHandler) === -1) {
+ response.connection.once('error', self._parserErrorHandler)
+ }
if (self._aborted) return
if (self._paused) response.pause()
-
+
self.response = response
response.request = self
response.toJSON = toJSON
@@ -522,7 +537,7 @@ Request.prototype.start = function () {
clearTimeout(self.timeoutTimer)
self.timeoutTimer = null
}
-
+
var addCookie = function (cookie) {
if (self._jar) self._jar.add(new Cookie(cookie))
else cookieJar.add(new Cookie(cookie))
@@ -624,7 +639,7 @@ Request.prototype.start = function () {
if (self.encoding === null) {
response.body = body
} else {
- response.body = body.toString()
+ response.body = body.toString(self.encoding)
}
} else if (buffer.length) {
response.body = buffer.join('')
@@ -668,7 +683,9 @@ Request.prototype.start = function () {
self.req.on('drain', function() {
self.emit('drain')
})
-
+ self.on('end', function() {
+ self.req.connection.removeListener('error', self._parserErrorHandler)
+ })
self.emit('request', self.req)
}
View
5 tests/test-body.js
@@ -30,6 +30,11 @@ var tests =
, encoding: null
, expectBody: new Buffer("TESTING!")
}
+ , testGetEncoding :
+ { resp : server.createGetResponse(new Buffer('efa3bfcea9e29883', 'hex'))
+ , encoding: 'hex'
+ , expectBody: "efa3bfcea9e29883"
+ }
, testGetJSON :
{ resp : server.createGetResponse('{"test":true}', 'application/json')
, json : true
View
30 tests/test-follow-all-303.js
@@ -0,0 +1,30 @@
+var request = require('request');
+var http = require('http');
+var requests = 0;
+var assert = require('assert');
+
+var server = http.createServer(function (req, res) {
+ console.error(req.method, req.url);
+ requests ++;
+
+ if (req.method === 'POST') {
+ console.error('send 303');
+ res.setHeader('location', req.url);
+ res.statusCode = 303;
+ res.end('try again, i guess\n');
+ } else {
+ console.error('send 200')
+ res.end('ok: ' + requests);
+ }
+});
+server.listen(6767);
+
+request.post({ url: 'http://localhost:6767/foo',
+ followAllRedirects: true,
+ form: { foo: 'bar' } }, function (er, req, body) {
+ if (er) throw er;
+ assert.equal(body, 'ok: 2');
+ assert.equal(requests, 2);
+ console.error('ok - ' + process.version);
+ server.close();
+});
View
35 tests/test-follow-all.js
@@ -0,0 +1,35 @@
+var request = require('request');
+var http = require('http');
+var requests = 0;
+var assert = require('assert');
+
+var server = http.createServer(function (req, res) {
+ requests ++;
+
+ // redirect everything 3 times, no matter what.
+ var c = req.headers.cookie;
+
+ if (!c) c = 0;
+ else c = +c.split('=')[1] || 0;
+
+ if (c > 3) {
+ res.end('ok: '+requests);
+ return;
+ }
+
+ res.setHeader('set-cookie', 'c=' + (c + 1));
+ res.setHeader('location', req.url);
+ res.statusCode = 302;
+ res.end('try again, i guess\n');
+});
+server.listen(6767);
+
+request.post({ url: 'http://localhost:6767/foo',
+ followAllRedirects: true,
+ form: { foo: 'bar' } }, function (er, req, body) {
+ if (er) throw er;
+ assert.equal(body, 'ok: 5');
+ assert.equal(requests, 5);
+ console.error('ok - ' + process.version);
+ server.close();
+});

0 comments on commit 827e950

Please sign in to comment.
Something went wrong with that request. Please try again.