Skip to content

Commit

Permalink
Merge pull request request#290 from isaacs/master
Browse files Browse the repository at this point in the history
A test for request#289
  • Loading branch information
mikeal committed Aug 10, 2012
2 parents 6297612 + 7adc5a2 commit 8d1f58e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion main.js
Expand Up @@ -618,7 +618,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('')
Expand Down
5 changes: 5 additions & 0 deletions tests/test-body.js
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions 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();
});
35 changes: 35 additions & 0 deletions 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 8d1f58e

Please sign in to comment.