Skip to content

Commit b4e4617

Browse files
JoostKthePunderWoman
authored andcommitted
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
1 parent 812c1ba commit b4e4617

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

packages/common/http/testing/src/backend.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,7 @@ export class HttpClientTestingBackend implements HttpBackend, HttpTestingControl
9595
let message = `Expected one matching request for criteria "${description}", found none.`;
9696
if (this.open.length > 0) {
9797
// Show the methods and URLs of open requests in the error, for convenience.
98-
const requests = this.open
99-
.map(testReq => {
100-
const url = testReq.request.urlWithParams;
101-
const method = testReq.request.method;
102-
return `${method} ${url}`;
103-
})
104-
.join(', ');
98+
const requests = this.open.map(describeRequest).join(', ');
10599
message += ` Requests received are: ${requests}.`;
106100
}
107101
throw new Error(message);
@@ -135,12 +129,7 @@ export class HttpClientTestingBackend implements HttpBackend, HttpTestingControl
135129
}
136130
if (open.length > 0) {
137131
// Show the methods and URLs of open requests in the error, for convenience.
138-
const requests = open.map(testReq => {
139-
const url = testReq.request.urlWithParams.split('?')[0];
140-
const method = testReq.request.method;
141-
return `${method} ${url}`;
142-
})
143-
.join(', ');
132+
const requests = open.map(describeRequest).join(', ');
144133
throw new Error(`Expected no open requests, found ${open.length}: ${requests}`);
145134
}
146135
}
@@ -158,3 +147,9 @@ export class HttpClientTestingBackend implements HttpBackend, HttpTestingControl
158147
}
159148
}
160149
}
150+
151+
function describeRequest(testRequest: TestRequest): string {
152+
const url = testRequest.request.urlWithParams;
153+
const method = testRequest.request.method;
154+
return `${method} ${url}`;
155+
}

packages/common/http/testing/test/request_spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,22 @@ describe('HttpClient TestRequest', () => {
9191
' Requests received are: GET /some-other-url?query=world, POST /and-another-url.');
9292
}
9393
});
94+
95+
it('throws if there are open requests when verify is called', () => {
96+
const mock = new HttpClientTestingBackend();
97+
const client = new HttpClient(mock);
98+
99+
client.get('/some-other-url?query=world').subscribe();
100+
client.post('/and-another-url', {}).subscribe();
101+
102+
try {
103+
mock.verify();
104+
fail();
105+
} catch (error) {
106+
expect(error.message)
107+
.toBe(
108+
'Expected no open requests, found 2:' +
109+
' GET /some-other-url?query=world, POST /and-another-url');
110+
}
111+
});
94112
});

0 commit comments

Comments
 (0)