From a2cbffc06f25ea1cb8f1c8aef658f7c213621a07 Mon Sep 17 00:00:00 2001 From: JoostK Date: Mon, 31 Jan 2022 21:15:42 +0100 Subject: [PATCH] fix(common): include query parameters for open HTTP requests in `verify` (#44917) When `HttpTestingController.verify` is used to verify that there are not open, unexpected requests it would throw an error with the method and URL of all pending requests, excluding the query parameters. This is confusing, as e.g. `expectOne` matches a URL including its query parameters and `expectOne` does include the query parameters when it reports when no request could be matched. This commit changes the error that is reported by `verify` to include the query parameters. Closes #19974 PR Close #44917 --- packages/common/http/testing/src/backend.ts | 21 +++++++------------ .../common/http/testing/test/request_spec.ts | 18 ++++++++++++++++ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/packages/common/http/testing/src/backend.ts b/packages/common/http/testing/src/backend.ts index b157027e4fb..06fb2d8305d 100644 --- a/packages/common/http/testing/src/backend.ts +++ b/packages/common/http/testing/src/backend.ts @@ -95,13 +95,7 @@ export class HttpClientTestingBackend implements HttpBackend, HttpTestingControl let message = `Expected one matching request for criteria "${description}", found none.`; if (this.open.length > 0) { // Show the methods and URLs of open requests in the error, for convenience. - const requests = this.open - .map(testReq => { - const url = testReq.request.urlWithParams; - const method = testReq.request.method; - return `${method} ${url}`; - }) - .join(', '); + const requests = this.open.map(describeRequest).join(', '); message += ` Requests received are: ${requests}.`; } throw new Error(message); @@ -135,12 +129,7 @@ export class HttpClientTestingBackend implements HttpBackend, HttpTestingControl } if (open.length > 0) { // Show the methods and URLs of open requests in the error, for convenience. - const requests = open.map(testReq => { - const url = testReq.request.urlWithParams.split('?')[0]; - const method = testReq.request.method; - return `${method} ${url}`; - }) - .join(', '); + const requests = open.map(describeRequest).join(', '); throw new Error(`Expected no open requests, found ${open.length}: ${requests}`); } } @@ -158,3 +147,9 @@ export class HttpClientTestingBackend implements HttpBackend, HttpTestingControl } } } + +function describeRequest(testRequest: TestRequest): string { + const url = testRequest.request.urlWithParams; + const method = testRequest.request.method; + return `${method} ${url}`; +} diff --git a/packages/common/http/testing/test/request_spec.ts b/packages/common/http/testing/test/request_spec.ts index 4c468297bd2..3c6b3f0c4f0 100644 --- a/packages/common/http/testing/test/request_spec.ts +++ b/packages/common/http/testing/test/request_spec.ts @@ -91,4 +91,22 @@ describe('HttpClient TestRequest', () => { ' Requests received are: GET /some-other-url?query=world, POST /and-another-url.'); } }); + + it('throws if there are open requests when verify is called', () => { + const mock = new HttpClientTestingBackend(); + const client = new HttpClient(mock); + + client.get('/some-other-url?query=world').subscribe(); + client.post('/and-another-url', {}).subscribe(); + + try { + mock.verify(); + fail(); + } catch (error) { + expect(error.message) + .toBe( + 'Expected no open requests, found 2:' + + ' GET /some-other-url?query=world, POST /and-another-url'); + } + }); });