diff --git a/test/auth.js b/test/auth.js index 676b136..2ea9941 100644 --- a/test/auth.js +++ b/test/auth.js @@ -1,19 +1,19 @@ -var concat = require('simple-concat') -var get = require('../') -var http = require('http') -var test = require('tape') +const concat = require('simple-concat') +const get = require('../') +const http = require('http') +const test = require('tape') test('basic auth', function (t) { t.plan(5) - var server = http.createServer(function (req, res) { + const 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 + const port = server.address().port get('http://foo:bar@localhost:' + port, function (err, res) { t.error(err) t.equal(res.statusCode, 200) diff --git a/test/basic.js b/test/basic.js index 07846ee..3188482 100644 --- a/test/basic.js +++ b/test/basic.js @@ -1,20 +1,20 @@ -var concat = require('simple-concat') -var get = require('../') -var http = require('http') -var selfSignedHttps = require('self-signed-https') -var test = require('tape') +const concat = require('simple-concat') +const get = require('../') +const http = require('http') +const selfSignedHttps = require('self-signed-https') +const test = require('tape') test('simple get', function (t) { t.plan(5) - var server = http.createServer(function (req, res) { + const 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 + const port = server.address().port get('http://localhost:' + port + '/path', function (err, res) { t.error(err) t.equal(res.statusCode, 200) @@ -30,14 +30,14 @@ test('simple get', function (t) { test('https', function (t) { t.plan(5) - var server = selfSignedHttps(function (req, res) { + const 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 + const port = server.address().port get({ url: 'https://localhost:' + port + '/path', rejectUnauthorized: false @@ -56,7 +56,7 @@ test('https', function (t) { test('simple get json', function (t) { t.plan(6) - var server = http.createServer(function (req, res) { + const server = http.createServer(function (req, res) { t.equal(req.url, '/path') t.equal(req.headers.accept, 'application/json') res.statusCode = 200 @@ -64,8 +64,8 @@ test('simple get json', function (t) { }) server.listen(0, function () { - var port = server.address().port - var opts = { + const port = server.address().port + const opts = { url: 'http://localhost:' + port + '/path', json: true } @@ -84,7 +84,7 @@ test('simple get json', function (t) { test('HEAD request', function (t) { t.plan(3) - var server = http.createServer(function (req, res) { + const server = http.createServer(function (req, res) { t.equal(req.method, 'HEAD') // Taken from real-world response from HEAD request to GitHub.com res.setHeader('content-type', 'text/html; charset=utf-8') @@ -95,8 +95,8 @@ test('HEAD request', function (t) { }) server.listen(0, function () { - var port = server.address().port - var opts = { + const port = server.address().port + const opts = { method: 'HEAD', url: 'http://localhost:' + port } @@ -111,7 +111,7 @@ test('HEAD request', function (t) { test('timeout option', function (t) { t.plan(2) - var server = http.createServer(function (req, res) { + const server = http.createServer(function (req, res) { t.equal(req.url, '/path') setTimeout(function () { // response should not be sent - should timeout before it's sent @@ -120,7 +120,7 @@ test('timeout option', function (t) { }) server.listen(0, function () { - var port = server.address().port + const port = server.address().port get({ url: 'http://localhost:' + port + '/path', timeout: 1000 @@ -134,9 +134,9 @@ test('timeout option', function (t) { test('rewrite POST redirects to GET', function (t) { t.plan(8) - var redirected = false + let redirected = false - var server = http.createServer(function (req, res) { + const server = http.createServer(function (req, res) { if (redirected) { t.equal(req.url, '/getthis') t.equal(req.method, 'GET') @@ -153,8 +153,8 @@ test('rewrite POST redirects to GET', function (t) { }) server.listen(0, function () { - var port = server.address().port - var opts = { + const port = server.address().port + const opts = { method: 'POST', body: '123', url: 'http://localhost:' + port diff --git a/test/concat.js b/test/concat.js index 4a97065..2bc22c9 100644 --- a/test/concat.js +++ b/test/concat.js @@ -1,19 +1,19 @@ -var get = require('../') -var http = require('http') -var str = require('string-to-stream') -var test = require('tape') +const get = require('../') +const http = require('http') +const str = require('string-to-stream') +const test = require('tape') test('get.concat (post, stream body, and json option)', function (t) { t.plan(4) - var server = http.createServer(function (req, res) { + const server = http.createServer(function (req, res) { res.statusCode = 200 req.pipe(res) }) server.listen(0, function () { - var port = server.address().port - var opts = { + const port = server.address().port + const opts = { url: 'http://localhost:' + port, body: str('{"a": "b"}'), method: 'POST', @@ -31,13 +31,13 @@ test('get.concat (post, stream body, and json option)', function (t) { test('get.concat', function (t) { t.plan(4) - var server = http.createServer(function (req, res) { + const server = http.createServer(function (req, res) { res.statusCode = 200 res.end('blah blah blah') }) server.listen(0, function () { - var port = server.address().port + const port = server.address().port get.concat('http://localhost:' + port, function (err, res, data) { t.error(err) t.equal(res.statusCode, 200) @@ -50,14 +50,14 @@ test('get.concat', function (t) { test('get.concat json', function (t) { t.plan(3) - var server = http.createServer(function (req, res) { + const server = http.createServer(function (req, res) { res.statusCode = 200 res.end('{"message":"response"}') }) server.listen(0, function () { - var port = server.address().port - var opts = { + const port = server.address().port + const opts = { url: 'http://localhost:' + port + '/path', json: true } @@ -72,14 +72,14 @@ test('get.concat json', function (t) { test('get.concat json error', function (t) { t.plan(1) - var server = http.createServer(function (req, res) { + const server = http.createServer(function (req, res) { res.statusCode = 500 res.end('not json') }) server.listen(0, function () { - var port = server.address().port - var opts = { + const port = server.address().port + const opts = { url: 'http://localhost:' + port + '/path', json: true } diff --git a/test/headers.js b/test/headers.js index a22c8e8..ac51f81 100644 --- a/test/headers.js +++ b/test/headers.js @@ -1,21 +1,21 @@ -var concat = require('simple-concat') -var get = require('../') -var http = require('http') -var str = require('string-to-stream') -var test = require('tape') -var zlib = require('zlib') +const concat = require('simple-concat') +const get = require('../') +const http = require('http') +const str = require('string-to-stream') +const test = require('tape') +const zlib = require('zlib') test('custom headers', function (t) { t.plan(2) - var server = http.createServer(function (req, res) { + const 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 + const port = server.address().port get({ url: 'http://localhost:' + port, headers: { @@ -32,14 +32,14 @@ test('custom headers', function (t) { test('gzip response', function (t) { t.plan(4) - var server = http.createServer(function (req, res) { + const 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 + const 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 @@ -55,14 +55,14 @@ test('gzip response', function (t) { test('deflate response', function (t) { t.plan(4) - var server = http.createServer(function (req, res) { + const 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 + const 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 diff --git a/test/post.js b/test/post.js index a02c09e..b0ae806 100644 --- a/test/post.js +++ b/test/post.js @@ -1,22 +1,22 @@ -var concat = require('simple-concat') -var get = require('../') -var http = require('http') -var querystring = require('querystring') -var str = require('string-to-stream') -var test = require('tape') +const concat = require('simple-concat') +const get = require('../') +const http = require('http') +const querystring = require('querystring') +const str = require('string-to-stream') +const test = require('tape') test('post (text body)', function (t) { t.plan(5) - var server = http.createServer(function (req, res) { + const 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 = { + const port = server.address().port + const opts = { url: 'http://localhost:' + port, body: 'this is the body' } @@ -35,15 +35,15 @@ test('post (text body)', function (t) { test('post (utf-8 text body)', function (t) { t.plan(5) - var server = http.createServer(function (req, res) { + const 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 = { + const port = server.address().port + const opts = { url: 'http://localhost:' + port, body: 'jedan dva tri Ĩetiri' } @@ -62,15 +62,15 @@ test('post (utf-8 text body)', function (t) { test('post (buffer body)', function (t) { t.plan(5) - var server = http.createServer(function (req, res) { + const 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 = { + const port = server.address().port + const opts = { url: 'http://localhost:' + port, body: Buffer.from('this is the body') } @@ -89,7 +89,7 @@ test('post (buffer body)', function (t) { test('post (stream body)', function (t) { t.plan(6) - var server = http.createServer(function (req, res) { + const server = http.createServer(function (req, res) { t.equal(req.method, 'POST') res.statusCode = 200 t.notOk(req.headers['content-length']) @@ -97,8 +97,8 @@ test('post (stream body)', function (t) { }) server.listen(0, function () { - var port = server.address().port - var opts = { + const port = server.address().port + const opts = { url: 'http://localhost:' + port, body: str('this is the body') } @@ -117,7 +117,7 @@ test('post (stream body)', function (t) { test('post (json body)', function (t) { t.plan(5) - var server = http.createServer(function (req, res) { + const server = http.createServer(function (req, res) { t.equal(req.method, 'POST') t.equal(req.headers['content-type'], 'application/json') res.statusCode = 200 @@ -125,8 +125,8 @@ test('post (json body)', function (t) { }) server.listen(0, function () { - var port = server.address().port - var opts = { + const port = server.address().port + const opts = { method: 'POST', url: 'http://localhost:' + port, body: { @@ -146,10 +146,10 @@ test('post (json body)', function (t) { test('post (form, object)', function (t) { t.plan(5) - var formData = Object.create(null) + const formData = Object.create(null) formData.foo = 'bar' - var server = http.createServer(function (req, res) { + const 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 @@ -157,8 +157,8 @@ test('post (form, object)', function (t) { }) server.listen(0, function () { - var port = server.address().port - var opts = { + const port = server.address().port + const opts = { method: 'POST', url: 'http://localhost:' + port, form: formData @@ -175,9 +175,9 @@ test('post (form, object)', function (t) { test('post (form, querystring)', function (t) { t.plan(5) - var formData = 'foo=bar' + const formData = 'foo=bar' - var server = http.createServer(function (req, res) { + const 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 @@ -185,8 +185,8 @@ test('post (form, querystring)', function (t) { }) server.listen(0, function () { - var port = server.address().port - var opts = { + const port = server.address().port + const opts = { method: 'POST', url: 'http://localhost:' + port, form: formData diff --git a/test/redirect.js b/test/redirect.js index 26131a6..2f06edd 100644 --- a/test/redirect.js +++ b/test/redirect.js @@ -1,14 +1,14 @@ -var concat = require('simple-concat') -var get = require('../') -var http = require('http') -var selfSignedHttps = require('self-signed-https') -var test = require('tape') +const concat = require('simple-concat') +const get = require('../') +const http = require('http') +const selfSignedHttps = require('self-signed-https') +const test = require('tape') test('follow redirects (up to 10)', function (t) { t.plan(15) - var num = 0 - var server = http.createServer(function (req, res) { + let num = 0 + const server = http.createServer(function (req, res) { t.equal(req.url, '/' + num, 'visited /' + num) if (num < 10) { @@ -23,7 +23,7 @@ test('follow redirects (up to 10)', function (t) { }) server.listen(0, function () { - var port = server.address().port + const port = server.address().port get('http://localhost:' + port + '/0', function (err, res) { t.error(err) t.equal(res.statusCode, 200) @@ -39,7 +39,7 @@ test('follow redirects (up to 10)', function (t) { test('do not follow redirects', function (t) { t.plan(2) - var server = http.createServer(function (req, res) { + const server = http.createServer(function (req, res) { t.equal(req.url, '/0', 'visited /0') res.statusCode = 301 @@ -48,7 +48,7 @@ test('do not follow redirects', function (t) { }) server.listen(0, function () { - var port = server.address().port + const port = server.address().port get({ url: 'http://localhost:' + port + '/0', maxRedirects: 0 @@ -62,7 +62,7 @@ test('do not follow redirects', function (t) { test('do not follow redirects and do not error', function (t) { t.plan(4) - var server = http.createServer(function (req, res) { + const server = http.createServer(function (req, res) { t.equal(req.url, '/0', 'visited /0') res.statusCode = 301 @@ -71,7 +71,7 @@ test('do not follow redirects and do not error', function (t) { }) server.listen(0, function () { - var port = server.address().port + const port = server.address().port get({ url: 'http://localhost:' + port + '/0', followRedirects: false @@ -87,8 +87,8 @@ test('do not follow redirects and do not error', function (t) { test('follow redirects (11 is too many)', function (t) { t.plan(12) - var num = 0 - var server = http.createServer(function (req, res) { + let num = 0 + const server = http.createServer(function (req, res) { t.equal(req.url, '/' + num, 'visited /' + num) if (num < 11) { @@ -102,7 +102,7 @@ test('follow redirects (11 is too many)', function (t) { }) server.listen(0, function () { - var port = server.address().port + const port = server.address().port get('http://localhost:' + port + '/0', function (err) { t.ok(err instanceof Error, 'got error') server.close() @@ -113,17 +113,17 @@ test('follow redirects (11 is too many)', function (t) { test('redirect https to http', function (t) { t.plan(6) - var httpPort = null - var httpsPort = null + let httpPort = null + let httpsPort = null - var httpsServer = selfSignedHttps(function (req, res) { + const 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) { + const httpServer = http.createServer(function (req, res) { t.equal(req.url, '/path2') res.statusCode = 200 res.end('response') @@ -153,17 +153,17 @@ test('redirect https to http', function (t) { test('redirect http to https', function (t) { t.plan(6) - var httpsPort = null - var httpPort = null + let httpsPort = null + let httpPort = null - var httpServer = http.createServer(function (req, res) { + const 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) { + const httpsServer = selfSignedHttps(function (req, res) { t.equal(req.url, '/path2') res.statusCode = 200 res.end('response') @@ -193,10 +193,10 @@ test('redirect http to https', function (t) { test('redirect to different host/port', function (t) { t.plan(7) - var port1 = null - var port2 = null + let port1 = null + let port2 = null - var server1 = http.createServer(function (req, res) { + const server1 = http.createServer(function (req, res) { t.equal(req.url, '/path1') res.statusCode = 301 // Redirect from localhost:port1 to 127.0.0.1:port2 (different host and port!) @@ -204,7 +204,7 @@ test('redirect to different host/port', function (t) { res.end() }) - var server2 = http.createServer(function (req, res) { + const server2 = http.createServer(function (req, res) { t.equal(req.url, '/path2') // Confirm that request was made with new host and port (127.0.0.1:port2) t.equal(req.headers.host, `127.0.0.1:${port2}`) @@ -234,10 +234,10 @@ test('redirect to different host/port', function (t) { test('redirect should clear explicitly specified `host` header', function (t) { t.plan(8) - var port1 = null - var port2 = null + let port1 = null + let port2 = null - var server1 = http.createServer(function (req, res) { + const server1 = http.createServer(function (req, res) { t.equal(req.url, '/path1') t.equal(req.headers.host, `localhost:${port1}`) res.statusCode = 301 @@ -246,7 +246,7 @@ test('redirect should clear explicitly specified `host` header', function (t) { res.end() }) - var server2 = http.createServer(function (req, res) { + const server2 = http.createServer(function (req, res) { t.equal(req.url, '/path2') // Confirm that request was made with new host and port (127.0.0.1:port2), i.e. // that the explicitly specified `Host` header was cleared upon redirect. @@ -282,10 +282,10 @@ test('redirect should clear explicitly specified `host` header', function (t) { test('redirect should clear explicitly specified `Host` (note uppercase) header', function (t) { t.plan(8) - var port1 = null - var port2 = null + let port1 = null + let port2 = null - var server1 = http.createServer(function (req, res) { + const server1 = http.createServer(function (req, res) { t.equal(req.url, '/path1') t.equal(req.headers.host, `localhost:${port1}`) res.statusCode = 301 @@ -294,7 +294,7 @@ test('redirect should clear explicitly specified `Host` (note uppercase) header' res.end() }) - var server2 = http.createServer(function (req, res) { + const server2 = http.createServer(function (req, res) { t.equal(req.url, '/path2') // Confirm that request was made with new host and port (127.0.0.1:port2), i.e. // that the explicitly specified `Host` header was cleared upon redirect. diff --git a/test/request.js b/test/request.js index 4747b52..9755287 100644 --- a/test/request.js +++ b/test/request.js @@ -1,18 +1,18 @@ -var get = require('../') -var http = require('http') -var test = require('tape') +const get = require('../') +const http = require('http') +const test = require('tape') test('access `req` object', function (t) { t.plan(2) - var server = http.createServer(function (req, res) { + const 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) { + const port = server.address().port + const req = get('http://localhost:' + port, function (err, res) { t.error(err) res.resume() // discard data server.close()