Browse Source

minor code style improvements

http2
Sindre Sorhus 10 years ago
parent
commit
41d147baaf
  1. 10
      index.js
  2. 7
      test/test-post.js
  3. 2
      test/test-redirects.js

10
index.js

@ -65,10 +65,12 @@ 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, data, response) {
proxy.emit('error', err, data, response);
@ -101,7 +103,7 @@ function got(url, opts, cb) {
typeof opts.passphrase !== 'undefined' ||
typeof opts.pfx !== 'undefined' ||
typeof opts.rejectUnauthorized !== 'undefined')) {
arg.agent = new (infinityAgent.https.Agent)(opts);
arg.agent = new infinityAgent.https.Agent(opts);
}
}
@ -117,9 +119,11 @@ function got(url, opts, cb) {
if (proxy) {
proxy.emit('response', res);
}
// auto-redirect only for GET and HEAD methods
if (isRedirect(statusCode) && 'location' in res.headers && (opts.method === 'GET' || opts.method === 'HEAD')) {
res.resume(); // discard response
// discard response
res.resume();
if (++redirectCount > 10) {
cb(new GotError('Redirected 10 times. Aborting.'), undefined, res);
@ -161,6 +165,7 @@ function got(url, opts, cb) {
return;
}
// pipe the response to the proxy if in proxy mode
if (proxy) {
proxy.setReadable(res);
@ -221,6 +226,7 @@ function got(url, opts, cb) {
}
get(url, opts, cb);
return proxy;
}

7
test/test-post.js

@ -30,7 +30,8 @@ test('GET can have body', function (t) {
var stream = from2Array(['wow']);
stream.on('end', function () {
t.ok(true); // Ensure, that stream was dumped
// ensure that stream was dumped
t.ok(true);
});
got.get(s.url + '/method', {body: stream}, function (err, data, res) {
@ -83,7 +84,9 @@ test('throws on write to stream with body specified', function (t) {
t.throws(function () {
got(s.url, {body: 'wow'}).write('wow');
});
setTimeout(t.end.bind(t), 10); // wait for request to end
// wait for request to end
setTimeout(t.end.bind(t), 10);
});
test('cleanup', function (t) {

2
test/test-redirects.js

@ -83,7 +83,7 @@ test('host+path in options are not breaking redirects', function (t) {
});
test('redirect only GET and HEAD requests', function (t) {
got(s.url + '/relative', {body: 'wow'}, function (err, data) {
got(s.url + '/relative', {body: 'wow'}, function (err) {
t.equal(err.message, 'POST http://localhost:6767/relative response code is 302 (Found)');
t.equal(err.code, 302);
t.end();

Loading…
Cancel
Save