test(service-worker): add helper function for making navigation requests (#31865)

Helper functions for making navigation requests were created in several
places inside the test suite, so this commit creates a top-level such
helper and uses that in all tests that need it.

PR Close #31865
This commit is contained in:
George Kalpakas 2019-07-26 20:18:36 +03:00 committed by Miško Hevery
parent 24b8b3427c
commit 20dc5e83ee
1 changed files with 33 additions and 40 deletions

View File

@ -433,15 +433,9 @@ import {SwTestHarness, SwTestHarnessBuilder} from '../testing/scope';
}); });
it('handles empty client ID', async() => { it('handles empty client ID', async() => {
const navRequest = (url: string, clientId: string | null) =>
makeRequest(scope, url, clientId, {
headers: {Accept: 'text/plain, text/html, text/css'},
mode: 'navigate',
});
// Initialize the SW. // Initialize the SW.
expect(await navRequest('/foo/file1', '')).toEqual('this is foo'); expect(await makeNavigationRequest(scope, '/foo/file1', '')).toEqual('this is foo');
expect(await navRequest('/bar/file2', null)).toEqual('this is foo'); expect(await makeNavigationRequest(scope, '/bar/file2', null)).toEqual('this is foo');
await driver.initialized; await driver.initialized;
// Update to a new version. // Update to a new version.
@ -449,8 +443,8 @@ import {SwTestHarness, SwTestHarnessBuilder} from '../testing/scope';
expect(await driver.checkForUpdate()).toEqual(true); expect(await driver.checkForUpdate()).toEqual(true);
// Correctly handle navigation requests, even if `clientId` is null/empty. // Correctly handle navigation requests, even if `clientId` is null/empty.
expect(await navRequest('/foo/file1', '')).toEqual('this is foo v2'); expect(await makeNavigationRequest(scope, '/foo/file1', '')).toEqual('this is foo v2');
expect(await navRequest('/bar/file2', null)).toEqual('this is foo v2'); expect(await makeNavigationRequest(scope, '/bar/file2', null)).toEqual('this is foo v2');
}); });
it('checks for updates on restart', async() => { it('checks for updates on restart', async() => {
@ -480,9 +474,7 @@ import {SwTestHarness, SwTestHarnessBuilder} from '../testing/scope';
await driver.initialized; await driver.initialized;
server.clearRequests(); server.clearRequests();
expect(await makeRequest(scope, '/foo.txt', 'default', { expect(await makeNavigationRequest(scope, '/foo.txt')).toEqual('this is foo');
mode: 'navigate',
})).toEqual('this is foo');
scope.advance(12000); scope.advance(12000);
await driver.idle.empty; await driver.idle.empty;
@ -495,13 +487,9 @@ import {SwTestHarness, SwTestHarnessBuilder} from '../testing/scope';
await driver.initialized; await driver.initialized;
server.clearRequests(); server.clearRequests();
expect(await makeRequest(scope, '/foo.txt', 'default', { expect(await makeNavigationRequest(scope, '/foo.txt')).toEqual('this is foo');
mode: 'navigate',
})).toEqual('this is foo');
expect(await makeRequest(scope, '/foo.txt', 'default', { expect(await makeNavigationRequest(scope, '/foo.txt')).toEqual('this is foo');
mode: 'navigate',
})).toEqual('this is foo');
scope.advance(12000); scope.advance(12000);
await driver.idle.empty; await driver.idle.empty;
@ -540,12 +528,7 @@ import {SwTestHarness, SwTestHarnessBuilder} from '../testing/scope';
expect(await driver.checkForUpdate()).toEqual(true); expect(await driver.checkForUpdate()).toEqual(true);
serverUpdate.clearRequests(); serverUpdate.clearRequests();
expect(await makeRequest(scope, '/file1', 'default', { expect(await makeNavigationRequest(scope, '/file1')).toEqual('this is foo v2');
headers: {
'Accept': 'text/plain, text/html, text/css',
},
mode: 'navigate',
})).toEqual('this is foo v2');
expect(client.messages).toEqual([ expect(client.messages).toEqual([
{ {
@ -715,7 +698,7 @@ import {SwTestHarness, SwTestHarnessBuilder} from '../testing/scope';
await makeRequest(scope, '/foo.txt', undefined, {headers: {'ngsw-bypass': 'anything'}}); await makeRequest(scope, '/foo.txt', undefined, {headers: {'ngsw-bypass': 'anything'}});
server.assertNoRequestFor('/foo.txt'); server.assertNoRequestFor('/foo.txt');
await makeRequest(scope, '/foo.txt', undefined, {headers: {'ngsw-bypass': null}}); await makeRequest(scope, '/foo.txt', undefined, {headers: {'ngsw-bypass': null !}});
server.assertNoRequestFor('/foo.txt'); server.assertNoRequestFor('/foo.txt');
await makeRequest(scope, '/foo.txt', undefined, {headers: {'NGSW-bypass': 'upperCASE'}}); await makeRequest(scope, '/foo.txt', undefined, {headers: {'NGSW-bypass': 'upperCASE'}});
@ -1012,10 +995,8 @@ import {SwTestHarness, SwTestHarnessBuilder} from '../testing/scope';
}); });
describe('routing', () => { describe('routing', () => {
const navRequest = (url: string, init = {}) => makeRequest(scope, url, undefined, { const navRequest = (url: string, init = {}) =>
headers: {Accept: 'text/plain, text/html, text/css'}, makeNavigationRequest(scope, url, undefined, init);
mode: 'navigate', ...init,
});
beforeEach(async() => { beforeEach(async() => {
expect(await makeRequest(scope, '/foo.txt')).toEqual('this is foo'); expect(await makeRequest(scope, '/foo.txt')).toEqual('this is foo');
@ -1278,8 +1259,8 @@ import {SwTestHarness, SwTestHarnessBuilder} from '../testing/scope';
})(); })();
async function makeRequest( async function makeRequest(
scope: SwTestHarness, url: string, clientId: string | null = 'default', scope: SwTestHarness, url: string, clientId: string | null = 'default', init?: Object):
init?: Object): Promise<string|null> { Promise<string|null> {
const [resPromise, done] = scope.handleFetch(new MockRequest(url, init), clientId); const [resPromise, done] = scope.handleFetch(new MockRequest(url, init), clientId);
await done; await done;
const res = await resPromise; const res = await resPromise;
@ -1287,4 +1268,16 @@ async function makeRequest(
return res.text(); return res.text();
} }
return null; return null;
} }
function makeNavigationRequest(
scope: SwTestHarness, url: string, clientId?: string | null, init: Object = {}):
Promise<string|null> {
return makeRequest(scope, url, clientId, {
headers: {
Accept: 'text/plain, text/html, text/css',
...(init as any).headers,
},
mode: 'navigate', ...init,
});
}