From 0862d0e2c8380ded9493244a9fbf249ef41b1294 Mon Sep 17 00:00:00 2001 From: Richard Harris Date: Sat, 27 Apr 2019 10:55:22 -0400 Subject: [PATCH] add a test for server-route-as-middleware --- .../basics/src/routes/middleware/index.js | 8 ++ .../basics/src/routes/middleware/index.svelte | 1 + test/apps/basics/test.ts | 74 ++++++++++++------- 3 files changed, 56 insertions(+), 27 deletions(-) create mode 100644 test/apps/basics/src/routes/middleware/index.js create mode 100644 test/apps/basics/src/routes/middleware/index.svelte diff --git a/test/apps/basics/src/routes/middleware/index.js b/test/apps/basics/src/routes/middleware/index.js new file mode 100644 index 0000000..52de5b6 --- /dev/null +++ b/test/apps/basics/src/routes/middleware/index.js @@ -0,0 +1,8 @@ +export function get(req, res, next) { + if (req.headers.accept === 'application/json') { + res.end('{"json":true}'); + return; + } + + next(); +} \ No newline at end of file diff --git a/test/apps/basics/src/routes/middleware/index.svelte b/test/apps/basics/src/routes/middleware/index.svelte new file mode 100644 index 0000000..9c4e094 --- /dev/null +++ b/test/apps/basics/src/routes/middleware/index.svelte @@ -0,0 +1 @@ +

HTML

\ No newline at end of file diff --git a/test/apps/basics/test.ts b/test/apps/basics/test.ts index 1247bfc..e1f16a2 100644 --- a/test/apps/basics/test.ts +++ b/test/apps/basics/test.ts @@ -8,6 +8,25 @@ import { wait } from '../../utils'; declare let deleted: { id: number }; declare let el: any; +function get(url: string, opts?: any): Promise<{ headers: Record, body: string }> { + return new Promise((fulfil, reject) => { + const req = http.get(url, opts || {}, res => { + res.on('error', reject); + + let body = ''; + res.on('data', chunk => body += chunk); + res.on('end', () => { + fulfil({ + headers: res.headers as Record, + body + }); + }); + }); + + req.on('error', reject); + }); +} + describe('basics', function() { this.timeout(10000); @@ -116,38 +135,25 @@ describe('basics', function() { assert.equal(requests[1], `${base}/b.json`); }); - // TODO equivalent test for a webpack app - it('sets Content-Type, Link...modulepreload, and Cache-Control headers', () => { - return new Promise((fulfil, reject) => { - const req = http.get(base, res => { - try { - const { headers } = res; + it('sets Content-Type, Link...modulepreload, and Cache-Control headers', async () => { + const { headers } = await get(base); - assert.equal( - headers['content-type'], - 'text/html' - ); + assert.equal( + headers['content-type'], + 'text/html' + ); - assert.equal( - headers['cache-control'], - 'max-age=600' - ); + assert.equal( + headers['cache-control'], + 'max-age=600' + ); - // TODO preload more than just the entry point - const regex = /<\/client\/client\.\w+\.js>;rel="modulepreload"/; - const link = headers['link']; + // TODO preload more than just the entry point + const regex = /<\/client\/client\.\w+\.js>;rel="modulepreload"/; + const link = headers['link']; - assert.ok(regex.test(link), link); - - fulfil(); - } catch (err) { - reject(err); - } - }); - - req.on('error', reject); - }); + assert.ok(regex.test(link), link); }); it('calls a delete handler', async () => { @@ -275,4 +281,18 @@ describe('basics', function() { await wait(50); assert.equal(await title(), 'bar'); }); + + it('runs server route handlers before page handlers, if they match', async () => { + const json = await get(`${base}/middleware`, { + headers: { + 'Accept': 'application/json' + } + }); + + assert.equal(json.body, '{"json":true}'); + + const html = await get(`${base}/middleware`); + + assert.ok(html.body.indexOf('

HTML

') !== -1); + }); }); \ No newline at end of file