Merge branch 'master' into navigate_fix

This commit is contained in:
Rich Harris
2019-04-27 11:11:27 -04:00
committed by GitHub
11 changed files with 158 additions and 64 deletions

View File

@@ -0,0 +1,8 @@
export function get(req, res, next) {
if (req.headers.accept === 'application/json') {
res.end('{"json":true}');
return;
}
next();
}

View File

@@ -0,0 +1 @@
<h1>HTML</h1>

View File

@@ -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 () => {
@@ -286,4 +292,18 @@ describe('basics', function() {
await wait(50);
assert.equal(await title(), 'B page');
});
});
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);
});
});

View File

@@ -1,3 +1,17 @@
<script>
import { onMount, onDestroy } from 'svelte';
export let status, error = {};
let mounted = false;
onMount(() => {
mounted = 'success';
})
</script>
<h1>{status}</h1>
<p>{error.message}</p>
<h2>{mounted}</h2>
<p>{error.message}</p>

View File

@@ -112,6 +112,17 @@ describe('errors', function() {
);
});
it('execute error page hooks', async () => {
await page.goto(`${base}/some-throw-page`);
await start();
await wait(50);
assert.equal(
await page.$eval('h2', node => node.textContent),
'success'
);
})
it('does not serve error page for async non-page error', async () => {
await page.goto(`${base}/async-throw.json`);
@@ -134,4 +145,4 @@ describe('errors', function() {
await wait(50);
assert.equal(await title(), 'No error here');
});
});
});