-
Notifications
You must be signed in to change notification settings - Fork 8
/
RequestInterceptor.ts
175 lines (139 loc) · 6.74 KB
/
RequestInterceptor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import {Overrides, Request, RespondOptions} from 'puppeteer';
import {instanceOfRequestBlocker} from './common/interfaceValidators/instanceOfRequestBlocker';
import {instanceOfRequestModifier} from './common/interfaceValidators/instanceOfRequestModifier';
import {instanceOfRequestSpy} from './common/interfaceValidators/instanceOfRequestSpy';
import {instanceOfResponseFaker} from './common/interfaceValidators/instanceOfResponseFaker';
import {ILogger} from './common/Logger';
import {resolveOptionalPromise} from './common/resolveOptionalPromise';
import {UrlAccessor} from './common/urlAccessor/UrlAccessor';
import {UrlAccessorResolver} from './common/urlAccessor/UrlAccessorResolver';
import {VoidLogger} from './common/VoidLogger';
import {IRequestBlocker} from './interface/IRequestBlocker';
import {IRequestModifier} from './interface/IRequestModifier';
import {IRequestSpy} from './interface/IRequestSpy';
import {IResponseFaker} from './interface/IResponseFaker';
import {RequestBlocker} from './RequestBlocker';
import {RequestMatcher} from './types/RequestMatcher';
export class RequestInterceptor {
private requestSpies: Array<IRequestSpy> = [];
private responseFakers: Array<IResponseFaker> = [];
private requestModifiers: Array<IRequestModifier> = [];
private matcher: RequestMatcher;
private logger: ILogger;
private requestBlocker: IRequestBlocker;
public constructor(matcher: RequestMatcher, logger?: ILogger) {
if (typeof logger === 'undefined') {
logger = new VoidLogger();
}
this.logger = logger;
this.matcher = matcher;
this.requestBlocker = new RequestBlocker();
}
public async intercept(interceptedRequest: Request): Promise<void> {
await this.matchSpies(interceptedRequest);
if (await resolveOptionalPromise<boolean>(this.requestBlocker.shouldBlockRequest(interceptedRequest, this.matcher))) {
await this.blockUrl(interceptedRequest);
return;
}
let requestOverride: Overrides | undefined = await this.getMatchingOverride(interceptedRequest);
if (typeof requestOverride !== 'undefined') {
let urlAccessor: UrlAccessor = UrlAccessorResolver.getUrlAccessor(interceptedRequest);
await interceptedRequest.continue(requestOverride);
this.logger.log(`modified: ${urlAccessor.getUrlFromRequest(interceptedRequest)}`);
return;
}
let responseFaker: undefined | IResponseFaker = await this.getMatchingFaker(interceptedRequest);
if (typeof responseFaker !== 'undefined') {
let responseFake: RespondOptions | Promise<RespondOptions> = responseFaker.getResponseFake(interceptedRequest);
await interceptedRequest.respond(await resolveOptionalPromise<RespondOptions>(responseFake));
this.logger.log(`faked: ${interceptedRequest.url()}`);
return;
}
await this.acceptUrl(interceptedRequest);
}
public addSpy(requestSpy: IRequestSpy): void {
if (!instanceOfRequestSpy(requestSpy)) {
throw new Error('invalid RequestSpy provided. Please make sure to match the interface provided.');
}
this.requestSpies.push(requestSpy);
}
public addFaker(responseFaker: IResponseFaker): void {
if (!instanceOfResponseFaker(responseFaker)) {
throw new Error('invalid ResponseFaker provided. Please make sure to match the interface provided.');
}
this.responseFakers.push(responseFaker);
}
public addRequestModifier(requestModifier: IRequestModifier): void {
if (!instanceOfRequestModifier(requestModifier)) {
throw new Error('invalid RequestModifier provided. Please make sure to match the interface provided.');
}
this.requestModifiers.push(requestModifier);
}
public block(urlsToBlock: Array<string> | string): void {
this.requestBlocker.addUrlsToBlock(urlsToBlock);
}
public clearSpies(): void {
this.requestSpies = [];
}
public clearFakers(): void {
this.responseFakers = [];
}
public clearRequestModifiers(): void {
this.requestModifiers = [];
}
public clearUrlsToBlock(): void {
this.requestBlocker.clearUrlsToBlock();
}
public setUrlsToBlock(urlsToBlock: Array<string>): void {
this.requestBlocker.clearUrlsToBlock();
this.requestBlocker.addUrlsToBlock(urlsToBlock);
}
public setRequestBlocker(requestBlocker: IRequestBlocker): void {
if (!instanceOfRequestBlocker(requestBlocker)) {
throw new Error('invalid RequestBlocker provided. Please make sure to match the interface provided.');
}
this.requestBlocker = requestBlocker;
}
private async getMatchingFaker(interceptedRequest: Request): Promise<IResponseFaker | undefined> {
for (let faker of this.responseFakers) {
if (await resolveOptionalPromise<boolean>(faker.isMatchingRequest(interceptedRequest, this.matcher))) {
return faker;
}
}
return undefined;
}
private async matchSpies(interceptedRequest: Request): Promise<void> {
for (let spy of this.requestSpies) {
if (await resolveOptionalPromise<boolean>(spy.isMatchingRequest(interceptedRequest, this.matcher))) {
await resolveOptionalPromise(spy.addMatch(interceptedRequest));
}
}
}
private async getMatchingOverride(interceptedRequest: Request): Promise<Overrides | undefined> {
let requestOverride: Overrides | undefined;
for (let requestModifier of this.requestModifiers) {
if (await resolveOptionalPromise<boolean>(requestModifier.isMatchingRequest(interceptedRequest, this.matcher))) {
requestOverride = await resolveOptionalPromise<Overrides>(requestModifier.getOverride(interceptedRequest));
}
}
return requestOverride;
}
private async blockUrl(interceptedRequest: Request): Promise<void> {
let urlAccessor: UrlAccessor = UrlAccessorResolver.getUrlAccessor(interceptedRequest);
try {
await interceptedRequest.abort();
this.logger.log(`aborted: ${urlAccessor.getUrlFromRequest(interceptedRequest)}`);
} catch (error) {
this.logger.log((<Error>error).toString());
}
}
private async acceptUrl(interceptedRequest: Request): Promise<void> {
let urlAccessor: UrlAccessor = UrlAccessorResolver.getUrlAccessor(interceptedRequest);
try {
await interceptedRequest.continue();
this.logger.log(`loaded: ${urlAccessor.getUrlFromRequest(interceptedRequest)}`);
} catch (error) {
this.logger.log((<Error>error).toString());
}
}
}