From f64dcc7f69053fba3170ae8d236e1cac6dedb590 Mon Sep 17 00:00:00 2001 From: Luke Childs Date: Thu, 11 May 2017 13:55:48 +0700 Subject: [PATCH] Move server endpoints inside test function for readability --- test/cache.js | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/test/cache.js b/test/cache.js index a8517e5..c078362 100644 --- a/test/cache.js +++ b/test/cache.js @@ -6,37 +6,38 @@ let s; test.before('setup', async () => { s = await createServer(); + await s.listen(s.port); +}); +test('Non cacheable requests are not cached', async t => { + const endpoint = '/no-cache'; let noCacheIndex = 0; - s.on('/no-cache', (req, res) => { + s.on(endpoint, (req, res) => { noCacheIndex++; res.end(noCacheIndex.toString()); }); - let cacheIndex = 0; - s.on('/cache', (req, res) => { - cacheIndex++; - res.setHeader('Cache-Control', 'public, max-age=60'); - res.end(cacheIndex.toString()); - }); - - await s.listen(s.port); -}); - -test('Non cacheable requests are not cached', async t => { const cache = new Map(); - const firstResponse = parseInt((await got(`${s.url}/no-cache`, {cache})).body, 10); - const secondResponse = parseInt((await got(`${s.url}/no-cache`, {cache})).body, 10); + const firstResponse = parseInt((await got(s.url + endpoint, {cache})).body, 10); + const secondResponse = parseInt((await got(s.url + endpoint, {cache})).body, 10); t.is(secondResponse, (firstResponse + 1)); }); test('Cacheable requests are cached', async t => { + const endpoint = '/cache'; + let cacheIndex = 0; + s.on(endpoint, (req, res) => { + cacheIndex++; + res.setHeader('Cache-Control', 'public, max-age=60'); + res.end(cacheIndex.toString()); + }); + const cache = new Map(); - const firstResponse = await got(`${s.url}/cache`, {cache}); - const secondResponse = await got(`${s.url}/cache`, {cache}); + const firstResponse = await got(s.url + endpoint, {cache}); + const secondResponse = await got(s.url + endpoint, {cache}); t.is(firstResponse.body, secondResponse.body); });