Created
July 1, 2020 14:41
-
-
Save victor-shelepen/21dbd4ba7c9b1afc74f6d9f9c5d64a19 to your computer and use it in GitHub Desktop.
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
Index: src/app/interceptor/in-memory-data.spec.ts | |
IDEA additional info: | |
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP | |
<+>UTF-8 | |
=================================================================== | |
--- src/app/interceptor/in-memory-data.spec.ts (revision 7e638c32329b38126ad578cb9fb0b68c86df4a76) | |
+++ src/app/interceptor/in-memory-data.spec.ts (revision 7e638c32329b38126ad578cb9fb0b68c86df4a76) | |
@@ -0,0 +1,40 @@ | |
+import {HTTP_INTERCEPTORS, HttpClient, HttpParams} from '@angular/common/http'; | |
+import {HttpClientTestingModule} from '@angular/common/http/testing'; | |
+import {TestBed} from '@angular/core/testing'; | |
+import {InMemoryDataInterceptor} from './in-memory-data'; | |
+import {EMethod, InMemoryDataService} from '../service/in-memory-data'; | |
+import {testingRoute} from '../service/in-memory-data.spec'; | |
+ | |
+describe('In memory data interceptor', () => { | |
+ let httpClient: HttpClient; | |
+ | |
+ beforeEach(() => { | |
+ TestBed.configureTestingModule({ | |
+ providers: [ | |
+ InMemoryDataService, | |
+ { | |
+ provide: HTTP_INTERCEPTORS, | |
+ useClass: InMemoryDataInterceptor, | |
+ multi: true, | |
+ }, | |
+ ], | |
+ imports: [HttpClientTestingModule] | |
+ }); | |
+ httpClient = TestBed.inject(HttpClient) | |
+ const inMemoryData = TestBed.inject(InMemoryDataService) | |
+ inMemoryData.points[EMethod.POST].push(testingRoute) | |
+ }); | |
+ | |
+ it('Request intercepted', () => { | |
+ const params = new HttpParams() | |
+ .set('userId', '1234'); | |
+ let executed = false; | |
+ httpClient.post('/api/test/29', { params }) | |
+ .subscribe((data) => { | |
+ expect(data).toEqual('id=29&userId=1234') | |
+ executed = true | |
+ }) | |
+ expect(executed).toEqual(true) | |
+ }) | |
+ | |
+}); | |
Index: src/app/interceptor/in-memory-data.ts | |
IDEA additional info: | |
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP | |
<+>UTF-8 | |
=================================================================== | |
--- src/app/interceptor/in-memory-data.ts (revision 7e638c32329b38126ad578cb9fb0b68c86df4a76) | |
+++ src/app/interceptor/in-memory-data.ts (revision 7e638c32329b38126ad578cb9fb0b68c86df4a76) | |
@@ -0,0 +1,17 @@ | |
+import { HttpInterceptor, HttpEvent, HttpRequest, HttpHandler } from '@angular/common/http'; | |
+import { InMemoryDataService } from '../service/in-memory-data'; | |
+import { Observable, of } from 'rxjs'; | |
+import {Injectable} from '@angular/core'; | |
+ | |
+@Injectable() | |
+export class InMemoryDataInterceptor implements HttpInterceptor { | |
+ constructor( | |
+ private inMemoryData: InMemoryDataService, | |
+ ) {} | |
+ | |
+ intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | |
+ const response = this.inMemoryData.process(request) | |
+ | |
+ return of(response); | |
+ } | |
+} | |
Index: src/app/service/in-memory-data.spec.ts | |
IDEA additional info: | |
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP | |
<+>UTF-8 | |
=================================================================== | |
--- src/app/service/in-memory-data.spec.ts (revision 7e638c32329b38126ad578cb9fb0b68c86df4a76) | |
+++ src/app/service/in-memory-data.spec.ts (revision 7e638c32329b38126ad578cb9fb0b68c86df4a76) | |
@@ -0,0 +1,49 @@ | |
+import {TestBed} from '@angular/core/testing'; | |
+import {HttpClient, HttpClientModule, HttpParams, HttpRequest, HttpResponse} from '@angular/common/http'; | |
+import {EMethod, InMemoryDataService, IRoute} from './in-memory-data'; | |
+ | |
+export const testingRoute = { | |
+ pattern: /^\/api\/test\/(?<id>\d*)$/s, | |
+ handler(request: HttpRequest<any>, {groups}): HttpResponse<any> { | |
+ const id = groups['id'] | |
+ const userId = request.body.params.get('userId') | |
+ return new HttpResponse<any>({ | |
+ status: 200, | |
+ body: `id=${id}&userId=${userId}` | |
+ }) | |
+ } | |
+} as IRoute<any, any> | |
+ | |
+describe('In memory data', () => { | |
+ let httpClient: HttpClient; | |
+ let inMemoryData: InMemoryDataService; | |
+ | |
+ beforeEach(() => { | |
+ TestBed.configureTestingModule({ | |
+ imports: [ | |
+ HttpClientModule | |
+ ], | |
+ providers: [ | |
+ InMemoryDataService | |
+ ] | |
+ }); | |
+ httpClient = TestBed.inject(HttpClient) | |
+ inMemoryData = TestBed.inject(InMemoryDataService) | |
+ inMemoryData.points[EMethod.POST].push(testingRoute) | |
+ }); | |
+ | |
+ it('Test', () => { | |
+ const params = new HttpParams() | |
+ .set('userId', '1234'); | |
+ | |
+ const httpRequest = new HttpRequest( | |
+ 'POST', | |
+ '/api/test/29', | |
+ { params } | |
+ ); | |
+ const response = inMemoryData.process(httpRequest) | |
+ expect(response.status).toEqual(200) | |
+ expect(response.body).toEqual('id=29&userId=1234') | |
+ }) | |
+ | |
+}) | |
Index: src/app/service/in-memory-data.ts | |
IDEA additional info: | |
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP | |
<+>UTF-8 | |
=================================================================== | |
--- src/app/service/in-memory-data.ts (revision 7e638c32329b38126ad578cb9fb0b68c86df4a76) | |
+++ src/app/service/in-memory-data.ts (revision 7e638c32329b38126ad578cb9fb0b68c86df4a76) | |
@@ -0,0 +1,41 @@ | |
+import {HttpRequest, HttpResponse} from '@angular/common/http'; | |
+ | |
+export interface IAnswer<T> { | |
+ status: number | |
+ body: T | |
+} | |
+ | |
+export enum EMethod { | |
+ GET = 'GET', | |
+ POST = 'POST' | |
+} | |
+ | |
+export interface IRoute<Rq, Rs> { | |
+ pattern: RegExp; | |
+ handler<Rq, Rs>(request: HttpRequest<Rq>, match: IMatch): HttpResponse<Rs>; | |
+} | |
+ | |
+export type IMethodPoints = {[P in EMethod]?: IRoute<any, any>[]} | |
+ | |
+export interface IMatch extends Array<any> { | |
+ groups: Object | |
+} | |
+ | |
+export class InMemoryDataService { | |
+ | |
+ public points: IMethodPoints = { | |
+ 'POST': [], | |
+ 'GET': [] | |
+ } | |
+ | |
+ process<Rq, Rs>(request: HttpRequest<Rq>): HttpResponse<Rs> { | |
+ const methodPoints = this.points[request.method] | |
+ for (const point of methodPoints) { | |
+ const match = request.url.match(point.pattern) | |
+ if (match) { | |
+ return point.handler(request, match) | |
+ } | |
+ } | |
+ } | |
+ | |
+} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment