Commit ad6340ed authored by Maxime REYROLLE's avatar Maxime REYROLLE

rework authentication test with HttpClient

parent 67d9b3fd
import {TestBed, inject, fakeAsync, tick} from '@angular/core/testing';
import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing';
import {BaseRequestOptions, Http, RequestMethod, ResponseOptions, Response} from '@angular/http';
import {MockBackend} from '@angular/http/testing';
import {encodeTestToken} from 'angular2-jwt/angular2-jwt-test-helpers';
......@@ -18,7 +19,10 @@ describe('AuthenticationService', () => {
},
MockBackend,
BaseRequestOptions
]
],
imports: [
HttpClientTestingModule,
],
});
});
......@@ -28,9 +32,8 @@ describe('AuthenticationService', () => {
describe('authenticate()', () => {
it('should authenticate a user', inject([AuthenticationService, MockBackend],
fakeAsync((authenticationService: AuthenticationService, mockBackend: MockBackend) => {
let result;
it('should authenticate a user', inject([AuthenticationService, HttpTestingController],
(authenticationService: AuthenticationService, httpMock: HttpTestingController) => {
const expectedBody = {
token: encodeTestToken({'sub': 2, 'name': 'mreyrolle', 'exp': 9999999999}),
user: {
......@@ -43,21 +46,26 @@ describe('AuthenticationService', () => {
}
};
mockBackend.connections.subscribe(connection => {
expect(connection.request.method).toBe(RequestMethod.Get);
expect(connection.request.url).toBe('https://cake.test.adullact.org/api/v1/users/token.json');
const options = new ResponseOptions({status: 200, body: expectedBody});
connection.mockRespond(new Response(options));
// An HTTP GET request is made in the authenticate() method, expect that the authenticate() method return true.
authenticationService.authenticate().subscribe(data => {
expect(data).toBeTruthy();
});
authenticationService.authenticate().subscribe(body => {
result = body;
});
// The request is pending, and no response has been sent. Expect that the request happened.
const req = httpMock.expectOne('https://cake.test.adullact.org/api/v1/users/token.json');
tick();
// If no request with that URL was made, or if multiple requests match, expectOne() would throw.
// However this test makes only one request to this URL, so it will match and return a mock request.
// The mock request can be used to deliver a response or make assertions against the request.
// In this case, the test asserts that the request is a GET.
expect(req.request.method).toEqual('GET');
expect(result).toBeTruthy();
})));
// Fulfill the request by transmitting a response.
req.flush(expectedBody);
// Assert that there are no outstanding requests.
httpMock.verify();
}));
it('should not authenticate a user', inject([AuthenticationService, MockBackend],
fakeAsync((authenticationService: AuthenticationService, mockBackend: MockBackend) => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment