Sindre Sorhus 10 years ago
parent
commit
6bde68ea79
  1. 7
      index.js
  2. 5
      readme.md
  3. 4
      test/server.js
  4. 6
      test/test-headers.js
  5. 4
      test/test-https.js
  6. 1
      test/test-post.js

7
index.js

@ -60,12 +60,10 @@ function got(url, opts, cb) {
}
opts.method = opts.method || 'GET';
// returns a proxy stream to the response
// if no callback has been provided
if (!cb) {
proxy = duplexify();
// forward errors on the stream
cb = function (err) {
proxy.emit('error', err);
@ -114,10 +112,9 @@ function got(url, opts, cb) {
if (proxy) {
proxy.emit('response', res);
}
// auto-redirect only for GET and HEAD methods
if (statuses.redirect[statusCode] && 'location' in res.headers && (opts.method === 'GET' || opts.method === 'HEAD')) {
res.resume(); // Discard response
res.resume(); // discard response
if (++redirectCount > 10) {
cb(new GotError('Redirected 10 times. Aborting.'), undefined, res);
@ -156,9 +153,9 @@ function got(url, opts, cb) {
cb(err, data, response);
});
return;
}
// pipe the response to the proxy if in proxy mode
if (proxy) {
proxy.setReadable(res);

5
readme.md

@ -112,16 +112,15 @@ The data you requested.
The [response object](http://nodejs.org/api/http.html#http_http_incomingmessage).
When in stream mode, you can listen for events:
##### .on('response', response)
`response` event to get the response object.
##### .on('redirect', response, nextOpts)
##### .on('redirect', response, nextOptions)
`redirect` event to get the response object of redirect. Second argument is options for next request to redirect location.
`redirect` event to get the response object of redirect. Second argument is options for next request to the redirect location.
###### response

4
test/server.js

@ -6,8 +6,9 @@ exports.host = 'localhost';
exports.port = 6767;
exports.portSSL = 16167;
exports.createServer = function (port) {
exports.createServer = function (port) {
var host = exports.host;
port = port || exports.port;
var s = http.createServer(function (req, resp) {
@ -24,6 +25,7 @@ exports.createServer = function (port) {
exports.createSSLServer = function (port, opts) {
var host = exports.host;
port = port || exports.portSSL;
var s = https.createServer(opts, function (req, resp) {

6
test/test-headers.js

@ -17,6 +17,7 @@ tape('setup', function (t) {
tape('send user-agent header by default', function (t) {
got(s.url, function (err, data) {
var headers = JSON.parse(data);
t.equal(headers['user-agent'], 'https://github.com/sindresorhus/got');
t.end();
});
@ -25,6 +26,7 @@ tape('send user-agent header by default', function (t) {
tape('send accept-encoding header by default', function (t) {
got(s.url, function (err, data) {
var headers = JSON.parse(data);
t.equal(headers['accept-encoding'], 'gzip,deflate');
t.end();
});
@ -33,14 +35,16 @@ tape('send accept-encoding header by default', function (t) {
tape('send host header by default', function (t) {
got(s.url, function (err, data) {
var headers = JSON.parse(data);
t.equal(headers.host, 'localhost:' + s.port);
t.end();
});
});
tape('transform headers names to lowercase', function (t) {
got(s.url, {headers:{'USER-AGENT': 'test'}}, function (err, data) {
got(s.url, {headers: {'USER-AGENT': 'test'}}, function (err, data) {
var headers = JSON.parse(data);
t.equal(headers['user-agent'], 'test');
t.end();
});

4
test/test-https.js

@ -12,8 +12,8 @@ var caRootCert;
tape('root pem', function (t) {
pem.createCertificate({
days:1,
selfSigned:true
days: 1,
selfSigned: true
}, function (err, keys) {
caRootKey = keys.serviceKey;
caRootCert = keys.certificate;

1
test/test-post.js

@ -28,6 +28,7 @@ tape('GET can have body', function (t) {
t.plan(3);
var stream = from2Array(['wow']);
stream.on('end', function () {
t.ok(true); // Ensure, that stream was dumped
});

Loading…
Cancel
Save