simple-get
Advanced tools
Comparing version 2.7.0 to 2.7.1
@@ -45,2 +45,3 @@ module.exports = simpleGet | ||
opts.url = res.headers.location | ||
delete opts.headers.host | ||
res.resume() // Discard response | ||
@@ -47,0 +48,0 @@ |
{ | ||
"name": "simple-get", | ||
"description": "Simplest way to make http get requests. Supports HTTPS, redirects, gzip/deflate, streams in < 100 lines.", | ||
"version": "2.7.0", | ||
"version": "2.7.1", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Feross Aboukhadijeh", |
var concat = require('simple-concat') | ||
var get = require('../') | ||
var http = require('http') | ||
var querystring = require('querystring') | ||
var selfSignedHttps = require('self-signed-https') | ||
var str = require('string-to-stream') | ||
var test = require('tape') | ||
var zlib = require('zlib') | ||
@@ -33,176 +30,2 @@ test('simple get', function (t) { | ||
test('basic auth', function (t) { | ||
t.plan(5) | ||
var server = http.createServer(function (req, res) { | ||
t.equal(req.headers.authorization, 'Basic Zm9vOmJhcg==') | ||
res.statusCode = 200 | ||
res.end('response') | ||
}) | ||
server.listen(0, function () { | ||
var port = server.address().port | ||
get('http://foo:bar@localhost:' + port, function (err, res) { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
concat(res, function (err, data) { | ||
t.error(err) | ||
t.equal(data.toString(), 'response') | ||
server.close() | ||
}) | ||
}) | ||
}) | ||
}) | ||
test('follow redirects (up to 10)', function (t) { | ||
t.plan(15) | ||
var num = 0 | ||
var server = http.createServer(function (req, res) { | ||
t.equal(req.url, '/' + num, 'visited /' + num) | ||
if (num < 10) { | ||
num += 1 | ||
res.statusCode = 301 | ||
res.setHeader('Location', '/' + num) | ||
res.end() | ||
} else { | ||
res.statusCode = 200 | ||
res.end('response') | ||
} | ||
}) | ||
server.listen(0, function () { | ||
var port = server.address().port | ||
get('http://localhost:' + port + '/0', function (err, res) { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
concat(res, function (err, data) { | ||
t.error(err) | ||
t.equal(data.toString(), 'response') | ||
server.close() | ||
}) | ||
}) | ||
}) | ||
}) | ||
test('do not follow redirects', function (t) { | ||
t.plan(2) | ||
var server = http.createServer(function (req, res) { | ||
t.equal(req.url, '/0', 'visited /0') | ||
res.statusCode = 301 | ||
res.setHeader('Location', '/1') | ||
res.end() | ||
}) | ||
server.listen(0, function () { | ||
var port = server.address().port | ||
get({ | ||
url: 'http://localhost:' + port + '/0', | ||
maxRedirects: 0 | ||
}, function (err) { | ||
t.ok(err instanceof Error, 'got error') | ||
server.close() | ||
}) | ||
}) | ||
}) | ||
test('follow redirects (11 is too many)', function (t) { | ||
t.plan(12) | ||
var num = 0 | ||
var server = http.createServer(function (req, res) { | ||
t.equal(req.url, '/' + num, 'visited /' + num) | ||
if (num < 11) { | ||
num += 1 | ||
res.statusCode = 301 | ||
res.setHeader('Location', '/' + num) | ||
res.end() | ||
} else { | ||
t.fail('no request to /11 should be made, should error first') | ||
} | ||
}) | ||
server.listen(0, function () { | ||
var port = server.address().port | ||
get('http://localhost:' + port + '/0', function (err) { | ||
t.ok(err instanceof Error, 'got error') | ||
server.close() | ||
}) | ||
}) | ||
}) | ||
test('custom headers', function (t) { | ||
t.plan(2) | ||
var server = http.createServer(function (req, res) { | ||
t.equal(req.headers['custom-header'], 'custom-value') | ||
res.statusCode = 200 | ||
res.end('response') | ||
}) | ||
server.listen(0, function () { | ||
var port = server.address().port | ||
get({ | ||
url: 'http://localhost:' + port, | ||
headers: { | ||
'custom-header': 'custom-value' | ||
} | ||
}, function (err, res) { | ||
t.error(err) | ||
res.resume() | ||
server.close() | ||
}) | ||
}) | ||
}) | ||
test('gzip response', function (t) { | ||
t.plan(4) | ||
var server = http.createServer(function (req, res) { | ||
res.statusCode = 200 | ||
res.setHeader('content-encoding', 'gzip') | ||
str('response').pipe(zlib.createGzip()).pipe(res) | ||
}) | ||
server.listen(0, function () { | ||
var port = server.address().port | ||
get('http://localhost:' + port, function (err, res) { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) // statusCode still works on gunzip stream | ||
concat(res, function (err, data) { | ||
t.error(err) | ||
t.equal(data.toString(), 'response') | ||
server.close() | ||
}) | ||
}) | ||
}) | ||
}) | ||
test('deflate response', function (t) { | ||
t.plan(4) | ||
var server = http.createServer(function (req, res) { | ||
res.statusCode = 200 | ||
res.setHeader('content-encoding', 'deflate') | ||
str('response').pipe(zlib.createDeflate()).pipe(res) | ||
}) | ||
server.listen(0, function () { | ||
var port = server.address().port | ||
get('http://localhost:' + port, function (err, res) { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) // statusCode still works on inflate stream | ||
concat(res, function (err, data) { | ||
t.error(err) | ||
t.equal(data.toString(), 'response') | ||
server.close() | ||
}) | ||
}) | ||
}) | ||
}) | ||
test('https', function (t) { | ||
@@ -234,258 +57,2 @@ t.plan(5) | ||
test('redirect https to http', function (t) { | ||
t.plan(6) | ||
var httpPort = null | ||
var httpsPort = null | ||
var httpsServer = selfSignedHttps(function (req, res) { | ||
t.equal(req.url, '/path1') | ||
res.statusCode = 301 | ||
res.setHeader('Location', 'http://localhost:' + httpPort + '/path2') | ||
res.end() | ||
}) | ||
var httpServer = http.createServer(function (req, res) { | ||
t.equal(req.url, '/path2') | ||
res.statusCode = 200 | ||
res.end('response') | ||
}) | ||
httpsServer.listen(0, function () { | ||
httpsPort = httpsServer.address().port | ||
httpServer.listen(0, function () { | ||
httpPort = httpServer.address().port | ||
get({ | ||
url: 'https://localhost:' + httpsPort + '/path1', | ||
rejectUnauthorized: false | ||
}, function (err, res) { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
concat(res, function (err, data) { | ||
t.error(err) | ||
t.equal(data.toString(), 'response') | ||
httpsServer.close() | ||
httpServer.close() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
test('redirect http to https', function (t) { | ||
t.plan(6) | ||
var httpsPort = null | ||
var httpPort = null | ||
var httpServer = http.createServer(function (req, res) { | ||
t.equal(req.url, '/path1') | ||
res.statusCode = 301 | ||
res.setHeader('Location', 'https://localhost:' + httpsPort + '/path2') | ||
res.end() | ||
}) | ||
var httpsServer = selfSignedHttps(function (req, res) { | ||
t.equal(req.url, '/path2') | ||
res.statusCode = 200 | ||
res.end('response') | ||
}) | ||
httpServer.listen(0, function () { | ||
httpPort = httpServer.address().port | ||
httpsServer.listen(0, function () { | ||
httpsPort = httpsServer.address().port | ||
get({ | ||
url: 'http://localhost:' + httpPort + '/path1', | ||
rejectUnauthorized: false | ||
}, function (err, res) { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
concat(res, function (err, data) { | ||
t.error(err) | ||
t.equal(data.toString(), 'response') | ||
httpsServer.close() | ||
httpServer.close() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
test('post (text body)', function (t) { | ||
t.plan(5) | ||
var server = http.createServer(function (req, res) { | ||
t.equal(req.method, 'POST') | ||
res.statusCode = 200 | ||
req.pipe(res) | ||
}) | ||
server.listen(0, function () { | ||
var port = server.address().port | ||
var opts = { | ||
url: 'http://localhost:' + port, | ||
body: 'this is the body' | ||
} | ||
get.post(opts, function (err, res) { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
concat(res, function (err, data) { | ||
t.error(err) | ||
t.equal(data.toString(), 'this is the body') | ||
server.close() | ||
}) | ||
}) | ||
}) | ||
}) | ||
test('post (utf-8 text body)', function (t) { | ||
t.plan(5) | ||
var server = http.createServer(function (req, res) { | ||
t.equal(req.method, 'POST') | ||
res.statusCode = 200 | ||
req.pipe(res) | ||
}) | ||
server.listen(0, function () { | ||
var port = server.address().port | ||
var opts = { | ||
url: 'http://localhost:' + port, | ||
body: 'jedan dva tri četiri' | ||
} | ||
get.post(opts, function (err, res) { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
concat(res, function (err, data) { | ||
t.error(err) | ||
t.equal(data.toString(), 'jedan dva tri četiri') | ||
server.close() | ||
}) | ||
}) | ||
}) | ||
}) | ||
test('post (buffer body)', function (t) { | ||
t.plan(5) | ||
var server = http.createServer(function (req, res) { | ||
t.equal(req.method, 'POST') | ||
res.statusCode = 200 | ||
req.pipe(res) | ||
}) | ||
server.listen(0, function () { | ||
var port = server.address().port | ||
var opts = { | ||
url: 'http://localhost:' + port, | ||
body: Buffer.from('this is the body') | ||
} | ||
get.post(opts, function (err, res) { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
concat(res, function (err, data) { | ||
t.error(err) | ||
t.equal(data.toString(), 'this is the body') | ||
server.close() | ||
}) | ||
}) | ||
}) | ||
}) | ||
test('post (stream body)', function (t) { | ||
t.plan(6) | ||
var server = http.createServer(function (req, res) { | ||
t.equal(req.method, 'POST') | ||
res.statusCode = 200 | ||
t.notOk(req.headers['content-length']) | ||
req.pipe(res) | ||
}) | ||
server.listen(0, function () { | ||
var port = server.address().port | ||
var opts = { | ||
url: 'http://localhost:' + port, | ||
body: str('this is the body') | ||
} | ||
get.post(opts, function (err, res) { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
concat(res, function (err, data) { | ||
t.error(err) | ||
t.equal(data.toString(), 'this is the body') | ||
server.close() | ||
}) | ||
}) | ||
}) | ||
}) | ||
test('get.concat (post, stream body, and json option)', function (t) { | ||
t.plan(4) | ||
var server = http.createServer(function (req, res) { | ||
res.statusCode = 200 | ||
req.pipe(res) | ||
}) | ||
server.listen(0, function () { | ||
var port = server.address().port | ||
var opts = { | ||
url: 'http://localhost:' + port, | ||
body: str('{"a": "b"}'), | ||
method: 'POST', | ||
json: true | ||
} | ||
get.concat(opts, function (err, res, data) { | ||
t.error(err) | ||
t.equal(typeof data, 'object') | ||
t.deepEqual(Object.keys(data), ['a']) | ||
t.equal(data.a, 'b') | ||
server.close() | ||
}) | ||
}) | ||
}) | ||
test('get.concat', function (t) { | ||
t.plan(4) | ||
var server = http.createServer(function (req, res) { | ||
res.statusCode = 200 | ||
res.end('blah blah blah') | ||
}) | ||
server.listen(0, function () { | ||
var port = server.address().port | ||
get.concat('http://localhost:' + port, function (err, res, data) { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
t.ok(Buffer.isBuffer(data), '`data` is type buffer') | ||
t.equal(data.toString(), 'blah blah blah') | ||
server.close() | ||
}) | ||
}) | ||
}) | ||
test('access `req` object', function (t) { | ||
t.plan(2) | ||
var server = http.createServer(function (req, res) { | ||
res.statusCode = 200 | ||
res.end('response') | ||
}) | ||
server.listen(0, function () { | ||
var port = server.address().port | ||
var req = get('http://localhost:' + port, function (err, res) { | ||
t.error(err) | ||
res.resume() // discard data | ||
server.close() | ||
}) | ||
req.on('socket', function () { | ||
t.pass('got `socket` event') | ||
}) | ||
}) | ||
}) | ||
test('simple get json', function (t) { | ||
@@ -519,131 +86,2 @@ t.plan(6) | ||
test('get.concat json', function (t) { | ||
t.plan(3) | ||
var server = http.createServer(function (req, res) { | ||
res.statusCode = 200 | ||
res.end('{"message":"response"}') | ||
}) | ||
server.listen(0, function () { | ||
var port = server.address().port | ||
var opts = { | ||
url: 'http://localhost:' + port + '/path', | ||
json: true | ||
} | ||
get.concat(opts, function (err, res, data) { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
t.equal(data.message, 'response') | ||
server.close() | ||
}) | ||
}) | ||
}) | ||
test('get.concat json error', function (t) { | ||
t.plan(1) | ||
var server = http.createServer(function (req, res) { | ||
res.statusCode = 500 | ||
res.end('not json') | ||
}) | ||
server.listen(0, function () { | ||
var port = server.address().port | ||
var opts = { | ||
url: 'http://localhost:' + port + '/path', | ||
json: true | ||
} | ||
get.concat(opts, function (err, res, data) { | ||
t.ok(err instanceof Error) | ||
server.close() | ||
}) | ||
}) | ||
}) | ||
test('post (json body)', function (t) { | ||
t.plan(5) | ||
var server = http.createServer(function (req, res) { | ||
t.equal(req.method, 'POST') | ||
t.equal(req.headers['content-type'], 'application/json') | ||
res.statusCode = 200 | ||
req.pipe(res) | ||
}) | ||
server.listen(0, function () { | ||
var port = server.address().port | ||
var opts = { | ||
method: 'POST', | ||
url: 'http://localhost:' + port, | ||
body: { | ||
message: 'this is the body' | ||
}, | ||
json: true | ||
} | ||
get.concat(opts, function (err, res, data) { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
t.equal(data.message, 'this is the body') | ||
server.close() | ||
}) | ||
}) | ||
}) | ||
test('post (form, object)', function (t) { | ||
t.plan(5) | ||
var formData = { | ||
foo: 'bar' | ||
} | ||
var server = http.createServer(function (req, res) { | ||
t.equal(req.method, 'POST') | ||
t.equal(req.headers['content-type'], 'application/x-www-form-urlencoded') | ||
res.statusCode = 200 | ||
req.pipe(res) | ||
}) | ||
server.listen(0, function () { | ||
var port = server.address().port | ||
var opts = { | ||
method: 'POST', | ||
url: 'http://localhost:' + port, | ||
form: formData | ||
} | ||
get.concat(opts, function (err, res, data) { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
t.deepEqual(querystring.parse(data.toString()), formData) | ||
server.close() | ||
}) | ||
}) | ||
}) | ||
test('post (form, querystring)', function (t) { | ||
t.plan(5) | ||
var formData = 'foo=bar' | ||
var server = http.createServer(function (req, res) { | ||
t.equal(req.method, 'POST') | ||
t.equal(req.headers['content-type'], 'application/x-www-form-urlencoded') | ||
res.statusCode = 200 | ||
req.pipe(res) | ||
}) | ||
server.listen(0, function () { | ||
var port = server.address().port | ||
var opts = { | ||
method: 'POST', | ||
url: 'http://localhost:' + port, | ||
form: formData | ||
} | ||
get.concat(opts, function (err, res, data) { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
t.equal(data.toString(), formData) | ||
server.close() | ||
}) | ||
}) | ||
}) | ||
test('HEAD request', function (t) { | ||
@@ -650,0 +88,0 @@ t.plan(3) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32779
12
806
10