From ffc912cf067f87cf841821820199d2c5f8bbe874 Mon Sep 17 00:00:00 2001 From: David Date: Sun, 27 Aug 2017 08:48:01 +0200 Subject: [PATCH] fix: HttpParams and HttpHeaders are immutable! --- .../http-client-feature.service.spec.ts | 14 +++++++++----- src/app/http-client/http-client-feature.service.ts | 8 ++++---- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/app/http-client/http-client-feature.service.spec.ts b/src/app/http-client/http-client-feature.service.spec.ts index 5d7d008..02eb5fa 100644 --- a/src/app/http-client/http-client-feature.service.spec.ts +++ b/src/app/http-client/http-client-feature.service.spec.ts @@ -1,5 +1,5 @@ import { TestBed, async, inject } from '@angular/core/testing'; -import { HttpClientModule, HttpRequest } from '@angular/common/http'; +import { HttpClientModule, HttpRequest, HttpParams } from '@angular/common/http'; import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; import { HttpClientFeatureService } from './http-client-feature.service'; @@ -26,10 +26,14 @@ describe(`HttpClientFeatureService`, () => { service.login('foo', 'bar').subscribe(); backend.expectOne((req: HttpRequest) => { - return req.url === 'auth/login' && - req.method === 'POST' && - req.headers.get('Content-Type') === 'application/x-www-form-urlencoded'; - }, 'Login Request'); + const body = new HttpParams({ fromString: req.body }); + + return req.url === 'auth/login' + && req.method === 'POST' + && req.headers.get('Content-Type') === 'application/x-www-form-urlencoded' + && body.get('user') === 'foo' + && body.get('password') === 'bar'; + }, `POST to 'auth/login' with form-encoded user and password`); }))); it(`should emit 'false' for 401 Unauthorized`, async(inject([HttpClientFeatureService, HttpTestingController], diff --git a/src/app/http-client/http-client-feature.service.ts b/src/app/http-client/http-client-feature.service.ts index 4a7ebb6..3de096e 100644 --- a/src/app/http-client/http-client-feature.service.ts +++ b/src/app/http-client/http-client-feature.service.ts @@ -11,12 +11,12 @@ export class HttpClientFeatureService { ) {} login(user: string, password: string): Observable { - const body = new HttpParams(); - body.set(`user`, user); - body.set(`password`, password); + const body = new HttpParams() + .set(`user`, user) + .set(`password`, password); const headers = new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded' }); - return this.http.post(`auth/login`, body.toString, { headers, observe: 'response' }) + return this.http.post(`auth/login`, body.toString(), { headers, observe: 'response' }) .map((res: HttpResponse) => res.ok) .catch((err: any) => Observable.of(false)); }