diff --git a/src/app/core/dspace-rest-v2/dspace-rest-v2.service.spec.ts b/src/app/core/dspace-rest-v2/dspace-rest-v2.service.spec.ts index 18b9090844..5cb2fb459e 100644 --- a/src/app/core/dspace-rest-v2/dspace-rest-v2.service.spec.ts +++ b/src/app/core/dspace-rest-v2/dspace-rest-v2.service.spec.ts @@ -3,6 +3,8 @@ import { HttpClientTestingModule, HttpTestingController } from '@angular/common/ import { DSpaceRESTv2Service } from './dspace-rest-v2.service'; import { DSpaceObject } from '../shared/dspace-object.model'; +import { RestRequestMethod } from '../data/rest-request-method'; +import { HttpHeaders } from '@angular/common/http'; describe('DSpaceRESTv2Service', () => { let dSpaceRESTv2Service: DSpaceRESTv2Service; @@ -47,29 +49,71 @@ describe('DSpaceRESTv2Service', () => { const req = httpMock.expectOne(url); expect(req.request.method).toBe('GET'); - req.flush(mockPayload, { status: mockStatusCode, statusText: mockStatusText}); + req.flush(mockPayload, { status: mockStatusCode, statusText: mockStatusText }); + }); + it('should throw an error', () => { + dSpaceRESTv2Service.get(url).subscribe(() => undefined, (err) => { + expect(err).toEqual(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); + }); + + it('when no content-type header is provided, it should use application/json', () => { + dSpaceRESTv2Service.request(RestRequestMethod.POST, url, {}).subscribe(); + + const req = httpMock.expectOne(url); + expect(req.request.headers.get('Content-Type')).toContain('application/json; charset=utf-8'); }); }); - it('should throw an error', () => { - dSpaceRESTv2Service.get(url).subscribe(() => undefined, (err) => { - expect(err).toEqual(mockError); - }); - const req = httpMock.expectOne(url); - expect(req.request.method).toBe('GET'); - req.error(mockError); - }); + describe('#request', () => { + it('should return an Observable', () => { + const mockPayload = { + page: 1 + }; + const mockStatusCode = 200; + const mockStatusText = 'GREAT'; - it('should log an error', () => { - spyOn(console, 'log'); + dSpaceRESTv2Service.request(RestRequestMethod.POST, url, {}).subscribe((response) => { + expect(response).toBeTruthy(); + expect(response.statusCode).toEqual(mockStatusCode); + expect(response.statusText).toEqual(mockStatusText); + expect(response.payload.page).toEqual(mockPayload.page); + }); - dSpaceRESTv2Service.get(url).subscribe(() => undefined, (err) => { - expect(console.log).toHaveBeenCalled(); + const req = httpMock.expectOne(url); + expect(req.request.method).toBe('POST'); + req.flush(mockPayload, { status: mockStatusCode, statusText: mockStatusText }); }); - const req = httpMock.expectOne(url); - expect(req.request.method).toBe('GET'); - req.error(mockError); + it('when a content-type header is provided, it should not use application/json', () => { + const headers = new HttpHeaders({'Content-Type': 'text/html'}); + dSpaceRESTv2Service.request(RestRequestMethod.POST, url, {}, { headers }).subscribe(); + + const req = httpMock.expectOne(url); + expect(req.request.headers.get('Content-Type')).not.toContain('application/json; charset=utf-8'); + }); + + it('when no content-type header is provided, it should use application/json', () => { + dSpaceRESTv2Service.request(RestRequestMethod.POST, url, {}).subscribe(); + + const req = httpMock.expectOne(url); + expect(req.request.headers.get('Content-Type')).toContain('application/json; charset=utf-8'); + }); }); describe('buildFormData', () => { diff --git a/src/app/core/dspace-rest-v2/dspace-rest-v2.service.ts b/src/app/core/dspace-rest-v2/dspace-rest-v2.service.ts index a2a9f2530c..1d72f5e4fe 100644 --- a/src/app/core/dspace-rest-v2/dspace-rest-v2.service.ts +++ b/src/app/core/dspace-rest-v2/dspace-rest-v2.service.ts @@ -1,5 +1,5 @@ -import {throwError as observableThrowError, Observable } from 'rxjs'; -import {catchError, map} from 'rxjs/operators'; +import { Observable, throwError as observableThrowError } from 'rxjs'; +import { catchError, map } from 'rxjs/operators'; import { Injectable } from '@angular/core'; import { HttpClient, HttpHeaders, HttpParams, HttpResponse } from '@angular/common/http' @@ -8,6 +8,7 @@ import { HttpObserve } from '@angular/common/http/src/client'; import { RestRequestMethod } from '../data/rest-request-method'; import { isNotEmpty } from '../../shared/empty.util'; import { DSpaceObject } from '../shared/dspace-object.model'; +import { cloneDeep } from 'lodash'; export interface HttpOptions { body?: any; @@ -38,11 +39,23 @@ export class DSpaceRESTv2Service { * An Observable containing the response from the server */ get(absoluteURL: string): Observable { - return this.http.get(absoluteURL, { observe: 'response' }).pipe( - map((res: HttpResponse) => ({ payload: res.body, statusCode: res.status, statusText: res.statusText })), + const requestOptions = { + observe: 'response' as any, + headers: new HttpHeaders({ 'Content-Type': 'application/json; charset=utf-8' }) + }; + return this.http.get(absoluteURL, requestOptions).pipe( + map((res: HttpResponse) => ({ + payload: res.body, + statusCode: res.status, + statusText: res.statusText + })), catchError((err) => { console.log('Error: ', err); - return observableThrowError({statusCode: err.status, statusText: err.statusText, message: err.message}); + return observableThrowError({ + statusCode: err.status, + statusText: err.statusText, + message: err.message + }); })); } @@ -65,17 +78,45 @@ export class DSpaceRESTv2Service { requestOptions.body = this.buildFormData(body); } requestOptions.observe = 'response'; - if (options && options.headers) { - requestOptions.headers = Object.assign(new HttpHeaders(), options.headers); - } + if (options && options.responseType) { requestOptions.responseType = options.responseType; } + // WORKING OPTION + // requestOptions.headers = ((options && options.headers) || new HttpHeaders()).set('blaat', 'bla').delete('blaat'); + + // OPTION 1 + // requestOptions.headers = ((options && options.headers) || new HttpHeaders()); + + // OPTION 2 + // requestOptions.headers = new HttpHeaders(); + // if (options && options.headers) { + // options.headers.keys().forEach((key) => { + // options.headers.getAll(key).forEach((header) => { + // // Because HttpHeaders is immutable, the set method returns a new object instead of updating the existing headers + // requestOptions.headers = requestOptions.headers.set(key, header); + // }) + // }); + // } + // + // if (!requestOptions.headers.has('Content-Type')) { + // // Because HttpHeaders is immutable, the set method returns a new object instead of updating the existing headers + // requestOptions.headers = requestOptions.headers.set('Content-Type', 'application/json; charset=utf-8'); + // } return this.http.request(method, url, requestOptions).pipe( - map((res) => ({ payload: res.body, headers: res.headers, statusCode: res.status, statusText: res.statusText })), + map((res) => ({ + payload: res.body, + headers: res.headers, + statusCode: res.status, + statusText: res.statusText + })), catchError((err) => { console.log('Error: ', err); - return observableThrowError({statusCode: err.status, statusText: err.statusText, message: err.message}); + return observableThrowError({ + statusCode: err.status, + statusText: err.statusText, + message: err.message + }); })); }