diff --git a/test/_server.js b/test/_server.js index 6ca6a2a..fe14838 100644 --- a/test/_server.js +++ b/test/_server.js @@ -1,5 +1,6 @@ import http from 'http'; import https from 'https'; +import pify from 'pify'; export const host = 'localhost'; export let port = 6767; @@ -15,6 +16,9 @@ export const createServer = port2 => { s.url = `http://${host}:${port}`; s.protocol = 'http'; + s.listen = pify(s.listen); + s.close = pify(s.close); + return s; }; @@ -28,5 +32,8 @@ export const createSSLServer = (portSSL2, opts) => { s.url = `https://${host}:${portSSL}`; s.protocol = 'https'; + s.listen = pify(s.listen); + s.close = pify(s.close); + return s; }; diff --git a/test/arguments.js b/test/arguments.js index 1f05978..a3537a2 100644 --- a/test/arguments.js +++ b/test/arguments.js @@ -1,4 +1,5 @@ import test from 'ava'; +import pify from 'pify'; import got from '../'; import {createServer} from './_server'; @@ -17,8 +18,8 @@ s.on('/?test=wow', (req, res) => { res.end(req.url); }); -test.before('arguments - setup', t => { - s.listen(s.port, () => t.end()); +test.before('arguments - setup', async t => { + await s.listen(s.port); }); test('arguments - url argument is required', async t => { @@ -47,7 +48,6 @@ test('arguments - should throw with auth in url', async t => { } }); -test.after('arguments - cleanup', t => { - s.close(); - t.end(); +test.after('arguments - cleanup', async t => { + await s.close(); }); diff --git a/test/error.js b/test/error.js index 97328a4..aff71b1 100644 --- a/test/error.js +++ b/test/error.js @@ -9,8 +9,8 @@ s.on('/', (req, res) => { res.end('not'); }); -test.before('error - setup', t => { - s.listen(s.port, () => t.end()); +test.before('error - setup', async t => { + await s.listen(s.port); }); test('error - error message', async t => { @@ -53,7 +53,6 @@ test('error - options.body error message', async t => { } }); -test.after('error - cleanup', t => { - s.close(); - t.end(); +test.after('error - cleanup', async t => { + await s.close(); }); diff --git a/test/gzip.js b/test/gzip.js index 47bc922..7960ad0 100644 --- a/test/gzip.js +++ b/test/gzip.js @@ -20,8 +20,8 @@ s.on('/corrupted', (req, res) => { res.end('Not gzipped content'); }); -test.before('gzip - setup', t => { - s.listen(s.port, () => t.end()); +test.before('gzip - setup', async t => { + await s.listen(s.port); }); test('gzip - ungzip content', async t => { @@ -43,7 +43,6 @@ test('gzip - preserve headers property', async t => { t.ok((await got(s.url)).headers); }); -test.after('gzip - cleanup', t => { - s.close(); - t.end(); +test.after('gzip - cleanup', async t => { + await s.close(); }); diff --git a/test/headers.js b/test/headers.js index 52cdc14..32ffec2 100644 --- a/test/headers.js +++ b/test/headers.js @@ -8,8 +8,8 @@ s.on('/', (req, res) => { res.end(JSON.stringify(req.headers)); }); -test.before('headers - setup', t => { - s.listen(s.port, () => t.end()); +test.before('headers - setup', async t => { + await s.listen(s.port); }); test('headers - send user-agent header by default', async t => { @@ -37,7 +37,6 @@ test('headers - transform headers names to lowercase', async t => { t.is(headers['user-agent'], 'test'); }); -test.after('headers - cleanup', t => { - s.close(); - t.end(); +test.after('headers - cleanup', async t => { + await s.close(); }); diff --git a/test/helpers.js b/test/helpers.js index 96efe63..4dbcee4 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -13,8 +13,8 @@ s.on('/404', (req, res) => { res.end('not found'); }); -test.before('helpers - setup', t => { - s.listen(s.port, () => t.end()); +test.before('helpers - setup', async t => { + await s.listen(s.port); }); test('helpers - callback mode', t => { @@ -43,7 +43,6 @@ test('helpers - promise mode', async t => { } }); -test.after('helpers - cleanup', t => { - s.close(); - t.end(); +test.after('helpers - cleanup', async t => { + await s.close(); }); diff --git a/test/http.js b/test/http.js index b2ba64c..697daad 100644 --- a/test/http.js +++ b/test/http.js @@ -23,8 +23,8 @@ s.on('/?recent=true', (req, res) => { res.end('recent'); }); -test.before('http - setup', t => { - s.listen(s.port, () => t.end()); +test.before('http - setup', async t => { + await s.listen(s.port); }); test('http - simple request', async t => { @@ -68,7 +68,6 @@ test('http - query option', async t => { t.is((await got(s.url, {query: 'recent=true'})).body, 'recent'); }); -test.after('http - cleanup', t => { - s.close(); - t.end(); +test.after('http - cleanup', async t => { + await s.close(); }); diff --git a/test/https.js b/test/https.js index 4311578..40927af 100644 --- a/test/https.js +++ b/test/https.js @@ -37,10 +37,10 @@ test.before('https - create pem', async t => { cert = keys.certificate; }); -test.before('https - setup', t => { +test.before('https - setup', async t => { s = createSSLServer(portSSL + 1, {key, cert}); s.on('/', (req, res) => res.end('ok')); - s.listen(s.port, () => t.end()); + await s.listen(s.port); }); test('https - redirects from http to https works', async t => { @@ -60,7 +60,6 @@ test('https - make request to https server with ca', async t => { t.is(body, 'ok'); }); -test.after('https - cleanup', t => { - s.close(); - t.end(); +test.after('https - cleanup', async t => { + await s.close(); }); diff --git a/test/json.js b/test/json.js index 92cee22..a76b117 100644 --- a/test/json.js +++ b/test/json.js @@ -27,8 +27,8 @@ s.on('/non200-invalid', (req, res) => { res.end('Internal error'); }); -test.before('json - setup', t => { - s.listen(s.port, () => t.end()); +test.before('json - setup', async t => { + await s.listen(s.port); }); test('json - json option should parse response', async t => { @@ -71,7 +71,6 @@ test('json - json option should catch errors on invalid non-200 responses', asyn } }); -test.after('json - cleanup', t => { - s.close(); - t.end(); +test.after('json - cleanup', async t => { + await s.close(); }); diff --git a/test/post.js b/test/post.js index 568be2b..6c5da33 100644 --- a/test/post.js +++ b/test/post.js @@ -20,8 +20,8 @@ s.on('/empty', (req, res) => { res.end(); }); -test.before('post - setup', t => { - s.listen(s.port, () => t.end()); +test.before('post - setup', async t => { + await s.listen(s.port); }); test('post - GET can have body', async t => { @@ -109,7 +109,6 @@ test('post - works with plain object in body', async t => { t.is(body['content-type'], 'doge'); }); -test.after('post - cleanup', t => { - s.close(); - t.end(); +test.after('post - cleanup', async t => { + await s.close(); }); diff --git a/test/redirects.js b/test/redirects.js index fa4d8dc..23deeca 100644 --- a/test/redirects.js +++ b/test/redirects.js @@ -36,8 +36,8 @@ s.on('/relativeQuery?bang', (req, res) => { res.end(); }); -test.before('redirects - setup', t => { - s.listen(s.port, () => t.end()); +test.before('redirects - setup', async t => { + await s.listen(s.port); }); test('redirects - follows redirect', async t => { @@ -76,7 +76,6 @@ test('redirects - redirect only GET and HEAD requests', async t => { } }); -test.after('redirects - cleanup', t => { - s.close(); - t.end(); +test.after('redirects - cleanup', async t => { + await s.close(); }); diff --git a/test/retry.js b/test/retry.js index 7036dc0..bda66ce 100644 --- a/test/retry.js +++ b/test/retry.js @@ -18,8 +18,8 @@ s.on('/try-me', () => { trys++; }); -test.before('retry - setup', t => { - s.listen(s.port, () => t.end()); +test.before('retry - setup', async t => { + await s.listen(s.port); }); test('retry - timeout errors', async t => { @@ -35,7 +35,6 @@ test('retry - can be disabled with option', async t => { t.is(trys, 1); }); -test.after('error - cleanup', t => { - s.close(); - t.end(); +test.after('error - cleanup', async t => { + await s.close(); }); diff --git a/test/stream.js b/test/stream.js index d935f31..444b713 100644 --- a/test/stream.js +++ b/test/stream.js @@ -24,8 +24,8 @@ s.on('/error', (req, res) => { res.end(); }); -test.before('stream - setup', t => { - s.listen(s.port, () => t.end()); +test.before('stream - setup', async t => { + await s.listen(s.port); }); test('stream - json option can not be used in stream mode', t => { @@ -121,7 +121,6 @@ test('stream - error event', t => { }); }); -test.after('stream - cleanup', t => { - s.close(); - t.end(); +test.after('stream - cleanup', async t => { + await s.close(); }); diff --git a/test/unix-socket.js b/test/unix-socket.js index 8d1b841..502b8e3 100644 --- a/test/unix-socket.js +++ b/test/unix-socket.js @@ -11,8 +11,8 @@ s.on('/', (req, res) => { res.end('ok'); }); -test.before('unix-socket - setup', t => { - s.listen(socketPath, () => t.end()); +test.before('unix-socket - setup', async t => { + await s.listen(socketPath); }); test('unix-socket - request via unix socket', async t => { @@ -25,7 +25,6 @@ test('unix-socket - protocol-less request', async t => { t.is((await got(url)).body, 'ok'); }); -test.after('unix-socket - cleanup', t => { - s.close(); - t.end(); +test.after('unix-socket - cleanup', async t => { + await s.close(); });