Skip to content
This repository was archived by the owner on May 7, 2018. It is now read-only.

Commit 1ad28d1

Browse files
committed
fix: HttpParams and HttpHeaders are immutable!
1 parent 82c0a2f commit 1ad28d1

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/app/http-client/http-client-feature.service.spec.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { TestBed, async, inject } from '@angular/core/testing';
2-
import { HttpClientModule, HttpRequest } from '@angular/common/http';
2+
import { HttpClientModule, HttpRequest, HttpParams } from '@angular/common/http';
33
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
44
import { HttpClientFeatureService } from './http-client-feature.service';
55

@@ -26,10 +26,14 @@ describe(`HttpClientFeatureService`, () => {
2626
service.login('foo', 'bar').subscribe();
2727

2828
backend.expectOne((req: HttpRequest<any>) => {
29-
return req.url === 'auth/login' &&
30-
req.method === 'POST' &&
31-
req.headers.get('Content-Type') === 'application/x-www-form-urlencoded';
32-
}, 'Login Request');
29+
const body = new HttpParams({ fromString: req.body });
30+
31+
return req.url === 'auth/login'
32+
&& req.method === 'POST'
33+
&& req.headers.get('Content-Type') === 'application/x-www-form-urlencoded'
34+
&& body.get('user') === 'foo'
35+
&& body.get('password') === 'bar';
36+
}, `POST to 'auth/login' with form-encoded user and password`);
3337
})));
3438

3539
it(`should emit 'false' for 401 Unauthorized`, async(inject([HttpClientFeatureService, HttpTestingController],

src/app/http-client/http-client-feature.service.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ export class HttpClientFeatureService {
1111
) {}
1212

1313
login(user: string, password: string): Observable<boolean> {
14-
const body = new HttpParams();
15-
body.set(`user`, user);
16-
body.set(`password`, password);
14+
const body = new HttpParams()
15+
.set(`user`, user)
16+
.set(`password`, password);
1717
const headers = new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded' });
1818

19-
return this.http.post(`auth/login`, body.toString, { headers, observe: 'response' })
19+
return this.http.post(`auth/login`, body.toString(), { headers, observe: 'response' })
2020
.map((res: HttpResponse<Object>) => res.ok)
2121
.catch((err: any) => Observable.of(false));
2222
}

0 commit comments

Comments
 (0)