8000 test(service-worker): simplify how redirects are defined in `MockServ… · angular/angular@d63fe2f · GitHub
[go: up one dir, main page]

Skip to content

Commit d63fe2f

Browse files
gkalpakAndrewKushnir
authored andcommitted
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
1 parent e842fd7 commit d63fe2f

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

packages/service-worker/worker/test/happy_spec.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const dist =
3232
.addFile('/qux.txt', 'this is qux')
3333
.addFile('/quux.txt', 'this is quux')
3434
.addFile('/quuux.txt', 'this is quuux')
35+
.addFile('/redirect-target.txt', 'this was a redirect')
3536
.addFile('/lazy/unchanged1.txt', 'this is unchanged (1)')
3637
.addFile('/lazy/unchanged2.txt', 'this is unchanged (2)')
3738
.addUnhashedFile('/unhashed/a.txt', 'this is unhashed', {'Cache-Control': 'max-age=10'})
@@ -48,6 +49,7 @@ const distUpdate =
4849
.addFile('/qux.txt', 'this is qux v2')
4950
.addFile('/quux.txt', 'this is quux v2')
5051
.addFile('/quuux.txt', 'this is quuux v2')
52+
.addFile('/redirect-target.txt', 'this was a redirect')
5153
.addFile('/lazy/unchanged1.txt', 'this is unchanged (1)')
5254
.addFile('/lazy/unchanged2.txt', 'this is unchanged (2)')
5355
.addUnhashedFile('/unhashed/a.txt', 'this is unhashed v2', {'Cache-Control': 'max-age=10'})
@@ -265,23 +267,21 @@ const manifestUpdate: Manifest = {
265267
hashTable: tmpHashTableForFs(distUpdate),
266268
};
267269

268-
const serverBuilderBase =
269-
new MockServerStateBuilder()
270-
.withStaticFiles(dist)
271-
.withRedirect('/redirected.txt', '/redirect-target.txt', 'this was a redirect')
272-
.withError('/error.txt');
270+
const serverBuilderBase = new MockServerStateBuilder()
271+
.withStaticFiles(dist)
272+
.withRedirect('/redirected.txt', '/redirect-target.txt')
273+
.withError('/error.txt');
273274

274275
const server = serverBuilderBase.withManifest(manifest).build();
275276

276277
const serverRollback =
277278
serverBuilderBase.withManifest({...manifest, timestamp: manifest.timestamp + 1}).build();
278279

279-
const serverUpdate =
280-
new MockServerStateBuilder()
281-
.withStaticFiles(distUpdate)
282-
.withManifest(manifestUpdate)
283-
.withRedirect('/redirected.txt', '/redirect-target.txt', 'this was a redirect')
284-
.build();
280+
const serverUpdate = new MockServerStateBuilder()
281+
.withStaticFiles(distUpdate)
282+
.withManifest(manifestUpdate)
283+
.withRedirect('/redirected.txt', '/redirect-target.txt')
284+
.build();
285285

286286
const brokenServer =
287287
new MockServerStateBuilder().withStaticFiles(brokenFs).withManifest(brokenManifest).build();

packages/service-worker/worker/testing/mock.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,8 @@ export class MockServerStateBuilder {
104104
return this;
105105
}
106106

107-
withRedirect(from: string, to: string, toContents: string): MockServerStateBuilder {
108-
this.resources.set(from, new MockResponse(toContents, {redirected: true, url: to}));
109-
this.resources.set(to, new MockResponse(toContents));
107+
withRedirect(from: string, to: string): MockServerStateBuilder {
108+
this.resources.set(from, new MockResponse('', {redirected: true, url: to}));
110109
return this;
111110
}
112111

0 commit comments

Comments
 (0)
0