Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Attaches the authentication token if one is currently held.
- Loading branch information
Showing
4 changed files
with
209 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* ComiXed - A digital comic book library management application. | ||
* Copyright (C) 2020, The ComiXed Project | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses> | ||
*/ | ||
|
||
export const HTTP_AUTHORIZATION_HEADER = 'Authorization'; | ||
export const HTTP_REQUESTED_WITH_HEADER = 'X-Requested-With'; | ||
export const HTTP_XML_REQUEST = 'XMLHttpRequest'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
comixed-web/src/app/interceptors/http.interceptor.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* ComiXed - A digital comic book library management application. | ||
* Copyright (C) 2020, The ComiXed Project | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses> | ||
*/ | ||
|
||
import { TestBed } from '@angular/core/testing'; | ||
import { HttpInterceptor } from './http.interceptor'; | ||
import { LoggerModule } from '@angular-ru/logger'; | ||
import { TokenService } from '@app/core'; | ||
import { CoreModule } from '@app/core/core.module'; | ||
import { AUTHENTICATION_TOKEN } from '@app/core/core.fixtures'; | ||
import { | ||
HttpClientTestingModule, | ||
HttpTestingController, | ||
TestRequest | ||
} from '@angular/common/http/testing'; | ||
import { Injectable } from '@angular/core'; | ||
import { Observable } from 'rxjs'; | ||
import { HTTP_INTERCEPTORS, HttpClient } from '@angular/common/http'; | ||
import { | ||
HTTP_AUTHORIZATION_HEADER, | ||
HTTP_REQUESTED_WITH_HEADER, | ||
HTTP_XML_REQUEST | ||
} from '@app/app.constants'; | ||
|
||
const TEST_REQUEST_URL = 'http://localhost'; | ||
|
||
@Injectable() | ||
class TestService { | ||
constructor(private http: HttpClient) {} | ||
|
||
request(): Observable<any> { | ||
return this.http.get(TEST_REQUEST_URL); | ||
} | ||
} | ||
|
||
describe('HttpInterceptor', () => { | ||
let interceptor: HttpInterceptor; | ||
let tokenService: jasmine.SpyObj<TokenService>; | ||
let httpMock: HttpTestingController; | ||
let testService: TestService; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [CoreModule, HttpClientTestingModule, LoggerModule.forRoot()], | ||
providers: [ | ||
{ provide: HTTP_INTERCEPTORS, useClass: HttpInterceptor, multi: true }, | ||
TestService, | ||
{ | ||
provide: TokenService, | ||
useValue: { | ||
hasAuthToken: jasmine.createSpy('TokenService.hasAuthToken()'), | ||
getAuthToken: jasmine.createSpy('TokenService.getAuthToken()') | ||
} | ||
} | ||
] | ||
}); | ||
|
||
tokenService = TestBed.inject(TokenService) as jasmine.SpyObj<TokenService>; | ||
httpMock = TestBed.inject(HttpTestingController); | ||
testService = TestBed.inject(TestService); | ||
}); | ||
|
||
describe('when no authentication token is present', () => { | ||
let req: TestRequest; | ||
|
||
beforeEach(() => { | ||
tokenService.hasAuthToken.and.returnValue(false); | ||
testService.request().subscribe(() => {}); | ||
req = httpMock.expectOne(TEST_REQUEST_URL); | ||
}); | ||
|
||
it('does not contain an authorization header', () => { | ||
expect(req.request.headers.get(HTTP_AUTHORIZATION_HEADER)).toBeNull(); | ||
}); | ||
|
||
it('attaches a requested with header', () => { | ||
expect(req.request.headers.get(HTTP_REQUESTED_WITH_HEADER)).toEqual( | ||
HTTP_XML_REQUEST | ||
); | ||
}); | ||
}); | ||
|
||
describe('when an authentication token is present', () => { | ||
let req: TestRequest; | ||
|
||
beforeEach(() => { | ||
tokenService.hasAuthToken.and.returnValue(true); | ||
tokenService.getAuthToken.and.returnValue(AUTHENTICATION_TOKEN); | ||
testService.request().subscribe(() => {}); | ||
req = httpMock.expectOne(TEST_REQUEST_URL); | ||
}); | ||
|
||
it('attaches the token as a request header', () => { | ||
expect(req.request.headers.get(HTTP_AUTHORIZATION_HEADER)).toEqual( | ||
`Bearer ${AUTHENTICATION_TOKEN}` | ||
); | ||
}); | ||
|
||
it('attaches a requested with header', () => { | ||
expect(req.request.headers.get(HTTP_REQUESTED_WITH_HEADER)).toEqual( | ||
HTTP_XML_REQUEST | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* ComiXed - A digital comic book library management application. | ||
* Copyright (C) 2020, The ComiXed Project | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses> | ||
*/ | ||
|
||
import { Injectable } from '@angular/core'; | ||
import { HttpEvent, HttpHandler, HttpRequest } from '@angular/common/http'; | ||
import { Observable } from 'rxjs'; | ||
import { LoggerService } from '@angular-ru/logger'; | ||
import { TokenService } from '@app/core'; | ||
import { | ||
HTTP_AUTHORIZATION_HEADER, | ||
HTTP_REQUESTED_WITH_HEADER, | ||
HTTP_XML_REQUEST | ||
} from '@app/app.constants'; | ||
import { retry, tap } from 'rxjs/operators'; | ||
|
||
@Injectable() | ||
export class HttpInterceptor implements HttpInterceptor { | ||
constructor( | ||
private logger: LoggerService, | ||
private tokenService: TokenService | ||
) {} | ||
|
||
intercept( | ||
request: HttpRequest<unknown>, | ||
next: HttpHandler | ||
): Observable<HttpEvent<unknown>> { | ||
let requestClone = request.clone({ | ||
headers: request.headers.set(HTTP_REQUESTED_WITH_HEADER, HTTP_XML_REQUEST) | ||
}); | ||
|
||
this.logger.trace('Processing outgoing HTTP request'); | ||
if (this.tokenService.hasAuthToken()) { | ||
this.logger.trace('Adding authentication token to request'); | ||
const token = this.tokenService.getAuthToken(); | ||
requestClone = requestClone.clone({ | ||
headers: requestClone.headers.set( | ||
HTTP_AUTHORIZATION_HEADER, | ||
`Bearer ${token}` | ||
) | ||
}); | ||
} | ||
return next.handle(requestClone).pipe( | ||
retry(3), | ||
tap(response => { | ||
this.logger.trace('Response received:', response); | ||
}) | ||
); | ||
} | ||
} |