mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-17 21:24:59 +00:00
Merge branch 'master' of github.com:sveltejs/sapper
This commit is contained in:
@@ -8,6 +8,10 @@ declare const prefetchRoutes: () => Promise<void>;
|
|||||||
declare const prefetch: (href: string) => Promise<void>;
|
declare const prefetch: (href: string) => Promise<void>;
|
||||||
declare const goto: (href: string) => Promise<void>;
|
declare const goto: (href: string) => Promise<void>;
|
||||||
|
|
||||||
|
type StartOpts = {
|
||||||
|
requestInterceptor?: (interceptedRequst: puppeteer.Request) => any
|
||||||
|
};
|
||||||
|
|
||||||
export class AppRunner {
|
export class AppRunner {
|
||||||
cwd: string;
|
cwd: string;
|
||||||
entry: string;
|
entry: string;
|
||||||
@@ -24,7 +28,7 @@ export class AppRunner {
|
|||||||
this.messages = [];
|
this.messages = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
async start() {
|
async start({ requestInterceptor }: StartOpts = {}) {
|
||||||
this.port = await ports.find(3000);
|
this.port = await ports.find(3000);
|
||||||
|
|
||||||
this.proc = fork(this.entry, [], {
|
this.proc = fork(this.entry, [], {
|
||||||
@@ -50,19 +54,10 @@ export class AppRunner {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
await this.page.setRequestInterception(true);
|
if (requestInterceptor) {
|
||||||
|
await this.page.setRequestInterception(true);
|
||||||
this.page.on('request', interceptedRequest => {
|
this.page.on('request', requestInterceptor);
|
||||||
if (/example\.com/.test(interceptedRequest.url())) {
|
}
|
||||||
interceptedRequest.respond({
|
|
||||||
status: 200,
|
|
||||||
contentType: 'text/html',
|
|
||||||
body: `<h1>external</h1>`
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
interceptedRequest.continue();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
page: this.page,
|
page: this.page,
|
||||||
|
|||||||
@@ -21,7 +21,19 @@ describe('redirects', function() {
|
|||||||
await build({ cwd: __dirname });
|
await build({ cwd: __dirname });
|
||||||
|
|
||||||
runner = new AppRunner(__dirname, '__sapper__/build/server/server.js');
|
runner = new AppRunner(__dirname, '__sapper__/build/server/server.js');
|
||||||
({ base, page, start, prefetchRoutes, title } = await runner.start());
|
({ base, page, start, prefetchRoutes, title } = await runner.start({
|
||||||
|
requestInterceptor: (interceptedRequest) => {
|
||||||
|
if (/example\.com/.test(interceptedRequest.url())) {
|
||||||
|
interceptedRequest.respond({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'text/html',
|
||||||
|
body: `<h1>external</h1>`
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
interceptedRequest.continue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
after(() => runner.end());
|
after(() => runner.end());
|
||||||
|
|||||||
Reference in New Issue
Block a user