Added LocaleInterceptor to set Accept-Language header

This commit is contained in:
Giuseppe Digilio
2020-04-20 20:07:24 +02:00
parent 145b26d262
commit 616a5fe0bd
3 changed files with 108 additions and 0 deletions

View File

@@ -143,6 +143,7 @@ import { Version } from './shared/version.model';
import { VersionHistory } from './shared/version-history.model';
import { WorkflowActionDataService } from './data/workflow-action-data.service';
import { WorkflowAction } from './tasks/models/workflow-action-object.model';
import { LocaleInterceptor } from './locale/locale.interceptor';
/**
* When not in production, endpoint responses can be mocked for testing purposes
@@ -272,6 +273,12 @@ const PROVIDERS = [
useClass: AuthInterceptor,
multi: true
},
// register LocaleInterceptor as HttpInterceptor
{
provide: HTTP_INTERCEPTORS,
useClass: LocaleInterceptor,
multi: true
},
NotificationsService,
FilteredDiscoveryPageResponseParsingService,
{ provide: NativeWindowService, useFactory: NativeWindowFactory }

View File

@@ -0,0 +1,70 @@
import { TestBed } from '@angular/core/testing';
import { HttpClientTestingModule, HttpTestingController, } from '@angular/common/http/testing';
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { DSpaceRESTv2Service } from '../dspace-rest-v2/dspace-rest-v2.service';
import { RestRequestMethod } from '../data/rest-request-method';
import { LocaleService } from './locale.service';
import { LocaleInterceptor } from './locale.interceptor';
describe(`LocaleInterceptor`, () => {
let service: DSpaceRESTv2Service;
let httpMock: HttpTestingController;
let localeService: any;
function getMockLocaleService(): LocaleService {
return jasmine.createSpyObj('LocaleService', {
getCurrentLanguageCode: jasmine.createSpy('getCurrentLanguageCode')
})
}
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [
DSpaceRESTv2Service,
{
provide: HTTP_INTERCEPTORS,
useClass: LocaleInterceptor,
multi: true,
},
{provide: LocaleService, useValue: getMockLocaleService()},
],
});
service = TestBed.get(DSpaceRESTv2Service);
httpMock = TestBed.get(HttpTestingController);
localeService = TestBed.get(LocaleService);
localeService.getCurrentLanguageCode.and.returnValue('en')
});
describe('', () => {
it('should add an Accept-Language header when were sending an HTTP POST request', () => {
service.request(RestRequestMethod.POST, 'server/api/submission/workspaceitems', 'test').subscribe((response) => {
expect(response).toBeTruthy();
});
const httpRequest = httpMock.expectOne(`server/api/submission/workspaceitems`);
expect(httpRequest.request.headers.has('Accept-Language'));
const lang = httpRequest.request.headers.get('Accept-Language');
expect(lang).toBe('en');
});
it('should add an Accept-Language header when were sending an HTTP GET request', () => {
service.request(RestRequestMethod.GET, 'server/api/submission/workspaceitems/123').subscribe((response) => {
expect(response).toBeTruthy();
});
const httpRequest = httpMock.expectOne(`server/api/submission/workspaceitems/123`);
expect(httpRequest.request.headers.has('Accept-Language'));
const lang = httpRequest.request.headers.get('Accept-Language');
expect(lang).toBe('en');
});
});
});

View File

@@ -0,0 +1,31 @@
import { Injectable } from '@angular/core';
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs';
import { LocaleService } from './locale.service';
@Injectable()
export class LocaleInterceptor implements HttpInterceptor {
constructor(private localeService: LocaleService) {
}
/**
* Intercept method
* @param req
* @param next
*/
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
let newReq: HttpRequest<any>;
// Clone the request to add the new header.
newReq = req.clone({
headers: req.headers
.set('Accept-Language', this.localeService.getCurrentLanguageCode())
});
// Pass on the new request instead of the original request.
return next.handle(newReq);
}
}