Skip to content

Commit

Permalink
fix(keepalive): return actual response or error in onPingResponse
Browse files Browse the repository at this point in the history
fixes #118
  • Loading branch information
mmubasher authored and grbsk committed Mar 12, 2019
1 parent f3e1e17 commit 0741ae2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
41 changes: 38 additions & 3 deletions modules/keepalive/src/keepalive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,13 @@ describe('keepalive/Keepalive', () => {
it('ping() should fire request and emit onPingResponse event', () => {
let actualResponse: HttpResponse<{}>;

instance.onPingResponse.subscribe((response: HttpResponse<{}>) => {
actualResponse = response;
});
instance.onPingResponse.subscribe(
(response: HttpResponse<{}>) => {
actualResponse = response;
},
(error: HttpResponse<{}>) => {
actualResponse = error;
});

instance.ping();

Expand All @@ -117,6 +121,37 @@ describe('keepalive/Keepalive', () => {
});
});

describe('using an HTTP request that results in error', () => {

let request = new HttpRequest('GET', 'https://test.com/404');

beforeEach(() => {
instance = TestBed.get(Keepalive);
instance.request(request);
});

afterEach(() => {
httpMock.verify();
});

it('ping() should fire request and emit onPingResponse event', () => {
let actualResponse: HttpResponse<{}>;

instance.onPingResponse.subscribe(
(response: HttpResponse<{}>) => {
actualResponse = response;
},
(error: HttpResponse<{}>) => {
actualResponse = error;
});

instance.ping();

httpMock.expectOne(request.url).flush(null, {status: 404, statusText: 'Error'});
expect(actualResponse.status).toBe(404);
});
});

describe('on an interval', () => {
beforeEach(() => {
instance.interval(5);
Expand Down
17 changes: 11 additions & 6 deletions modules/keepalive/src/keepalive.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {EventEmitter, Injectable, OnDestroy} from '@angular/core';
import {HttpClient, HttpRequest, HttpResponse} from '@angular/common/http';
import {KeepaliveSvc} from '@ng-idle/core';
import { EventEmitter, Injectable, OnDestroy } from '@angular/core';
import { HttpClient, HttpRequest, HttpResponse } from '@angular/common/http';
import { KeepaliveSvc } from '@ng-idle/core';


/**
Expand Down Expand Up @@ -70,9 +70,14 @@ export class Keepalive extends KeepaliveSvc implements OnDestroy {
ping(): void {
this.onPing.emit(null);
if (this.pingRequest) {
this.http.request(this.pingRequest).subscribe((response: HttpResponse<any>) => {
this.onPingResponse.emit(response);
});
this.http.request(this.pingRequest).subscribe(
(response: HttpResponse<any>) => {
this.onPingResponse.emit(response);
},
(error: HttpResponse<any>) => {
this.onPingResponse.emit(error);
}
);
}
}

Expand Down

0 comments on commit 0741ae2

Please sign in to comment.