| 
									
										
										
										
											2018-01-09 08:25:01 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							| 
									
										
										
										
											2020-05-19 12:08:49 -07:00
										 |  |  |  * Copyright Google LLC All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2018-01-09 08:25:01 -08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * 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
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-30 16:56:23 -08:00
										 |  |  | import {HttpClient} from '@angular/common/http'; | 
					
						
							|  |  |  | import {HttpClientTestingBackend} from '@angular/common/http/testing/src/backend'; | 
					
						
							| 
									
										
										
										
											2018-01-09 08:25:01 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe('HttpClient TestRequest', () => { | 
					
						
							|  |  |  |   it('accepts a null body', () => { | 
					
						
							|  |  |  |     const mock = new HttpClientTestingBackend(); | 
					
						
							|  |  |  |     const client = new HttpClient(mock); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let resp: any; | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |     client.post('/some-url', {test: 'test'}).subscribe(body => { | 
					
						
							|  |  |  |       resp = body; | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2018-01-09 08:25:01 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const req = mock.expectOne('/some-url'); | 
					
						
							|  |  |  |     req.flush(null); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(resp).toBeNull(); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2018-11-08 16:34:37 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   it('throws if no request matches', () => { | 
					
						
							|  |  |  |     const mock = new HttpClientTestingBackend(); | 
					
						
							|  |  |  |     const client = new HttpClient(mock); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let resp: any; | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |     client.get('/some-other-url').subscribe(body => { | 
					
						
							|  |  |  |       resp = body; | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2018-11-08 16:34:37 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       // expect different URL
 | 
					
						
							|  |  |  |       mock.expectOne('/some-url').flush(null); | 
					
						
							|  |  |  |       fail(); | 
					
						
							|  |  |  |     } catch (error) { | 
					
						
							|  |  |  |       expect(error.message) | 
					
						
							| 
									
										
										
										
											2018-11-08 16:25:29 +01:00
										 |  |  |           .toBe( | 
					
						
							|  |  |  |               'Expected one matching request for criteria "Match URL: /some-url", found none.' + | 
					
						
							|  |  |  |               ' Requests received are: GET /some-other-url.'); | 
					
						
							| 
									
										
										
										
											2018-11-08 16:34:37 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   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'}; | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |     client.get('/some-url', {params}).subscribe(body => { | 
					
						
							|  |  |  |       resp = body; | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2018-11-08 16:34:37 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       // expect different query parameters
 | 
					
						
							|  |  |  |       mock.expectOne('/some-url?query=world').flush(null); | 
					
						
							|  |  |  |       fail(); | 
					
						
							|  |  |  |     } catch (error) { | 
					
						
							|  |  |  |       expect(error.message) | 
					
						
							|  |  |  |           .toBe( | 
					
						
							| 
									
										
										
										
											2018-11-08 16:25:29 +01:00
										 |  |  |               'Expected one matching request for criteria "Match URL: /some-url?query=world", found none.' + | 
					
						
							|  |  |  |               ' Requests received are: GET /some-url?query=hello.'); | 
					
						
							| 
									
										
										
										
											2018-11-08 16:34:37 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-01-22 08:32:56 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   it('throws if no request matches with several requests received', () => { | 
					
						
							|  |  |  |     const mock = new HttpClientTestingBackend(); | 
					
						
							|  |  |  |     const client = new HttpClient(mock); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let resp: any; | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |     client.get('/some-other-url?query=world').subscribe(body => { | 
					
						
							|  |  |  |       resp = body; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     client.post('/and-another-url', {}).subscribe(body => { | 
					
						
							|  |  |  |       resp = body; | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-01-22 08:32:56 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       // expect different URL
 | 
					
						
							|  |  |  |       mock.expectOne('/some-url').flush(null); | 
					
						
							|  |  |  |       fail(); | 
					
						
							|  |  |  |     } catch (error) { | 
					
						
							|  |  |  |       expect(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.'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2018-07-05 18:10:09 +03:00
										 |  |  | }); |