mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-22 07:05:24 +00:00
add a test for server-route-as-middleware
This commit is contained in:
8
test/apps/basics/src/routes/middleware/index.js
Normal file
8
test/apps/basics/src/routes/middleware/index.js
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
export function get(req, res, next) {
|
||||||
|
if (req.headers.accept === 'application/json') {
|
||||||
|
res.end('{"json":true}');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
}
|
||||||
1
test/apps/basics/src/routes/middleware/index.svelte
Normal file
1
test/apps/basics/src/routes/middleware/index.svelte
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<h1>HTML</h1>
|
||||||
@@ -8,6 +8,25 @@ import { wait } from '../../utils';
|
|||||||
declare let deleted: { id: number };
|
declare let deleted: { id: number };
|
||||||
declare let el: any;
|
declare let el: any;
|
||||||
|
|
||||||
|
function get(url: string, opts?: any): Promise<{ headers: Record<string, string>, 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<string, string>,
|
||||||
|
body
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
req.on('error', reject);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
describe('basics', function() {
|
describe('basics', function() {
|
||||||
this.timeout(10000);
|
this.timeout(10000);
|
||||||
|
|
||||||
@@ -116,38 +135,25 @@ describe('basics', function() {
|
|||||||
assert.equal(requests[1], `${base}/b.json`);
|
assert.equal(requests[1], `${base}/b.json`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// TODO equivalent test for a webpack app
|
// TODO equivalent test for a webpack app
|
||||||
it('sets Content-Type, Link...modulepreload, and Cache-Control headers', () => {
|
it('sets Content-Type, Link...modulepreload, and Cache-Control headers', async () => {
|
||||||
return new Promise((fulfil, reject) => {
|
const { headers } = await get(base);
|
||||||
const req = http.get(base, res => {
|
|
||||||
try {
|
|
||||||
const { headers } = res;
|
|
||||||
|
|
||||||
assert.equal(
|
assert.equal(
|
||||||
headers['content-type'],
|
headers['content-type'],
|
||||||
'text/html'
|
'text/html'
|
||||||
);
|
);
|
||||||
|
|
||||||
assert.equal(
|
assert.equal(
|
||||||
headers['cache-control'],
|
headers['cache-control'],
|
||||||
'max-age=600'
|
'max-age=600'
|
||||||
);
|
);
|
||||||
|
|
||||||
// TODO preload more than just the entry point
|
// TODO preload more than just the entry point
|
||||||
const regex = /<\/client\/client\.\w+\.js>;rel="modulepreload"/;
|
const regex = /<\/client\/client\.\w+\.js>;rel="modulepreload"/;
|
||||||
const link = <string>headers['link'];
|
const link = <string>headers['link'];
|
||||||
|
|
||||||
assert.ok(regex.test(link), link);
|
assert.ok(regex.test(link), link);
|
||||||
|
|
||||||
fulfil();
|
|
||||||
} catch (err) {
|
|
||||||
reject(err);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
req.on('error', reject);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('calls a delete handler', async () => {
|
it('calls a delete handler', async () => {
|
||||||
@@ -275,4 +281,18 @@ describe('basics', function() {
|
|||||||
await wait(50);
|
await wait(50);
|
||||||
assert.equal(await title(), 'bar');
|
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('<h1>HTML</h1>') !== -1);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user