mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
test(service-worker): make MockResponse behave more similarly to real responses (#44723)
Make the `MockResponse` class used in tests behave more similarly to how real responses work by making the following improvements: - Use an empty `statusText` (`''`) when provided, instead of the default `'OK'` value. This allows better representing opaque responses. - Preserve more properties (`redirected`, `type`, `url`) when cloning a `MockResponse`. PR Close #44723
This commit is contained in:
parent
2f1457d12f
commit
bfcc69d8c0
2 changed files with 15 additions and 3 deletions
|
|
@ -362,6 +362,7 @@ describe('Driver', () => {
|
|||
server.assertSawRequestFor('/foo.txt');
|
||||
server.assertSawRequestFor('/bar.txt');
|
||||
server.assertSawRequestFor('/redirected.txt');
|
||||
server.assertSawRequestFor('/redirect-target.txt');
|
||||
expect(await makeRequest(scope, '/foo.txt')).toEqual('this is foo');
|
||||
expect(await makeRequest(scope, '/bar.txt')).toEqual('this is bar');
|
||||
server.assertNoOtherRequests();
|
||||
|
|
@ -374,6 +375,7 @@ describe('Driver', () => {
|
|||
server.assertSawRequestFor('/foo.txt');
|
||||
server.assertSawRequestFor('/bar.txt');
|
||||
server.assertSawRequestFor('/redirected.txt');
|
||||
server.assertSawRequestFor('/redirect-target.txt');
|
||||
expect(await makeRequest(scope, '/foo.txt')).toEqual('this is foo');
|
||||
expect(await makeRequest(scope, '/bar.txt')).toEqual('this is bar');
|
||||
server.assertNoOtherRequests();
|
||||
|
|
@ -391,6 +393,7 @@ describe('Driver', () => {
|
|||
server.assertSawRequestFor('/foo.txt');
|
||||
server.assertSawRequestFor('/bar.txt');
|
||||
server.assertSawRequestFor('/redirected.txt');
|
||||
server.assertSawRequestFor('/redirect-target.txt');
|
||||
|
||||
// Once initialized, cached resources are served without network requests.
|
||||
expect(await makeRequest(scope, '/foo.txt')).toEqual('this is foo');
|
||||
|
|
@ -411,6 +414,7 @@ describe('Driver', () => {
|
|||
server.assertSawRequestFor('/foo.txt');
|
||||
server.assertSawRequestFor('/bar.txt');
|
||||
server.assertSawRequestFor('/redirected.txt');
|
||||
server.assertSawRequestFor('/redirect-target.txt');
|
||||
|
||||
// Once initialized, pushed messages are handled without re-initializing.
|
||||
await scope.handleMessage({action: 'bar'}, 'someClient');
|
||||
|
|
@ -476,6 +480,7 @@ describe('Driver', () => {
|
|||
serverUpdate.assertSawRequestFor('/ngsw.json');
|
||||
serverUpdate.assertSawRequestFor('/foo.txt');
|
||||
serverUpdate.assertSawRequestFor('/redirected.txt');
|
||||
serverUpdate.assertSawRequestFor('/redirect-target.txt');
|
||||
serverUpdate.assertNoOtherRequests();
|
||||
|
||||
expect(client.messages).toEqual([
|
||||
|
|
@ -577,6 +582,7 @@ describe('Driver', () => {
|
|||
serverUpdate.assertSawRequestFor('/ngsw.json');
|
||||
serverUpdate.assertSawRequestFor('/foo.txt');
|
||||
serverUpdate.assertSawRequestFor('/redirected.txt');
|
||||
serverUpdate.assertSawRequestFor('/redirect-target.txt');
|
||||
serverUpdate.assertNoOtherRequests();
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -171,7 +171,7 @@ export class MockResponse extends MockBody implements Response {
|
|||
init: ResponseInit&{type?: ResponseType, redirected?: boolean, url?: string} = {}) {
|
||||
super(typeof body === 'string' ? body : null);
|
||||
this.status = (init.status !== undefined) ? init.status : 200;
|
||||
this.statusText = init.statusText || 'OK';
|
||||
this.statusText = (init.statusText !== undefined) ? init.statusText : 'OK';
|
||||
const headers = init.headers as {[key: string]: string};
|
||||
if (headers !== undefined) {
|
||||
if (headers instanceof MockHeaders) {
|
||||
|
|
@ -197,7 +197,13 @@ export class MockResponse extends MockBody implements Response {
|
|||
if (this.bodyUsed) {
|
||||
throw 'Body already consumed';
|
||||
}
|
||||
return new MockResponse(
|
||||
this._body, {status: this.status, statusText: this.statusText, headers: this.headers});
|
||||
return new MockResponse(this._body, {
|
||||
status: this.status,
|
||||
statusText: this.statusText,
|
||||
headers: this.headers,
|
||||
type: this.type,
|
||||
redirected: this.redirected,
|
||||
url: this.url,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue