angular/packages/common/http/testing/test/request_spec.ts
JoostK a2cbffc06f 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
2022-02-01 21:41:43 +00:00

112 lines
3.2 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {HttpClient} from '@angular/common/http';
import {HttpClientTestingBackend} from '@angular/common/http/testing/src/backend';
describe('HttpClient TestRequest', () => {
it('accepts a null body', () => {
const mock = new HttpClientTestingBackend();
const client = new HttpClient(mock);
let resp: any;
client.post('/some-url', {test: 'test'}).subscribe(body => {
resp = body;
});
const req = mock.expectOne('/some-url');
req.flush(null);
expect(resp).toBeNull();
});
it('throws if no request matches', () => {
const mock = new HttpClientTestingBackend();
const client = new HttpClient(mock);
let resp: any;
client.get('/some-other-url').subscribe(body => {
resp = body;
});
try {
// expect different URL
mock.expectOne('/some-url').flush(null);
fail();
} catch (error) {
expect((error as Error).message)
.toBe(
'Expected one matching request for criteria "Match URL: /some-url", found none.' +
' Requests received are: GET /some-other-url.');
}
});
it('throws if no request matches the exact parameters', () => {
const mock = new HttpClientTestingBackend();
const client = new HttpClient(mock);
let resp: any;
const params = {query: 'hello'};
client.get('/some-url', {params}).subscribe(body => {
resp = body;
});
try {
// expect different query parameters
mock.expectOne('/some-url?query=world').flush(null);
fail();
} catch (error) {
expect((error as Error).message)
.toBe(
'Expected one matching request for criteria "Match URL: /some-url?query=world", found none.' +
' Requests received are: GET /some-url?query=hello.');
}
});
it('throws if no request matches with several requests received', () => {
const mock = new HttpClientTestingBackend();
const client = new HttpClient(mock);
let resp: any;
client.get('/some-other-url?query=world').subscribe(body => {
resp = body;
});
client.post('/and-another-url', {}).subscribe(body => {
resp = body;
});
try {
// expect different URL
mock.expectOne('/some-url').flush(null);
fail();
} catch (error) {
expect((error as Error).message)
.toBe(
'Expected one matching request for criteria "Match URL: /some-url", found none.' +
' 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');
}
});
});