mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-11 19:04:30 +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 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() {
|
||||
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 = <string>headers['link'];
|
||||
// TODO preload more than just the entry point
|
||||
const regex = /<\/client\/client\.\w+\.js>;rel="modulepreload"/;
|
||||
const link = <string>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('<h1>HTML</h1>') !== -1);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user