From d63fe2fa46212a66134175fe2cbc4ca8d84dd371 Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Thu, 25 Aug 2022 13:50:12 +0300 Subject: [PATCH] test(service-worker): simplify how redirects are defined in `MockServerState` (#47260) Previously, the `MockServerStateBuilder#withRedirect()` method did two things: (a) define a redirect from one path to another and (b) specify the contents of the redirect destination. This was confusing, because it deviated from the regular way of specifying file contents, which is via a `MockFileSystem` instance. This commit slightly simplifies the process of defining redirects by having the `withRedirect()` method only define the redirect and let the contents of the redirect destination be specified as usual via `MockFileSystem`. This makes `MockFileSystem` the single source of truth for file contents used with `MockServerState`. PR Close #47260 --- .../service-worker/worker/test/happy_spec.ts | 22 +++++++++---------- .../service-worker/worker/testing/mock.ts | 5 ++--- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/packages/service-worker/worker/test/happy_spec.ts b/packages/service-worker/worker/test/happy_spec.ts index a31d764abee..771ff74efda 100644 --- a/packages/service-worker/worker/test/happy_spec.ts +++ b/packages/service-worker/worker/test/happy_spec.ts @@ -32,6 +32,7 @@ const dist = .addFile('/qux.txt', 'this is qux') .addFile('/quux.txt', 'this is quux') .addFile('/quuux.txt', 'this is quuux') + .addFile('/redirect-target.txt', 'this was a redirect') .addFile('/lazy/unchanged1.txt', 'this is unchanged (1)') .addFile('/lazy/unchanged2.txt', 'this is unchanged (2)') .addUnhashedFile('/unhashed/a.txt', 'this is unhashed', {'Cache-Control': 'max-age=10'}) @@ -48,6 +49,7 @@ const distUpdate = .addFile('/qux.txt', 'this is qux v2') .addFile('/quux.txt', 'this is quux v2') .addFile('/quuux.txt', 'this is quuux v2') + .addFile('/redirect-target.txt', 'this was a redirect') .addFile('/lazy/unchanged1.txt', 'this is unchanged (1)') .addFile('/lazy/unchanged2.txt', 'this is unchanged (2)') .addUnhashedFile('/unhashed/a.txt', 'this is unhashed v2', {'Cache-Control': 'max-age=10'}) @@ -265,23 +267,21 @@ const manifestUpdate: Manifest = { hashTable: tmpHashTableForFs(distUpdate), }; -const serverBuilderBase = - new MockServerStateBuilder() - .withStaticFiles(dist) - .withRedirect('/redirected.txt', '/redirect-target.txt', 'this was a redirect') - .withError('/error.txt'); +const serverBuilderBase = new MockServerStateBuilder() + .withStaticFiles(dist) + .withRedirect('/redirected.txt', '/redirect-target.txt') + .withError('/error.txt'); const server = serverBuilderBase.withManifest(manifest).build(); const serverRollback = serverBuilderBase.withManifest({...manifest, timestamp: manifest.timestamp + 1}).build(); -const serverUpdate = - new MockServerStateBuilder() - .withStaticFiles(distUpdate) - .withManifest(manifestUpdate) - .withRedirect('/redirected.txt', '/redirect-target.txt', 'this was a redirect') - .build(); +const serverUpdate = new MockServerStateBuilder() + .withStaticFiles(distUpdate) + .withManifest(manifestUpdate) + .withRedirect('/redirected.txt', '/redirect-target.txt') + .build(); const brokenServer = new MockServerStateBuilder().withStaticFiles(brokenFs).withManifest(brokenManifest).build(); diff --git a/packages/service-worker/worker/testing/mock.ts b/packages/service-worker/worker/testing/mock.ts index 62bb1c84e00..435b3d98bb1 100644 --- a/packages/service-worker/worker/testing/mock.ts +++ b/packages/service-worker/worker/testing/mock.ts @@ -104,9 +104,8 @@ export class MockServerStateBuilder { return this; } - withRedirect(from: string, to: string, toContents: string): MockServerStateBuilder { - this.resources.set(from, new MockResponse(toContents, {redirected: true, url: to})); - this.resources.set(to, new MockResponse(toContents)); + withRedirect(from: string, to: string): MockServerStateBuilder { + this.resources.set(from, new MockResponse('', {redirected: true, url: to})); return this; }