Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/service-worker/worker/src/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ export abstract class AssetGroup {
* Create a new `Request` based on the specified URL and `RequestInit` options, preserving only
* metadata that are known to be safe.
*
* Currently, only headers are preserved.
* Currently, only headers and redirect policy are preserved.
*
* NOTE:
* Things like credential inclusion are intentionally omitted to avoid issues with opaque
Expand All @@ -512,7 +512,10 @@ export abstract class AssetGroup {
* https://github.com/angular/angular/issues/41931#issuecomment-1227601347
*/
private newRequestWithMetadata(url: string, options: RequestInit): Request {
return this.adapter.newRequest(url, {headers: options.headers});
return this.adapter.newRequest(url, {
headers: options.headers,
redirect: options.redirect,
});
}

/**
Expand Down
6 changes: 6 additions & 0 deletions packages/service-worker/worker/test/happy_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1686,6 +1686,12 @@ import {envIsSupported} from '../testing/utils';
expect(redirectReq.mode).toBe('cors'); // The default value.
expect((redirectReq as any).unknownOption).toBeUndefined();
});

it('does not follow redirects when redirect policy is error', async () => {
await expectAsync(
makeRequest(scope, '/lazy/redirected.txt', undefined, {redirect: 'error'}),
).toBeRejected();
});
});
});

Expand Down
6 changes: 5 additions & 1 deletion packages/service-worker/worker/testing/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class MockRequest extends MockBody implements Request {
readonly keepalive: boolean = true;
readonly method: string = 'GET';
readonly mode: RequestMode = 'cors';
readonly redirect: RequestRedirect = 'error';
readonly redirect: RequestRedirect = 'follow';
readonly referrer: string = '';
readonly referrerPolicy: ReferrerPolicy = 'no-referrer';
readonly signal: AbortSignal = null as any;
Expand Down Expand Up @@ -153,6 +153,9 @@ export class MockRequest extends MockBody implements Request {
if (init.method !== undefined) {
this.method = init.method;
}
if (init.redirect !== undefined) {
this.redirect = init.redirect;
}
if (init.destination !== undefined) {
this.destination = init.destination;
}
Expand All @@ -167,6 +170,7 @@ export class MockRequest extends MockBody implements Request {
mode: this.mode,
credentials: this.credentials,
headers: this.headers,
redirect: this.redirect,
});
}
}
Expand Down
6 changes: 5 additions & 1 deletion packages/service-worker/worker/testing/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@ export class MockServerState {
}
const url = req.url.split('?')[0];
if (this.resources.has(url)) {
return this.resources.get(url)!.clone();
const response = this.resources.get(url)!.clone();
if (response.redirected && req.redirect === 'error') {
throw new Error('Redirect disallowed by request policy.');
}
return response;
}
if (this.errors.has(url)) {
throw new Error('Intentional failure!');
Expand Down
Loading