123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- var concat = require('concat-stream')
- var http = require('http')
- var get = require('../')
- var selfSignedHttps = require('self-signed-https')
- var str = require('string-to-stream')
- var test = require('tape')
- var zlib = require('zlib')
- // Allow self-signed certs
- process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
- test('simple get', function (t) {
- t.plan(4)
- var server = http.createServer(function (req, res) {
- t.equal(req.url, '/path')
- res.statusCode = 200
- res.end('response')
- })
- server.listen(0, function () {
- var port = server.address().port
- get('http://localhost:' + port + '/path', function (err, res) {
- t.error(err)
- t.equal(res.statusCode, 200)
- res.pipe(concat(function (data) {
- t.equal(data.toString(), 'response')
- server.close()
- }))
- })
- })
- })
- test('follow redirects (up to 10)', function (t) {
- t.plan(13)
- var num = 1
- var server = http.createServer(function (req, res) {
- t.equal(req.url, '/' + num, 'visited /' + num)
- num += 1
- if (num <= 10) {
- 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 + '/1', function (err, res) {
- t.error(err)
- t.equal(res.statusCode, 200)
- res.pipe(concat(function (data) {
- 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, '/1', 'visited /1')
- res.statusCode = 301
- res.setHeader('Location', '/2')
- res.end()
- })
- server.listen(0, function () {
- var port = server.address().port
- get({
- url: 'http://localhost:' + port + '/1',
- maxRedirects: 0
- }, function (err) {
- t.ok(err instanceof Error, 'got error')
- server.close()
- })
- })
- })
- test('follow redirects (11 is too many)', function (t) {
- t.plan(11)
- var num = 1
- var server = http.createServer(function (req, res) {
- t.equal(req.url, '/' + num, 'visited /' + num)
- num += 1
- res.statusCode = 301
- res.setHeader('Location', '/' + num)
- res.end()
- })
- server.listen(0, function () {
- var port = server.address().port
- get('http://localhost:' + port + '/1', 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(3)
- 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
- res.pipe(concat(function (data) {
- t.equal(data.toString(), 'response')
- server.close()
- }))
- })
- })
- })
- test('deflate response', function (t) {
- t.plan(3)
- 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
- res.pipe(concat(function (data) {
- t.equal(data.toString(), 'response')
- server.close()
- }))
- })
- })
- })
- test('https', function (t) {
- t.plan(4)
- var server = selfSignedHttps(function (req, res) {
- t.equal(req.url, '/path')
- res.statusCode = 200
- res.end('response')
- })
- server.listen(0, function () {
- var port = server.address().port
- get('https://localhost:' + port + '/path', function (err, res) {
- t.error(err)
- t.equal(res.statusCode, 200)
- res.pipe(concat(function (data) {
- t.equal(data.toString(), 'response')
- server.close()
- }))
- })
- })
- })
- test('redirect https to http', function (t) {
- t.plan(5)
- 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('https://localhost:' + httpsPort + '/path1', function (err, res) {
- t.error(err)
- t.equal(res.statusCode, 200)
- res.pipe(concat(function (data) {
- t.equal(data.toString(), 'response')
- httpsServer.close()
- httpServer.close()
- }))
- })
- })
- })
- })
- test('redirect http to https', function (t) {
- t.plan(5)
- 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('http://localhost:' + httpPort + '/path1', function (err, res) {
- t.error(err)
- t.equal(res.statusCode, 200)
- res.pipe(concat(function (data) {
- t.equal(data.toString(), 'response')
- httpsServer.close()
- httpServer.close()
- }))
- })
- })
- })
- })
- test('post (text body)', function (t) {
- t.plan(4)
- 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)
- res.pipe(concat(function (data) {
- t.equal(data.toString(), 'this is the body')
- server.close()
- }))
- })
- })
- })
- test('post (buffer body)', function (t) {
- t.plan(4)
- 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: new Buffer('this is the body')
- }
- get.post(opts, function (err, res) {
- t.error(err)
- t.equal(res.statusCode, 200)
- res.pipe(concat(function (data) {
- t.equal(data.toString(), 'this is the body')
- 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, data, res) {
- 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')
- })
- })
- })
|