-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoint.js
167 lines (139 loc) · 3.33 KB
/
endpoint.js
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
const { assign, create, has, is, isArray, isNullish, asNumber, isRegExp, asString } = require('./util');
class Endpoints extends Array {
add(opts) {
const endpoint = opts instanceof Endpoint ? opts : new Endpoint(opts);
this.push(endpoint);
return endpoint;
}
clone(endpoint) {
return this.add(endpoint.clone());
}
cloneOfURL(endpoint) {
return this.add(endpoint.cloneOfURL());
}
findByRequest(request) {
return this.find(endpoint => endpoint.matchesRequest(request)) || null;
}
someByRequest(request) {
return this.some(endpoint => endpoint.matchesRequest(request));
}
someByURL(url) {
return this.some(endpoint => endpoint.matchesURL(url));
}
toJSON() {
return Object(this);
}
}
class Endpoint {
constructor(opts) {
opts = create(opts);
const req = create(opts.request);
const res = create(opts.response);
assign(this, {
request: create({
method: isNullish(req.method) ? null : String(req.method).toUpperCase(),
url: isNullish(req.url) ? null : isRegExp(req.url) ? req.url : String(req.url),
urlParams: isArray(req.urlParams) ? req.urlParams : [],
headers: create(req.headers),
body: isNullish(req.body) ? null : req.body,
}),
response: create({
status: asNumber(res.status) || 200,
headers: create({
'Content-Type': 'application/json; charset=utf-8'
}, res.headers),
body: has(res, 'body') ? res.body : create(),
}),
});
}
clone() {
return new Endpoint(this);
}
cloneOfURL() {
return new Endpoint(
create(
this,
{
request: create({
url: this.request.url
}),
response: create()
}
)
);
}
matchesRequest(request) {
const selfReq = create(this.request);
const testReq = create(request);
// match the endpoint request method
const matchesMethod = (
isNullish(testReq.method) ||
isNullish(selfReq.method) ||
is(
asString(testReq.method).toUpperCase(),
asString(selfReq.method).toUpperCase()
)
);
if (!matchesMethod) {
return false;
}
// match the endpoint request url
const matchesUrl = (
isNullish(testReq.url) ||
isNullish(selfReq.url) ||
(
isRegExp(selfReq.url)
? selfReq.url.test(testReq.url)
: is(
asString(testReq.url),
asString(selfReq.url)
)
)
);
if (!matchesUrl) {
return false;
}
// match the endpoint request headers
const selfReqHeaders = create(selfReq.headers);
const testReqHeaders = create(testReq.headers);
const matchesHeaders = Object.entries(selfReqHeaders).every(
([ name, value ]) => (
has(testReqHeaders, name.toLowerCase()) &&
is(
asString(testReqHeaders[name.toLowerCase()]),
asString(value)
)
)
);
if (!matchesHeaders) {
return false;
}
// match the endpoint request body
const selfReqBody = create(selfReq.body);
const testReqBody = create(testReq.body);
const matchesBody = Object.entries(selfReqBody).every(
([ name, value ]) => (
has(testReqBody, name) &&
is(
asString(testReqBody[name]),
asString(value)
)
)
);
if (!matchesBody) {
return false;
}
return true;
}
matchesURL(testURL) {
const thisURL = this.request.url;
return isRegExp(thisURL)
? thisURL.test(testURL)
: asString(thisURL) === asString(testURL);
}
toJSON() {
return Object(this);
}
}
exports.Endpoints = Endpoints;
exports.Endpoint = Endpoint;