mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-17 06:53:03 +00:00
DSpaceRESTv2Service gets response with payload and statusCode
This commit is contained in:
@@ -6,7 +6,8 @@ import { DSpaceRESTv2Service } from './dspace-rest-v2.service';
|
||||
describe('DSpaceRESTv2Service', () => {
|
||||
let dSpaceRESTv2Service: DSpaceRESTv2Service;
|
||||
let httpMock: HttpTestingController;
|
||||
const mockDSpaceRESTV2Response = {};
|
||||
const url = 'http://www.dspace.org/';
|
||||
const mockError = new ErrorEvent('test error');
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
@@ -18,20 +19,50 @@ describe('DSpaceRESTv2Service', () => {
|
||||
httpMock = TestBed.get(HttpTestingController);
|
||||
});
|
||||
|
||||
afterEach(() => httpMock.verify());
|
||||
|
||||
it('should be created', inject([DSpaceRESTv2Service], (service: DSpaceRESTv2Service) => {
|
||||
expect(service).toBeTruthy();
|
||||
}));
|
||||
|
||||
describe('#get', () => {
|
||||
it('should return an Observable<DSpaceRESTV2Response>', () => {
|
||||
const url = 'http://www.dspace.org/';
|
||||
const mockPayload = {
|
||||
page: 1
|
||||
};
|
||||
const mockStatusCode = 'GREAT';
|
||||
|
||||
dSpaceRESTv2Service.get(url).subscribe((response) => {
|
||||
expect(response).toBeTruthy();
|
||||
expect(response.statusCode).toEqual(mockStatusCode);
|
||||
expect(response.payload.page).toEqual(mockPayload.page);
|
||||
});
|
||||
|
||||
const req = httpMock.expectOne(url);
|
||||
expect(req.request.method).toBe('GET');
|
||||
req.flush(mockDSpaceRESTV2Response);
|
||||
req.flush(mockPayload, { statusText: mockStatusCode});
|
||||
});
|
||||
});
|
||||
|
||||
it('should throw an error', () => {
|
||||
dSpaceRESTv2Service.get(url).subscribe(() => undefined, (err) => {
|
||||
expect(err.error).toBe(mockError);
|
||||
});
|
||||
|
||||
const req = httpMock.expectOne(url);
|
||||
expect(req.request.method).toBe('GET');
|
||||
req.error(mockError);
|
||||
});
|
||||
|
||||
it('should log an error', () => {
|
||||
spyOn(console, 'log');
|
||||
|
||||
dSpaceRESTv2Service.get(url).subscribe(() => undefined, (err) => {
|
||||
expect(console.log).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
const req = httpMock.expectOne(url);
|
||||
expect(req.request.method).toBe('GET');
|
||||
req.error(mockError);
|
||||
});
|
||||
});
|
||||
|
@@ -1,11 +1,9 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { HttpClient, HttpResponse } from '@angular/common/http';
|
||||
import { Observable } from 'rxjs/Observable';
|
||||
|
||||
import { DSpaceRESTV2Response } from './dspace-rest-v2-response.model';
|
||||
|
||||
import { GLOBAL_CONFIG, GlobalConfig } from '../../../config';
|
||||
|
||||
/**
|
||||
* Service to access DSpace's REST API
|
||||
*/
|
||||
@@ -26,8 +24,9 @@ export class DSpaceRESTv2Service {
|
||||
* @return {Observable<DSpaceRESTV2Response>}
|
||||
* An Observable<DSpaceRESTV2Response> containing the response from the server
|
||||
*/
|
||||
get(absoluteURL: string, options?: {}): Observable<DSpaceRESTV2Response> {
|
||||
return this.http.get(absoluteURL, options)
|
||||
get(absoluteURL: string): Observable<DSpaceRESTV2Response> {
|
||||
return this.http.get(absoluteURL, { observe: 'response' })
|
||||
.map((res: HttpResponse<any>) => ({ payload: res.body, statusCode: res.statusText }))
|
||||
.catch((err) => {
|
||||
console.log('Error: ', err);
|
||||
return Observable.throw(err);
|
||||
|
Reference in New Issue
Block a user