diff --git a/package.json b/package.json index 2e89a3e..6671cde 100644 --- a/package.json +++ b/package.json @@ -61,8 +61,9 @@ "url-parse-lax": "^1.0.0" }, "devDependencies": { - "ava": "^0.2.0", + "ava": "git+https://github.com/sindresorhus/ava.git", "coveralls": "^2.11.4", + "get-port": "^1.0.0", "into-stream": "^2.0.0", "nyc": "^3.2.2", "pem": "^1.4.4", diff --git a/test/_server.js b/test/_server.js index fe14838..7f5750e 100644 --- a/test/_server.js +++ b/test/_server.js @@ -1,39 +1,41 @@ import http from 'http'; import https from 'https'; import pify from 'pify'; +import getPort from 'get-port'; export const host = 'localhost'; -export let port = 6767; -export let portSSL = 16167; +const getPortify = pify(getPort); -export const createServer = port2 => { - port = port2 || ++port; +export const createServer = () => { + return getPortify() + .then(port => { + const s = http.createServer((req, resp) => s.emit(req.url, req, resp)); - const s = http.createServer((req, resp) => s.emit(req.url, req, resp)); + s.host = host; + s.port = port; + s.url = `http://${host}:${port}`; + s.protocol = 'http'; - s.host = host; - s.port = port; - s.url = `http://${host}:${port}`; - s.protocol = 'http'; + s.listen = pify(s.listen); + s.close = pify(s.close); - s.listen = pify(s.listen); - s.close = pify(s.close); - - return s; + return s; + }); }; -export const createSSLServer = (portSSL2, opts) => { - portSSL = portSSL2 || ++portSSL; - - const s = https.createServer(opts, (req, resp) => s.emit(req.url, req, resp)); +export const createSSLServer = (opts) => { + return getPortify() + .then(port => { + const s = https.createServer(opts, (req, resp) => s.emit(req.url, req, resp)); - s.host = host; - s.port = portSSL; - s.url = `https://${host}:${portSSL}`; - s.protocol = 'https'; + s.host = host; + s.port = port; + s.url = `https://${host}:${port}`; + s.protocol = 'https'; - s.listen = pify(s.listen); - s.close = pify(s.close); + s.listen = pify(s.listen); + s.close = pify(s.close); - return s; + return s; + }); }; diff --git a/test/arguments.js b/test/arguments.js index a3537a2..1845c4d 100644 --- a/test/arguments.js +++ b/test/arguments.js @@ -3,26 +3,28 @@ import pify from 'pify'; import got from '../'; import {createServer} from './_server'; -const s = createServer(); +let s; -s.on('/', (req, res) => { - res.statusCode = 404; - res.end(); -}); +test.before('setup', async t => { + s = await createServer(); -s.on('/test', (req, res) => { - res.end(req.url); -}); + s.on('/', (req, res) => { + res.statusCode = 404; + res.end(); + }); -s.on('/?test=wow', (req, res) => { - res.end(req.url); -}); + s.on('/test', (req, res) => { + res.end(req.url); + }); + + s.on('/?test=wow', (req, res) => { + res.end(req.url); + }); -test.before('arguments - setup', async t => { await s.listen(s.port); }); -test('arguments - url argument is required', async t => { +test('url is required', async t => { try { await got(); t.fail('Exception is not thrown'); @@ -31,15 +33,15 @@ test('arguments - url argument is required', async t => { } }); -test('arguments - accepts url.parse object as first argument', async t => { +test('accepts url.parse object as first argument', async t => { t.is((await got({hostname: s.host, port: s.port, path: '/test'})).body, '/test'); }); -test('arguments - overrides querystring from opts', async t => { +test('overrides querystring from opts', async t => { t.is((await got(`${s.url}/?test=doge`, {query: {test: 'wow'}})).body, '/?test=wow'); }); -test('arguments - should throw with auth in url', async t => { +test('should throw with auth in url', async t => { try { await got(`https://test:45d3ps453@account.myservice.com/api/token`); t.fail('Exception is not thrown'); @@ -48,6 +50,6 @@ test('arguments - should throw with auth in url', async t => { } }); -test.after('arguments - cleanup', async t => { +test.after('cleanup', async t => { await s.close(); }); diff --git a/test/error.js b/test/error.js index aff71b1..68b8d70 100644 --- a/test/error.js +++ b/test/error.js @@ -2,18 +2,20 @@ import test from 'ava'; import got from '../'; import {createServer} from './_server'; -const s = createServer(); +let s; -s.on('/', (req, res) => { - res.statusCode = 404; - res.end('not'); -}); +test.before('setup', async t => { + s = await createServer(); + + s.on('/', (req, res) => { + res.statusCode = 404; + res.end('not'); + }); -test.before('error - setup', async t => { await s.listen(s.port); }); -test('error - error message', async t => { +test('message', async t => { try { await got(s.url); t.fail('Exception was not thrown'); @@ -25,7 +27,7 @@ test('error - error message', async t => { } }); -test('error - dns error message', async t => { +test('dns message', async t => { try { await got('.com', {retries: 0}); t.fail('Exception was not thrown'); @@ -37,7 +39,7 @@ test('error - dns error message', async t => { } }); -test('error - options.body error message', async t => { +test('options.body error message', async t => { try { got(s.url, {body: () => {}}, () => {}); t.fail('Exception was not thrown'); @@ -53,6 +55,6 @@ test('error - options.body error message', async t => { } }); -test.after('error - cleanup', async t => { +test.after('cleanup', async t => { await s.close(); }); diff --git a/test/gzip.js b/test/gzip.js index 7960ad0..a9c08ee 100644 --- a/test/gzip.js +++ b/test/gzip.js @@ -3,32 +3,35 @@ import test from 'ava'; import got from '../'; import {createServer} from './_server'; -const s = createServer(); const testContent = 'Compressible response content.\n'; -s.on('/', (req, res) => { - res.statusCode = 200; - res.setHeader('Content-Type', 'text/plain'); - res.setHeader('Content-Encoding', 'gzip'); - zlib.gzip(testContent, (_, data) => res.end(data)); -}); +let s; -s.on('/corrupted', (req, res) => { - res.statusCode = 200; - res.setHeader('Content-Type', 'text/plain'); - res.setHeader('Content-Encoding', 'gzip'); - res.end('Not gzipped content'); -}); +test.before('setup', async t => { + s = await createServer(); + + s.on('/', (req, res) => { + res.statusCode = 200; + res.setHeader('Content-Type', 'text/plain'); + res.setHeader('Content-Encoding', 'gzip'); + zlib.gzip(testContent, (_, data) => res.end(data)); + }); + + s.on('/corrupted', (req, res) => { + res.statusCode = 200; + res.setHeader('Content-Type', 'text/plain'); + res.setHeader('Content-Encoding', 'gzip'); + res.end('Not gzipped content'); + }); -test.before('gzip - setup', async t => { await s.listen(s.port); }); -test('gzip - ungzip content', async t => { +test('decompress content', async t => { t.is((await got(s.url)).body, testContent); }); -test('gzip - ungzip error', async t => { +test('handles gzip error', async t => { try { await got(`${s.url}/corrupted`); t.fail('Exception was not thrown'); @@ -39,10 +42,10 @@ test('gzip - ungzip error', async t => { } }); -test('gzip - preserve headers property', async t => { +test('preserve headers property', async t => { t.ok((await got(s.url)).headers); }); -test.after('gzip - cleanup', async t => { +test.after('cleanup', async t => { await s.close(); }); diff --git a/test/headers.js b/test/headers.js index 32ffec2..9fa3e25 100644 --- a/test/headers.js +++ b/test/headers.js @@ -2,41 +2,43 @@ import test from 'ava'; import got from '../'; import {createServer} from './_server'; -const s = createServer(); +let s; -s.on('/', (req, res) => { - res.end(JSON.stringify(req.headers)); -}); +test.before('setup', async t => { + s = await createServer(); + + s.on('/', (req, res) => { + res.end(JSON.stringify(req.headers)); + }); -test.before('headers - setup', async t => { await s.listen(s.port); }); -test('headers - send user-agent header by default', async t => { +test('user-agent', async t => { const headers = (await got(s.url, {json: true})).body; t.is(headers['user-agent'], 'https://github.com/sindresorhus/got'); }); -test('headers - send accept-encoding header by default', async t => { +test('accept-encoding', async t => { const headers = (await got(s.url, {json: true})).body; t.is(headers['accept-encoding'], 'gzip,deflate'); }); -test('headers - send accept header with json option', async t => { +test('accept header with json option', async t => { const headers = (await got(s.url, {json: true})).body; t.is(headers.accept, 'application/json'); }); -test('headers - send host header by default', async t => { +test('host', async t => { const headers = (await got(s.url, {json: true})).body; t.is(headers.host, `localhost:${s.port}`); }); -test('headers - transform headers names to lowercase', async t => { +test('transform names to lowercase', async t => { const headers = (await got(s.url, {headers: {'USER-AGENT': 'test'}, json: true})).body; t.is(headers['user-agent'], 'test'); }); -test.after('headers - cleanup', async t => { +test.after('cleanup', async t => { await s.close(); }); diff --git a/test/helpers.js b/test/helpers.js index 4dbcee4..0bac1ee 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -2,22 +2,24 @@ import test from 'ava'; import got from '../'; import {createServer} from './_server'; -const s = createServer(); +let s; -s.on('/', (req, res) => { - res.end('ok'); -}); +test.before('setup', async t => { + s = await createServer(); -s.on('/404', (req, res) => { - res.statusCode = 404; - res.end('not found'); -}); + s.on('/', (req, res) => { + res.end('ok'); + }); + + s.on('/404', (req, res) => { + res.statusCode = 404; + res.end('not found'); + }); -test.before('helpers - setup', async t => { await s.listen(s.port); }); -test('helpers - callback mode', t => { +test('callback mode', t => { got.get(s.url, function (err, body) { t.ifError(err); t.is(body, 'ok'); @@ -25,7 +27,7 @@ test('helpers - callback mode', t => { }); }); -test('helpers - promise mode', async t => { +test('promise mode', async t => { t.is((await got.get(s.url)).body, 'ok'); try { @@ -43,6 +45,6 @@ test('helpers - promise mode', async t => { } }); -test.after('helpers - cleanup', async t => { +test.after('cleanup', async t => { await s.close(); }); diff --git a/test/http.js b/test/http.js index 697daad..3a09d0c 100644 --- a/test/http.js +++ b/test/http.js @@ -2,44 +2,46 @@ import test from 'ava'; import got from '../'; import {createServer} from './_server'; -const s = createServer(); +let s; -s.on('/', (req, res) => { - res.end('ok'); -}); +test.before('setup', async t => { + s = await createServer(); -s.on('/empty', (req, res) => { - res.end(); -}); + s.on('/', (req, res) => { + res.end('ok'); + }); -s.on('/404', (req, res) => { - setTimeout(() => { - res.statusCode = 404; - res.end('not'); - }, 10); -}); + s.on('/empty', (req, res) => { + res.end(); + }); -s.on('/?recent=true', (req, res) => { - res.end('recent'); -}); + s.on('/404', (req, res) => { + setTimeout(() => { + res.statusCode = 404; + res.end('not'); + }, 10); + }); + + s.on('/?recent=true', (req, res) => { + res.end('recent'); + }); -test.before('http - setup', async t => { await s.listen(s.port); }); -test('http - simple request', async t => { +test('simple request', async t => { t.is((await got(s.url)).body, 'ok'); }); -test('http - protocol-less URLs', async t => { +test('protocol-less URLs', async t => { t.is((await got(s.url.replace(/^http:\/\//, ''))).body, 'ok'); }); -test('http - empty response', async t => { +test('empty response', async t => { t.is((await got(`${s.url}/empty`)).body, ''); }); -test('http - error with code', async t => { +test('error with code', async t => { try { await got(`${s.url}/404`); t.fail('Exception was not thrown'); @@ -49,12 +51,12 @@ test('http - error with code', async t => { } }); -test('http - buffer on encoding === null', async t => { +test('buffer on encoding === null', async t => { const data = (await got(s.url, {encoding: null})).body; t.ok(Buffer.isBuffer(data)); }); -test('http - timeout option', async t => { +test('timeout option', async t => { try { await got(`${s.url}/404`, {timeout: 1, retries: 0}); t.fail('Exception was not thrown'); @@ -63,11 +65,11 @@ test('http - timeout option', async t => { } }); -test('http - query option', async t => { +test('query option', async t => { t.is((await got(s.url, {query: {recent: true}})).body, 'recent'); t.is((await got(s.url, {query: 'recent=true'})).body, 'recent'); }); -test.after('http - cleanup', async t => { +test.after('cleanup', async t => { await s.close(); }); diff --git a/test/https.js b/test/https.js index 40927af..ce03957 100644 --- a/test/https.js +++ b/test/https.js @@ -2,7 +2,7 @@ import test from 'ava'; import pem from 'pem'; import pify from 'pify'; import got from '../'; -import {createSSLServer, portSSL} from './_server'; +import {createSSLServer} from './_server'; let s; let key; @@ -12,14 +12,12 @@ let caRootCert; let pemify = pify.all(pem); -test.before('https - create root pem', async t => { - const keys = await pemify.createCertificate({days: 1, selfSigned: true}); +test.before('setup', async t => { + const caKeys = await pemify.createCertificate({days: 1, selfSigned: true}); - caRootKey = keys.serviceKey; - caRootCert = keys.certificate; -}); + caRootKey = caKeys.serviceKey; + caRootCert = caKeys.certificate; -test.before('https - create pem', async t => { const keys = await pemify.createCertificate({ serviceCertificate: caRootCert, serviceKey: caRootKey, @@ -35,23 +33,23 @@ test.before('https - create pem', async t => { key = keys.clientKey; cert = keys.certificate; -}); -test.before('https - setup', async t => { - s = createSSLServer(portSSL + 1, {key, cert}); + s = await createSSLServer({key, cert}); + s.on('/', (req, res) => res.end('ok')); + await s.listen(s.port); }); -test('https - redirects from http to https works', async t => { +test('redirects from http to https works', async t => { t.ok((await got('http://github.com')).body); }); -test('https - make request to https server', async t => { +test('make request to https server', async t => { t.ok((await got('https://google.com', {strictSSL: true})).body); }); -test('https - make request to https server with ca', async t => { +test('make request to https server with ca', async t => { const {body} = await got(s.url, { strictSSL: true, ca: caRootCert, @@ -60,6 +58,6 @@ test('https - make request to https server with ca', async t => { t.is(body, 'ok'); }); -test.after('https - cleanup', async t => { +test.after('cleanup', async t => { await s.close(); }); diff --git a/test/json.js b/test/json.js index a76b117..b0b08ef 100644 --- a/test/json.js +++ b/test/json.js @@ -2,45 +2,47 @@ import test from 'ava'; import got from '../'; import {createServer} from './_server'; -const s = createServer(); +let s; -s.on('/', (req, res) => { - res.end('{"data":"dog"}'); -}); +test.before('setup', async t => { + s = await createServer(); -s.on('/invalid', (req, res) => { - res.end('/'); -}); + s.on('/', (req, res) => { + res.end('{"data":"dog"}'); + }); -s.on('/204', (req, res) => { - res.statusCode = 204; - res.end(); -}); + s.on('/invalid', (req, res) => { + res.end('/'); + }); -s.on('/non200', (req, res) => { - res.statusCode = 500; - res.end('{"data":"dog"}'); -}); + s.on('/204', (req, res) => { + res.statusCode = 204; + res.end(); + }); -s.on('/non200-invalid', (req, res) => { - res.statusCode = 500; - res.end('Internal error'); -}); + s.on('/non200', (req, res) => { + res.statusCode = 500; + res.end('{"data":"dog"}'); + }); + + s.on('/non200-invalid', (req, res) => { + res.statusCode = 500; + res.end('Internal error'); + }); -test.before('json - setup', async t => { await s.listen(s.port); }); -test('json - json option should parse response', async t => { +test('parses response', async t => { t.same((await got(s.url, {json: true})).body, {data: 'dog'}); }); -test('json - json option should not parse responses without a body', async t => { +test('not parses responses without a body', async t => { const {body} = await got(`${s.url}/204`, {json: true}); t.is(body, ''); }); -test('json - json option wrap parsing errors', async t => { +test('wraps parsing errors', async t => { try { await got(`${s.url}/invalid`, {json: true}); t.fail('Exception was not thrown'); @@ -51,7 +53,7 @@ test('json - json option wrap parsing errors', async t => { } }); -test('json - json option should parse non-200 responses', async t => { +test('parses non-200 responses', async t => { try { await got(`${s.url}/non200`, {json: true}); t.fail('Exception was not thrown'); @@ -60,7 +62,7 @@ test('json - json option should parse non-200 responses', async t => { } }); -test('json - json option should catch errors on invalid non-200 responses', async t => { +test('catches errors on invalid non-200 responses', async t => { try { await got(`${s.url}/non200-invalid`, {json: true}); t.fail('Exception was not thrown'); @@ -71,6 +73,6 @@ test('json - json option should catch errors on invalid non-200 responses', asyn } }); -test.after('json - cleanup', async t => { +test.after('cleanup', async t => { await s.close(); }); diff --git a/test/post.js b/test/post.js index 6c5da33..55aadac 100644 --- a/test/post.js +++ b/test/post.js @@ -5,67 +5,69 @@ import intoStream from 'into-stream'; import got from '../'; import {createServer} from './_server'; -const s = createServer(); +let s; -s.on('/', (req, res) => { - res.setHeader('method', req.method); - req.pipe(res); -}); +test.before('setup', async t => { + s = await createServer(); -s.on('/headers', (req, res) => { - res.end(JSON.stringify(req.headers)); -}); + s.on('/', (req, res) => { + res.setHeader('method', req.method); + req.pipe(res); + }); -s.on('/empty', (req, res) => { - res.end(); -}); + s.on('/headers', (req, res) => { + res.end(JSON.stringify(req.headers)); + }); + + s.on('/empty', (req, res) => { + res.end(); + }); -test.before('post - setup', async t => { await s.listen(s.port); }); -test('post - GET can have body', async t => { +test('GET can have body', async t => { const {body, headers} = await got.get(s.url, {body: 'hi'}); t.is(body, 'hi'); t.is(headers.method, 'GET'); }); -test('post - send data from options with post request', async t => { +test('sends strings', async t => { const {body} = await got(s.url, {body: 'wow'}); t.is(body, 'wow'); }); -test('post - send data from options with post request', async t => { +test('sends Buffers', async t => { const {body} = await got(s.url, {body: new Buffer('wow')}); t.is(body, 'wow'); }); -test('post - send data from options with post request', async t => { +test('sends Streams', async t => { const {body} = await got(s.url, {body: intoStream(['wow'])}); t.is(body, 'wow'); }); -test('post - works with empty post response', async t => { +test('works with empty post response', async t => { const {body} = await got(`${s.url}/empty`, {body: 'wow'}); t.is(body, ''); }); -test('post - post have content-length header to string', async t => { +test('content-length header with string body', async t => { const {body} = await got(`${s.url}/headers`, {body: 'wow', json: true}); t.is(body['content-length'], '3'); }); -test('post - post have content-length header to string', async t => { +test('content-length header with Buffer body', async t => { const {body} = await got(`${s.url}/headers`, {body: new Buffer('wow'), json: true}); t.is(body['content-length'], '3'); }); -test('post - post have content-length header to string', async t => { +test('content-length header with Stream body', async t => { const {body} = await got(`${s.url}/headers`, {body: intoStream(['wow']), json: true}); t.is(body['content-length'], undefined); }); -test('post - post have content-length header to string', async t => { +test('content-length header is not overriden', async t => { const {body} = await got(`${s.url}/headers`, { body: 'wow', json: true, @@ -76,7 +78,7 @@ test('post - post have content-length header to string', async t => { t.is(body['content-length'], '10'); }); -test('post - post have content-length header to string', async t => { +test('content-length header disabled for chunked transfer-encoding', async t => { const {body} = await got(`${s.url}/headers`, { body: '3\r\nwow\r\n0\r\n', json: true, @@ -87,7 +89,7 @@ test('post - post have content-length header to string', async t => { t.is(body['content-length'], undefined); }); -test('post - works with plain object in body', async t => { +test('object in options.body treated as querystring', async t => { const {body} = await got(s.url, { body: { such: 'wow' @@ -96,7 +98,7 @@ test('post - works with plain object in body', async t => { t.is(body, 'such=wow'); }); -test('post - works with plain object in body', async t => { +test('content-type header is not overriden when object in options.body', async t => { const {body} = await got(`${s.url}/headers`, { headers: { 'content-type': 'doge' @@ -109,6 +111,6 @@ test('post - works with plain object in body', async t => { t.is(body['content-type'], 'doge'); }); -test.after('post - cleanup', async t => { +test.after('cleanup', async t => { await s.close(); }); diff --git a/test/redirects.js b/test/redirects.js index 23deeca..a723e8b 100644 --- a/test/redirects.js +++ b/test/redirects.js @@ -2,53 +2,55 @@ import test from 'ava'; import got from '../'; import {createServer} from './_server'; -const s = createServer(); +let s; -s.on('/', (req, res) => { - res.end('reached'); -}); +test.before('setup', async t => { + s = await createServer(); -s.on('/finite', (req, res) => { - res.writeHead(302, { - location: `${s.url}/` + s.on('/', (req, res) => { + res.end('reached'); }); - res.end(); -}); -s.on('/endless', (req, res) => { - res.writeHead(302, { - location: `${s.url}/endless` + s.on('/finite', (req, res) => { + res.writeHead(302, { + location: `${s.url}/` + }); + res.end(); }); - res.end(); -}); -s.on('/relative', (req, res) => { - res.writeHead(302, { - location: '/' + s.on('/endless', (req, res) => { + res.writeHead(302, { + location: `${s.url}/endless` + }); + res.end(); }); - res.end(); -}); -s.on('/relativeQuery?bang', (req, res) => { - res.writeHead(302, { - location: '/' + s.on('/relative', (req, res) => { + res.writeHead(302, { + location: '/' + }); + res.end(); + }); + + s.on('/relativeQuery?bang', (req, res) => { + res.writeHead(302, { + location: '/' + }); + res.end(); }); - res.end(); -}); -test.before('redirects - setup', async t => { await s.listen(s.port); }); -test('redirects - follows redirect', async t => { +test('follows redirect', async t => { t.is((await got(`${s.url}/finite`)).body, 'reached'); }); -test('redirects - follows relative redirect', async t => { +test('relative redirect works', async t => { t.is((await got(`${s.url}/relative`)).body, 'reached'); }); -test('redirects - throws on endless redirect', async t => { +test('throws on endless redirect', async t => { try { await got(`${s.url}/endless`); t.fail('Exception was not thrown'); @@ -57,15 +59,15 @@ test('redirects - throws on endless redirect', async t => { } }); -test('redirects - query in options are not breaking redirects', async t => { +test('query in options are not breaking redirects', async t => { t.is((await got(`${s.url}/relativeQuery`, {query: 'bang'})).body, 'reached'); }); -test('redirects - hostname+path in options are not breaking redirects', async t => { +test('hostname+path in options are not breaking redirects', async t => { t.is((await got(`${s.url}/relative`, {hostname: s.host, path: '/relative'})).body, 'reached'); }); -test('redirects - redirect only GET and HEAD requests', async t => { +test('redirect only GET and HEAD requests', async t => { try { await got(`${s.url}/relative`, {body: 'wow'}); t.fail('Exception was not thrown'); @@ -76,6 +78,6 @@ test('redirects - redirect only GET and HEAD requests', async t => { } }); -test.after('redirects - cleanup', async t => { +test.after('cleanup', async t => { await s.close(); }); diff --git a/test/retry.js b/test/retry.js index bda66ce..97143d0 100644 --- a/test/retry.js +++ b/test/retry.js @@ -2,31 +2,33 @@ import test from 'ava'; import got from '../'; import {createServer} from './_server'; -const s = createServer(); +let s; +let trys = 0; +let knocks = 0; -s.on('/long', () => {}); +test.before('setup', async t => { + s = await createServer(); -let knocks = 0; -s.on('/knock-twice', (req, res) => { - if (knocks++ === 1) { - res.end('who`s there?'); - } -}); + s.on('/long', () => {}); -let trys = 0; -s.on('/try-me', () => { - trys++; -}); + s.on('/knock-twice', (req, res) => { + if (knocks++ === 1) { + res.end('who`s there?'); + } + }); + + s.on('/try-me', () => { + trys++; + }); -test.before('retry - setup', async t => { await s.listen(s.port); }); -test('retry - timeout errors', async t => { +test('works on timeout error', async t => { t.is((await got(`${s.url}/knock-twice`, {timeout: 1000})).body, 'who`s there?'); }); -test('retry - can be disabled with option', async t => { +test('can be disabled with option', async t => { try { await got(`${s.url}/try-me`, {timeout: 1000, retries: 0}); } catch (err) { @@ -35,6 +37,6 @@ test('retry - can be disabled with option', async t => { t.is(trys, 1); }); -test.after('error - cleanup', async t => { +test.after('cleanup', async t => { await s.close(); }); diff --git a/test/stream.js b/test/stream.js index 444b713..add11ac 100644 --- a/test/stream.js +++ b/test/stream.js @@ -2,40 +2,42 @@ import test from 'ava'; import got from '../'; import {createServer} from './_server'; -const s = createServer(); +let s; -s.on('/', (req, res) => { - res.end('ok'); -}); +test.before('setup', async t => { + s = await createServer(); -s.on('/post', (req, res) => { - req.pipe(res); -}); + s.on('/', (req, res) => { + res.end('ok'); + }); -s.on('/redirect', (req, res) => { - res.writeHead(302, { - location: s.url + s.on('/post', (req, res) => { + req.pipe(res); }); - res.end(); -}); -s.on('/error', (req, res) => { - res.statusCode = 404; - res.end(); -}); + s.on('/redirect', (req, res) => { + res.writeHead(302, { + location: s.url + }); + res.end(); + }); + + s.on('/error', (req, res) => { + res.statusCode = 404; + res.end(); + }); -test.before('stream - setup', async t => { await s.listen(s.port); }); -test('stream - json option can not be used in stream mode', t => { +test('option.json can not be used', t => { t.throws(() => { got.stream(s.url, {json: true}); }, 'got can not be used as stream when options.json is used'); t.end(); }); -test('stream - callback can not be used in stream mode', t => { +test('callback can not be used', t => { t.throws(() => { got.stream(s.url, {json: true}, () => {}); }, 'callback can not be used in stream mode'); @@ -47,7 +49,7 @@ test('stream - callback can not be used in stream mode', t => { t.end(); }); -test('stream - return readable stream', t => { +test('returns readable stream', t => { got.stream(s.url) .on('data', data => { t.is(data.toString(), 'ok'); @@ -55,7 +57,7 @@ test('stream - return readable stream', t => { }); }); -test('stream - return writeable stream', t => { +test('returns writeable stream', t => { t.plan(1); got.stream.post(`${s.url}/post`) .on('data', data => { @@ -64,7 +66,7 @@ test('stream - return writeable stream', t => { .end('wow'); }); -test('stream - throws on write to stream with body specified', t => { +test('throws on write to stream with body specified', t => { t.throws(() => { got.stream(s.url, {body: 'wow'}).write('wow'); }, 'got\'s stream is not writable when options.body is used'); @@ -73,7 +75,7 @@ test('stream - throws on write to stream with body specified', t => { setTimeout(t.end.bind(t), 10); }); -test('stream - request event', t => { +test('have request event', t => { got.stream(s.url) .on('request', req => { t.ok(req); @@ -81,7 +83,7 @@ test('stream - request event', t => { }); }); -test('stream - redirect event', t => { +test('have redirect event', t => { got.stream(`${s.url}/redirect`) .on('redirect', res => { t.is(res.headers.location, s.url); @@ -89,7 +91,7 @@ test('stream - redirect event', t => { }); }); -test('stream - response event', t => { +test('have response event', t => { got.stream(s.url) .on('response', res => { t.is(res.statusCode, 200); @@ -97,7 +99,7 @@ test('stream - response event', t => { }); }); -test('stream - error event', t => { +test('have error event', t => { got.stream(`${s.url}/error`, {retries: 0}) .on('response', () => { t.fail('response event should not be emitted'); @@ -110,7 +112,7 @@ test('stream - error event', t => { }); }); -test('stream - error event', t => { +test('have error event', t => { got.stream('.com', {retries: 0}) .on('response', () => { t.fail('response event should not be emitted'); @@ -121,6 +123,6 @@ test('stream - error event', t => { }); }); -test.after('stream - cleanup', async t => { +test.after('cleanup', async t => { await s.close(); }); diff --git a/test/unix-socket.js b/test/unix-socket.js index 502b8e3..7237d52 100644 --- a/test/unix-socket.js +++ b/test/unix-socket.js @@ -4,27 +4,30 @@ import test from 'ava'; import got from '../'; import {createServer} from './_server'; -const s = createServer(); const socketPath = tempfile('.socket'); -s.on('/', (req, res) => { - res.end('ok'); -}); +let s; + +test.before('setup', async t => { + s = await createServer(); + + s.on('/', (req, res) => { + res.end('ok'); + }); -test.before('unix-socket - setup', async t => { await s.listen(socketPath); }); -test('unix-socket - request via unix socket', async t => { +test('works', async t => { const url = format('http://unix:%s:%s', socketPath, '/'); t.is((await got(url)).body, 'ok'); }); -test('unix-socket - protocol-less request', async t => { +test('protocol-less works', async t => { const url = format('unix:%s:%s', socketPath, '/'); t.is((await got(url)).body, 'ok'); }); -test.after('unix-socket - cleanup', async t => { +test.after('cleanup', async t => { await s.close(); });