Skip to content
This repository was archived by the owner on May 7, 2018. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/app/http-client/http-client-feature.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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';

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

backend.expectOne((req: HttpRequest<any>) => {
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],
Expand Down
8 changes: 4 additions & 4 deletions src/app/http-client/http-client-feature.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ export class HttpClientFeatureService {
) {}

login(user: string, password: string): Observable<boolean> {
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<Object>) => res.ok)
.catch((err: any) => Observable.of(false));
}
Expand Down