Skip to content
Snippets Groups Projects
Commit a5491b95 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-2574 OZG-3439 Refactoring

parent 1788c619
No related branches found
No related tags found
No related merge requests found
...@@ -29,10 +29,11 @@ import { XhrInterceptor } from './xhr.interceptor'; ...@@ -29,10 +29,11 @@ import { XhrInterceptor } from './xhr.interceptor';
describe('XhrInterceptor', () => { describe('XhrInterceptor', () => {
let interceptor: XhrInterceptor; let interceptor: XhrInterceptor;
const apiHost = 'http://kop/api' const apiHost: string = 'http://kop/api'
const apiUrl: string = apiHost + '/vorgaenge';
const nonApiUrl: string = 'http://kop/vorgang/123';
beforeEach(() => TestBed.configureTestingModule({ beforeEach(() => TestBed.configureTestingModule({
imports: [],
providers: [ providers: [
XhrInterceptor, XhrInterceptor,
{ {
...@@ -62,7 +63,7 @@ describe('XhrInterceptor', () => { ...@@ -62,7 +63,7 @@ describe('XhrInterceptor', () => {
const httpHandler: HttpHandler = { handle }; const httpHandler: HttpHandler = { handle };
it('should add header for api requests', () => { it('should add header for api requests', () => {
const req: HttpRequest<any> = new HttpRequest('GET', apiHost + '/vorgaenge'); const req: HttpRequest<any> = new HttpRequest('GET', apiUrl);
interceptor.intercept(req, httpHandler); interceptor.intercept(req, httpHandler);
...@@ -70,7 +71,7 @@ describe('XhrInterceptor', () => { ...@@ -70,7 +71,7 @@ describe('XhrInterceptor', () => {
}) })
it('should not add header for no api requests', () => { it('should not add header for no api requests', () => {
const req: HttpRequest<any> = new HttpRequest('GET', 'http://someserver/resource'); const req: HttpRequest<any> = new HttpRequest('GET', nonApiUrl);
interceptor.intercept(req, httpHandler); interceptor.intercept(req, httpHandler);
...@@ -78,4 +79,23 @@ describe('XhrInterceptor', () => { ...@@ -78,4 +79,23 @@ describe('XhrInterceptor', () => {
}) })
}) })
describe('isApiRequest', () => {
it('should return true for API URL', () => {
expect(interceptor.isApiRequest(apiUrl)).toBeTruthy()
})
it('should return false for non-API URL', () => {
expect(interceptor.isApiRequest(nonApiUrl)).toBeFalsy()
})
})
describe('addHeader', () => {
it('should add X-Requested-With header', () => {
const request: HttpRequest<unknown> = new HttpRequest('GET', apiUrl);
const result: HttpRequest<unknown> = interceptor.addHeader(request);
expect(result.headers.get('X-Requested-With')).toEqual('XMLHttpRequest');
})
})
}); });
\ No newline at end of file
...@@ -21,9 +21,10 @@ ...@@ -21,9 +21,10 @@
* Die sprachspezifischen Genehmigungen und Beschränkungen * Die sprachspezifischen Genehmigungen und Beschränkungen
* unter der Lizenz sind dem Lizenztext zu entnehmen. * unter der Lizenz sind dem Lizenztext zu entnehmen.
*/ */
import { HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http'; import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Inject, Injectable } from '@angular/core'; import { Inject, Injectable } from '@angular/core';
import { Environment, ENVIRONMENT_CONFIG } from '@goofy-client/environment-shared'; import { Environment, ENVIRONMENT_CONFIG } from '@goofy-client/environment-shared';
import { Observable } from 'rxjs';
@Injectable() @Injectable()
export class XhrInterceptor implements HttpInterceptor { export class XhrInterceptor implements HttpInterceptor {
...@@ -31,13 +32,20 @@ export class XhrInterceptor implements HttpInterceptor { ...@@ -31,13 +32,20 @@ export class XhrInterceptor implements HttpInterceptor {
constructor(@Inject(ENVIRONMENT_CONFIG) private envConfig: Environment) { constructor(@Inject(ENVIRONMENT_CONFIG) private envConfig: Environment) {
} }
intercept(req: HttpRequest<any>, next: HttpHandler) { intercept(req: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
if (req.url.startsWith(this.envConfig.remoteHost)) { if (this.isApiRequest(req.url)) {
const xhr: HttpRequest<any> = req.clone({ return next.handle(this.addHeader(req));
headers: req.headers.set('X-Requested-With', 'XMLHttpRequest')
});
return next.handle(xhr);
} }
return next.handle(req); return next.handle(req);
} }
isApiRequest(url: string): boolean {
return url.startsWith(this.envConfig.remoteHost);
}
addHeader(req: HttpRequest<unknown>): HttpRequest<unknown> {
return req.clone({
headers: req.headers.set('X-Requested-With', 'XMLHttpRequest')
});
}
} }
\ No newline at end of file
...@@ -26,6 +26,7 @@ import { HTTP_INTERCEPTORS } from '@angular/common/http'; ...@@ -26,6 +26,7 @@ import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { Injector, NgModule } from '@angular/core'; import { Injector, NgModule } from '@angular/core';
import { HttpBinaryFileInterceptor } from './interceptor/http-binary-file.interceptor'; import { HttpBinaryFileInterceptor } from './interceptor/http-binary-file.interceptor';
import { HttpXsrfInterceptor } from './interceptor/http-xsrf.interceptor'; import { HttpXsrfInterceptor } from './interceptor/http-xsrf.interceptor';
import { XhrInterceptor } from './interceptor/xhr.interceptor';
import { ConvertForDataTestPipe } from './pipe/convert-for-data-test.pipe'; import { ConvertForDataTestPipe } from './pipe/convert-for-data-test.pipe';
import { EnumToLabelPipe } from './pipe/enum-to-label.pipe'; import { EnumToLabelPipe } from './pipe/enum-to-label.pipe';
import { FileSizePipe } from './pipe/file-size.pipe'; import { FileSizePipe } from './pipe/file-size.pipe';
...@@ -67,6 +68,11 @@ import { ToTrafficLightPipe } from './pipe/to-traffic-light.pipe'; ...@@ -67,6 +68,11 @@ import { ToTrafficLightPipe } from './pipe/to-traffic-light.pipe';
FileSizePipe, FileSizePipe,
], ],
providers: [ providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: XhrInterceptor,
multi: true,
},
{ {
provide: HTTP_INTERCEPTORS, provide: HTTP_INTERCEPTORS,
useClass: HttpXsrfInterceptor, useClass: HttpXsrfInterceptor,
......
...@@ -58,7 +58,6 @@ import { AppIconComponent } from '../icon/app-icon/app-icon.component'; ...@@ -58,7 +58,6 @@ import { AppIconComponent } from '../icon/app-icon/app-icon.component';
import { PostfachIconComponent } from '../icon/postfach-icon/postfach-icon.component'; import { PostfachIconComponent } from '../icon/postfach-icon/postfach-icon.component';
import { HttpConnectionTimeoutInterceptor } from '../interceptor/http-connection-timeout.interceptor'; import { HttpConnectionTimeoutInterceptor } from '../interceptor/http-connection-timeout.interceptor';
import { HttpErrorInterceptor } from '../interceptor/http-error.interceptor'; import { HttpErrorInterceptor } from '../interceptor/http-error.interceptor';
import { XhrInterceptor } from '../interceptor/xhr.interceptor';
import { SnackbarCloseButtonComponent } from '../snackbar/snackbar-close-button/snackbar-close-button.component'; import { SnackbarCloseButtonComponent } from '../snackbar/snackbar-close-button/snackbar-close-button.component';
import { SnackbarErrorComponent } from '../snackbar/snackbar-error/snackbar-error.component'; import { SnackbarErrorComponent } from '../snackbar/snackbar-error/snackbar-error.component';
import { SnackbarInfoComponent } from '../snackbar/snackbar-info/snackbar-info.component'; import { SnackbarInfoComponent } from '../snackbar/snackbar-info/snackbar-info.component';
...@@ -154,11 +153,6 @@ const modules = [ ...@@ -154,11 +153,6 @@ const modules = [
imports: [...modules], imports: [...modules],
exports: [...modules, ...components], exports: [...modules, ...components],
providers: [ providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: XhrInterceptor,
multi: true,
},
{ {
provide: HTTP_INTERCEPTORS, provide: HTTP_INTERCEPTORS,
useClass: HttpErrorInterceptor, useClass: HttpErrorInterceptor,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment