Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix(http): adding complete callback #88

Merged
merged 1 commit into from
Sep 28, 2016
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
124 changes: 124 additions & 0 deletions src/platform/http/http-interceptor.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import {
TestBed,
inject,
async,
} from '@angular/core/testing';
import { Injector } from '@angular/core';
import { XHRBackend, Response, ResponseOptions } from '@angular/http';
import { MockBackend } from '@angular/http/testing';
import { HttpModule, Http } from '@angular/http';
import { HttpInterceptorService } from './http-interceptor.service';
import 'rxjs/Rx';

describe('Service: HttpInterceptor', () => {

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
HttpModule,
],
providers: [
MockBackend, {
provide: XHRBackend,
useExisting: MockBackend,
}, {
provide: HttpInterceptorService,
useFactory: (http: Http, injector: Injector): HttpInterceptorService => {
return new HttpInterceptorService(http, injector, []);
},
deps: [Http, Injector],
},
],
});
}));

it('expect to do a post succesfully with observables',
async(inject([HttpInterceptorService, MockBackend], (service: HttpInterceptorService, mockBackend: MockBackend) => {
mockBackend.connections.subscribe((connection: any) => {
connection.mockRespond(new Response(new ResponseOptions({
status: 200,
body: JSON.stringify('success')}
)));
});
let success: boolean = false;
let error: boolean = false;
let complete: boolean = false;
service.post('testurl', {}).map((res: Response) => res.json()).subscribe((data: string) => {
expect(data).toBe('success');
success = true;
}, () => {
error = true;
}, () => {
complete = true;
});
expect(success).toBe(true, 'on success didnt execute with observables');
expect(error).toBe(false, 'on error executed when it shouldnt have with observables');
expect(complete).toBe(true, 'on complete didnt execute with observables');
})
));

it('expect to do a post failure with observables',
async(inject([HttpInterceptorService, MockBackend], (service: HttpInterceptorService, mockBackend: MockBackend) => {
mockBackend.connections.subscribe((connection: any) => {
connection.mockError(new Error('error'));
});
let success: boolean = false;
let error: boolean = false;
let complete: boolean = false;
service.post('testurl', {}).map((res: Response) => res.json()).subscribe(() => {
success = true;
}, (err: Error) => {
expect(err.message).toBe('error');
error = true;
}, () => {
complete = true;
});
expect(success).toBe(false, 'on success execute when it shouldnt have with observables');
expect(error).toBe(true, 'on error didnt execute with observables');
expect(complete).toBe(false, 'on complete execute when it shouldnt have with observables');
})
));

it('expect to do a post succesfully with promises',
async(inject([HttpInterceptorService, MockBackend], (service: HttpInterceptorService, mockBackend: MockBackend) => {
mockBackend.connections.subscribe((connection: any) => {
connection.mockRespond(new Response(new ResponseOptions({
status: 200,
body: JSON.stringify('success')}
)));
});
let success: boolean = false;
let error: boolean = false;
service.post('testurl', {}).map((res: Response) => res.json()).toPromise().then((data: string) => {
expect(data).toBe('success');
success = true;
}, () => {
error = true;
});
setTimeout(() => {
expect(success).toBe(true, 'on success didnt execute with promises');
expect(error).toBe(false, 'on error executed when it shouldnt have with promises');
});
})
));

it('expect to do a post failure with promises',
async(inject([HttpInterceptorService, MockBackend], (service: HttpInterceptorService, mockBackend: MockBackend) => {
mockBackend.connections.subscribe((connection: any) => {
connection.mockError(new Error('error'));
});
let success: boolean = false;
let error: boolean = false;
service.post('testurl', {}).map((res: Response) => res.json()).toPromise().then(() => {
success = true;
}, (err: Error) => {
expect(err.message).toBe('error');
error = true;
});
setTimeout(() => {
expect(success).toBe(false, 'on success execute when it shouldnt have with promises');
expect(error).toBe(true, 'on error didnt execute with promises');
});
})
));
});
1 change: 1 addition & 0 deletions src/platform/http/http-interceptor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export class HttpInterceptorService {
return new Observable<any>((subscriber: Subscriber<any>) => {
responseObservable.do((response: Response) => {
subscriber.next(this._responseResolve(response));
subscriber.complete();
}).catch((error: Response) => {
return new Observable<any>(() => {
subscriber.error(this._responseErrorResolve(error));
Expand Down