This repository was archived by the owner on Feb 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcontext.ts
126 lines (102 loc) · 3.5 KB
/
context.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
import { PartialResponseForRequest } from './filtering/collection';
import { byUrl as comparatorByUrl, ComparatorFn } from './filtering/comparator';
import { match, Matcher } from './filtering/matcher';
import { Redactor } from './filtering/redact';
import {
ISerializedHttp,
ISerializedRequest,
ISerializedResponse,
RequestSerializer,
} from './http-serializer';
import { RecordMode as Mode } from './recording';
import Rule from './rule';
export interface IRedactProp {
property: string | string[];
redactor?: Redactor;
}
export interface IInFlightRequest {
startTime: number;
requestSerializer: RequestSerializer;
}
export interface IResponseForMatchingRequest {
response: PartialResponseForRequest;
matcher: Matcher;
}
/**
* Store the current execution context for YesNo by tracking requests & mocks.
*/
export default class Context {
public mode: Mode = Mode.Spy;
public rules: Rule[] = [];
/**
* Setting to redact all incoming requests to match redacted mocks
*/
public autoRedact: IRedactProp | null = null;
/**
* Completed serialized request-response objects. Used for:
* A. Assertions
* B. Saved to disk if in record mode
*/
public interceptedRequestsCompleted: ISerializedHttp[] = [];
/**
* Serialized records loaded from disk.
*/
public loadedMocks: ISerializedHttp[] = [];
/**
* Proxied requests which have not yet responded. When completed
* the value is set to "null" but the index is preserved.
*/
public inFlightRequests: Array<IInFlightRequest | null> = [];
public ignoresForMatchingRequests: IResponseForMatchingRequest[] = [];
public responsesForMatchingRequests: IResponseForMatchingRequest[] = [];
public comparatorFn: ComparatorFn = comparatorByUrl;
public clear() {
this.ignoresForMatchingRequests = [];
this.interceptedRequestsCompleted = [];
this.inFlightRequests = [];
this.loadedMocks = [];
this.responsesForMatchingRequests = [];
this.comparatorFn = comparatorByUrl;
}
public getMatchingMocks(matcher: Matcher): ISerializedHttp[] {
return this.loadedMocks.filter(match(matcher));
}
public getMatchingIntercepted(matcher: Matcher): ISerializedHttp[] {
return this.interceptedRequestsCompleted.filter(match(matcher));
}
public hasResponsesDefinedForMatchers(): boolean {
return !!this.responsesForMatchingRequests.length;
}
public addResponseForMatchingRequests(matchingResponse: IResponseForMatchingRequest): void {
this.responsesForMatchingRequests.push(matchingResponse);
}
public getResponseDefinedMatching(request: ISerializedRequest): ISerializedResponse | undefined {
let firstMatchingResponse: ISerializedResponse | undefined;
for (const { matcher, response } of this.responsesForMatchingRequests) {
const doesMatch = match(matcher)({ request });
if (doesMatch) {
firstMatchingResponse = {
body: {},
headers: {},
...(typeof response === 'function' ? response(request) : response),
};
break;
}
}
return firstMatchingResponse;
}
public addIgnoreForMatchingRequests(matchingResponse: IResponseForMatchingRequest): void {
this.ignoresForMatchingRequests.push(matchingResponse);
}
public hasMatchingIgnore(request: ISerializedRequest): boolean {
if (!this.ignoresForMatchingRequests.length) {
return false;
}
for (const { matcher } of this.ignoresForMatchingRequests) {
if (match(matcher)({ request })) {
return true;
}
}
return false;
}
}