gingersnap 0.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/_virtual/_tslib.cjs +102 -0
- package/_virtual/_tslib.mjs +93 -0
- package/data/AtomicValue.d.ts +15 -0
- package/data/bus.cjs +179 -0
- package/data/bus.d.ts +72 -0
- package/data/bus.mjs +177 -0
- package/data/decoders/index.d.ts +4 -0
- package/data/decoders/json.cjs +12 -0
- package/data/decoders/json.d.ts +7 -0
- package/data/decoders/json.mjs +10 -0
- package/data/decoders/msgpack.cjs +14 -0
- package/data/decoders/msgpack.d.ts +7 -0
- package/data/decoders/msgpack.mjs +12 -0
- package/data/decoders/protobuf.d.ts +13 -0
- package/data/decoders/string.cjs +12 -0
- package/data/decoders/string.d.ts +7 -0
- package/data/decoders/string.mjs +10 -0
- package/data/decoders/type.d.ts +7 -0
- package/data/decoders.cjs +11 -0
- package/data/decoders.mjs +3 -0
- package/data/model/credentials.cjs +114 -0
- package/data/model/credentials.d.ts +61 -0
- package/data/model/credentials.mjs +109 -0
- package/data/model/extra.cjs +88 -0
- package/data/model/extra.d.ts +5 -0
- package/data/model/extra.mjs +66 -0
- package/data/model/index.d.ts +6 -0
- package/data/model/model.cjs +541 -0
- package/data/model/model.d.ts +207 -0
- package/data/model/model.mjs +518 -0
- package/data/model/plugin.cjs +96 -0
- package/data/model/plugin.d.ts +38 -0
- package/data/model/plugin.mjs +72 -0
- package/data/model/property.cjs +310 -0
- package/data/model/property.d.ts +83 -0
- package/data/model/property.mjs +278 -0
- package/data/model/types.cjs +32 -0
- package/data/model/types.d.ts +28 -0
- package/data/model/types.mjs +32 -0
- package/data/model.cjs +43 -0
- package/data/model.mjs +6 -0
- package/data/signal.cjs +47 -0
- package/data/signal.d.ts +17 -0
- package/data/signal.mjs +45 -0
- package/data/store/index.d.ts +4 -0
- package/data/store/json.cjs +58 -0
- package/data/store/json.d.ts +18 -0
- package/data/store/json.mjs +56 -0
- package/data/store/manager.cjs +206 -0
- package/data/store/manager.d.ts +44 -0
- package/data/store/manager.mjs +203 -0
- package/data/store/web.cjs +92 -0
- package/data/store/web.d.ts +25 -0
- package/data/store/web.mjs +89 -0
- package/data/value.cjs +66 -0
- package/data/value.mjs +64 -0
- package/data-structures/array/CyclicalList.cjs +57 -0
- package/data-structures/array/CyclicalList.d.ts +12 -0
- package/data-structures/array/CyclicalList.mjs +55 -0
- package/data-structures/array/Pair.cjs +30 -0
- package/data-structures/array/Pair.d.ts +17 -0
- package/data-structures/array/Pair.mjs +27 -0
- package/data-structures/array/Stack.cjs +96 -0
- package/data-structures/array/Stack.d.ts +41 -0
- package/data-structures/array/Stack.mjs +94 -0
- package/data-structures/array/Triple.cjs +34 -0
- package/data-structures/array/Triple.d.ts +19 -0
- package/data-structures/array/Triple.mjs +31 -0
- package/data-structures/array/Tuple.d.ts +8 -0
- package/data-structures/array/index.d.ts +5 -0
- package/data-structures/array.cjs +15 -0
- package/data-structures/array.mjs +4 -0
- package/data-structures/object/BufferQueue.cjs +98 -0
- package/data-structures/object/BufferQueue.d.ts +21 -0
- package/data-structures/object/BufferQueue.mjs +77 -0
- package/data-structures/object/CyclicalObject.cjs +155 -0
- package/data-structures/object/CyclicalObject.d.ts +74 -0
- package/data-structures/object/CyclicalObject.mjs +134 -0
- package/data-structures/object/Queue.cjs +178 -0
- package/data-structures/object/Queue.d.ts +31 -0
- package/data-structures/object/Queue.mjs +176 -0
- package/data-structures/object/SimpleQueue.cjs +53 -0
- package/data-structures/object/SimpleQueue.d.ts +13 -0
- package/data-structures/object/SimpleQueue.mjs +51 -0
- package/data-structures/object/TimeableObject.cjs +73 -0
- package/data-structures/object/TimeableObject.d.ts +30 -0
- package/data-structures/object/TimeableObject.mjs +71 -0
- package/data-structures/object/WaitableObject.cjs +61 -0
- package/data-structures/object/WaitableObject.d.ts +29 -0
- package/data-structures/object/WaitableObject.mjs +59 -0
- package/data-structures/object/WatchableObject.cjs +251 -0
- package/data-structures/object/WatchableObject.d.ts +88 -0
- package/data-structures/object/WatchableObject.mjs +230 -0
- package/data-structures/object/index.d.ts +7 -0
- package/data-structures/object.cjs +23 -0
- package/data-structures/object.mjs +7 -0
- package/errors/AbortError.cjs +9 -0
- package/errors/AbortError.d.ts +5 -0
- package/errors/AbortError.mjs +7 -0
- package/errors/CallExecutionError.cjs +13 -0
- package/errors/CallExecutionError.d.ts +7 -0
- package/errors/CallExecutionError.mjs +11 -0
- package/errors/FutureCancelled.cjs +11 -0
- package/errors/FutureCancelled.d.ts +6 -0
- package/errors/FutureCancelled.mjs +9 -0
- package/errors/FutureError.cjs +11 -0
- package/errors/FutureError.d.ts +6 -0
- package/errors/FutureError.mjs +9 -0
- package/errors/IllegalArgumentsError.cjs +13 -0
- package/errors/IllegalArgumentsError.d.ts +7 -0
- package/errors/IllegalArgumentsError.mjs +11 -0
- package/errors/IllegalOperationError.cjs +6 -0
- package/errors/IllegalOperationError.d.ts +2 -0
- package/errors/IllegalOperationError.mjs +4 -0
- package/errors/InvalidType.cjs +6 -0
- package/errors/InvalidType.d.ts +2 -0
- package/errors/InvalidType.mjs +4 -0
- package/errors/InvalidValue.cjs +11 -0
- package/errors/InvalidValue.d.ts +4 -0
- package/errors/InvalidValue.mjs +9 -0
- package/errors/MissingArgumentsError.cjs +13 -0
- package/errors/MissingArgumentsError.d.ts +7 -0
- package/errors/MissingArgumentsError.mjs +11 -0
- package/errors/NetworkError.cjs +13 -0
- package/errors/NetworkError.d.ts +8 -0
- package/errors/NetworkError.mjs +11 -0
- package/errors/NoSuchElement.cjs +6 -0
- package/errors/NoSuchElement.d.ts +2 -0
- package/errors/NoSuchElement.mjs +4 -0
- package/errors/NotImplemented.cjs +9 -0
- package/errors/NotImplemented.d.ts +5 -0
- package/errors/NotImplemented.mjs +7 -0
- package/errors/ParsingError.cjs +10 -0
- package/errors/ParsingError.d.ts +5 -0
- package/errors/ParsingError.mjs +8 -0
- package/errors/QueueEmptyError.cjs +6 -0
- package/errors/QueueEmptyError.d.ts +2 -0
- package/errors/QueueEmptyError.mjs +4 -0
- package/errors/QueueFullError.cjs +6 -0
- package/errors/QueueFullError.d.ts +2 -0
- package/errors/QueueFullError.mjs +4 -0
- package/errors/StackEmptyError.cjs +6 -0
- package/errors/StackEmptyError.d.ts +2 -0
- package/errors/StackEmptyError.mjs +4 -0
- package/errors/StackFullError.cjs +6 -0
- package/errors/StackFullError.d.ts +2 -0
- package/errors/StackFullError.mjs +4 -0
- package/errors/StreamEnded.cjs +6 -0
- package/errors/StreamEnded.d.ts +2 -0
- package/errors/StreamEnded.mjs +4 -0
- package/errors/TimeoutError.cjs +11 -0
- package/errors/TimeoutError.d.ts +6 -0
- package/errors/TimeoutError.mjs +9 -0
- package/errors/ValueDestroyedError.cjs +6 -0
- package/errors/ValueDestroyedError.d.ts +2 -0
- package/errors/ValueDestroyedError.mjs +4 -0
- package/errors/VerificationError.cjs +9 -0
- package/errors/VerificationError.d.ts +5 -0
- package/errors/VerificationError.mjs +7 -0
- package/errors/index.d.ts +18 -0
- package/errors.cjs +41 -0
- package/errors.mjs +18 -0
- package/files.cjs +201 -0
- package/files.d.ts +31 -0
- package/files.mjs +180 -0
- package/functools/index.d.ts +31 -0
- package/functools.cjs +371 -0
- package/functools.mjs +334 -0
- package/future/future.cjs +570 -0
- package/future/future.d.ts +208 -0
- package/future/future.mjs +548 -0
- package/future/group.cjs +180 -0
- package/future/group.d.ts +82 -0
- package/future/group.mjs +159 -0
- package/future/index.d.ts +3 -0
- package/future/result.cjs +19 -0
- package/future/result.d.ts +18 -0
- package/future/result.mjs +17 -0
- package/future.cjs +11 -0
- package/future.mjs +3 -0
- package/managers/context.d.ts +8 -0
- package/managers/index.d.ts +1 -0
- package/managers.cjs +2 -0
- package/managers.mjs +1 -0
- package/mocks.cjs +301 -0
- package/mocks.d.ts +125 -0
- package/mocks.mjs +293 -0
- package/networking/EventSourceService.cjs +104 -0
- package/networking/EventSourceService.d.ts +11 -0
- package/networking/EventSourceService.mjs +83 -0
- package/networking/NetworkService.cjs +394 -0
- package/networking/NetworkService.d.ts +97 -0
- package/networking/NetworkService.mjs +373 -0
- package/networking/SocketService.cjs +218 -0
- package/networking/SocketService.d.ts +23 -0
- package/networking/SocketService.mjs +197 -0
- package/networking/decorators/index.d.ts +4 -0
- package/networking/decorators/options.cjs +289 -0
- package/networking/decorators/options.d.ts +148 -0
- package/networking/decorators/options.mjs +242 -0
- package/networking/decorators/requests.cjs +80 -0
- package/networking/decorators/requests.d.ts +34 -0
- package/networking/decorators/requests.mjs +52 -0
- package/networking/decorators/socket.cjs +81 -0
- package/networking/decorators/socket.d.ts +13 -0
- package/networking/decorators/socket.mjs +51 -0
- package/networking/http.cjs +69 -0
- package/networking/http.d.ts +66 -0
- package/networking/http.mjs +69 -0
- package/networking/index.d.ts +39 -0
- package/networking/request.cjs +18 -0
- package/networking/request.d.ts +9 -0
- package/networking/request.mjs +16 -0
- package/networking/types.cjs +37 -0
- package/networking/types.d.ts +132 -0
- package/networking/types.mjs +35 -0
- package/networking.cjs +91 -0
- package/networking.mjs +33 -0
- package/package.json +1 -0
- package/reflection/injector.cjs +43 -0
- package/reflection/injector.d.ts +3 -0
- package/reflection/injector.mjs +40 -0
- package/socket.cjs +239 -0
- package/socket.d.ts +65 -0
- package/socket.mjs +237 -0
- package/store.cjs +12 -0
- package/store.mjs +3 -0
- package/stream/call.cjs +157 -0
- package/stream/call.d.ts +66 -0
- package/stream/call.mjs +154 -0
- package/stream/collector.cjs +217 -0
- package/stream/collector.d.ts +14 -0
- package/stream/collector.mjs +215 -0
- package/stream/index.d.ts +257 -0
- package/stream/observable.cjs +110 -0
- package/stream/observable.d.ts +65 -0
- package/stream/observable.mjs +106 -0
- package/stream/state.cjs +10 -0
- package/stream/state.d.ts +5 -0
- package/stream/state.mjs +8 -0
- package/stream.cjs +1153 -0
- package/stream.mjs +1132 -0
- package/synchronize.cjs +106 -0
- package/synchronize.d.ts +54 -0
- package/synchronize.mjs +103 -0
- package/typing/types.d.ts +24 -0
- package/typing.cjs +2 -0
- package/typing.mjs +1 -0
- package/utils/parser.cjs +37 -0
- package/utils/parser.d.ts +7 -0
- package/utils/parser.mjs +16 -0
package/mocks.cjs
ADDED
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _tslib = require('./_virtual/_tslib.cjs');
|
|
4
|
+
var AbortError = require('./errors/AbortError.cjs');
|
|
5
|
+
var VerificationError = require('./errors/VerificationError.cjs');
|
|
6
|
+
require('./data/model/model.cjs');
|
|
7
|
+
require('ramda');
|
|
8
|
+
require('./data/model/types.cjs');
|
|
9
|
+
require('./data/model/credentials.cjs');
|
|
10
|
+
require('./networking/types.cjs');
|
|
11
|
+
require('./stream.cjs');
|
|
12
|
+
var future = require('./future/future.cjs');
|
|
13
|
+
require('./networking/decorators/options.cjs');
|
|
14
|
+
require('./networking/decorators/requests.cjs');
|
|
15
|
+
var http = require('./networking/http.cjs');
|
|
16
|
+
require('uuid');
|
|
17
|
+
require('object-hash');
|
|
18
|
+
require('./data/store/manager.cjs');
|
|
19
|
+
require('./data-structures/object/WatchableObject.cjs');
|
|
20
|
+
require('modern-isomorphic-ws');
|
|
21
|
+
require('@msgpack/msgpack');
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Tracks specific details that should be used to match specific given network request
|
|
25
|
+
*/
|
|
26
|
+
class RequestDetails {
|
|
27
|
+
constructor() {
|
|
28
|
+
this.headers = {};
|
|
29
|
+
this.queries = {};
|
|
30
|
+
}
|
|
31
|
+
withMethod(method) {
|
|
32
|
+
this.method = method;
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
withPath(path) {
|
|
36
|
+
this.path = path;
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
withHeader(key, value) {
|
|
40
|
+
this.headers[key] = value;
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
withHeaders(value) {
|
|
44
|
+
this.headers = value;
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
withQuery(key, value) {
|
|
48
|
+
this.queries[key] = value;
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
withQueries(value) {
|
|
52
|
+
this.queries = value;
|
|
53
|
+
return this;
|
|
54
|
+
}
|
|
55
|
+
withBody(value) {
|
|
56
|
+
this.body = value;
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
class ResponseBuilder {
|
|
61
|
+
constructor() {
|
|
62
|
+
this.status = http.HTTPStatus.OK;
|
|
63
|
+
this.headers = {};
|
|
64
|
+
}
|
|
65
|
+
withStatus(status) {
|
|
66
|
+
this.status = status;
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
withHeader(key, value) {
|
|
70
|
+
this.headers[key] = value;
|
|
71
|
+
return this;
|
|
72
|
+
}
|
|
73
|
+
withHeaders(value) {
|
|
74
|
+
this.headers = value;
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
withBody(value) {
|
|
78
|
+
this.body = value;
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
withURI(value) {
|
|
82
|
+
this.uri = value;
|
|
83
|
+
return this;
|
|
84
|
+
}
|
|
85
|
+
withFragment(value) {
|
|
86
|
+
this.fragment = value;
|
|
87
|
+
return this;
|
|
88
|
+
}
|
|
89
|
+
withDelay(value) {
|
|
90
|
+
this.delay = value;
|
|
91
|
+
return this;
|
|
92
|
+
}
|
|
93
|
+
build() {
|
|
94
|
+
var _a, _b;
|
|
95
|
+
let data;
|
|
96
|
+
if (this.body && typeof this.body === "string") {
|
|
97
|
+
data = new Blob([this.body]);
|
|
98
|
+
}
|
|
99
|
+
else if (this.body instanceof Blob) {
|
|
100
|
+
data = this.body;
|
|
101
|
+
}
|
|
102
|
+
const status = (_a = this.status) !== null && _a !== void 0 ? _a : http.HTTPStatus.OK;
|
|
103
|
+
const response = new Response(data, {
|
|
104
|
+
status,
|
|
105
|
+
statusText: (_b = Object.keys(http.HTTPStatus)
|
|
106
|
+
.find((key) => http.HTTPStatus[key] === status)) === null || _b === void 0 ? void 0 : _b.toLowerCase().split("_").map((v) => v[0].toUpperCase() + v.slice(1)).join(" "),
|
|
107
|
+
headers: Object.entries(this.headers),
|
|
108
|
+
});
|
|
109
|
+
if (this.uri) {
|
|
110
|
+
const url = new URL(this.uri);
|
|
111
|
+
if (this.fragment) {
|
|
112
|
+
url.hash = this.fragment;
|
|
113
|
+
}
|
|
114
|
+
Object.defineProperty(response, "url", { value: url.href });
|
|
115
|
+
}
|
|
116
|
+
return { response, delay: this.delay };
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
class API {
|
|
120
|
+
constructor() {
|
|
121
|
+
this.matchers = [];
|
|
122
|
+
this.processedRequests = [];
|
|
123
|
+
}
|
|
124
|
+
resolve(req) {
|
|
125
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
126
|
+
const resp = yield this.findMatchingResponse(req);
|
|
127
|
+
if (resp) {
|
|
128
|
+
this.processedRequests.push([req, resp]);
|
|
129
|
+
return resp;
|
|
130
|
+
}
|
|
131
|
+
return new Response(null, { status: http.HTTPStatus.NOT_FOUND });
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
when(request, response) {
|
|
135
|
+
this.matchers.push(this.createMatcher(request, response));
|
|
136
|
+
return this;
|
|
137
|
+
}
|
|
138
|
+
verify(request, validator) {
|
|
139
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
140
|
+
const matcher = this.requestMatcher(request);
|
|
141
|
+
const responses = yield Promise.all(this.processedRequests.map(([req, resp]) => matcher(req).then((v) => (v ? resp : null))));
|
|
142
|
+
if (!validator(responses.filter((v) => v !== null))) {
|
|
143
|
+
throw new VerificationError.VerificationError();
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
reset() {
|
|
148
|
+
this.processedRequests = [];
|
|
149
|
+
}
|
|
150
|
+
findMatchingResponse(request) {
|
|
151
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
152
|
+
for (const matcher of this.matchers) {
|
|
153
|
+
const resp = yield matcher(request);
|
|
154
|
+
if (resp) {
|
|
155
|
+
return resp;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
requestMatcher(mock) {
|
|
161
|
+
const mockRequest = mock;
|
|
162
|
+
const pathChecker = (req) => mockRequest.path ? new URL(req.url).pathname.startsWith(mockRequest.path) : true;
|
|
163
|
+
const methodChecker = (req) => mockRequest.method ? req.method.toUpperCase() === mockRequest.method : true;
|
|
164
|
+
const headersChecker = (req) => Object.entries(mockRequest.headers).every(([k, v]) => req.headers.get(k) === v);
|
|
165
|
+
const bodyChecker = (req) => {
|
|
166
|
+
if (!mockRequest.body) {
|
|
167
|
+
return Promise.resolve(true);
|
|
168
|
+
}
|
|
169
|
+
const data = typeof mockRequest.body !== "string" ? JSON.stringify(mockRequest.body) : mockRequest.body;
|
|
170
|
+
const buffer = new TextEncoder().encode(data);
|
|
171
|
+
return req.arrayBuffer().then((v) => {
|
|
172
|
+
const data = new Uint8Array(v);
|
|
173
|
+
for (let i = 0; i < buffer.length; i++) {
|
|
174
|
+
if (buffer.at(i) !== data.at(i)) {
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return true;
|
|
179
|
+
});
|
|
180
|
+
};
|
|
181
|
+
const queriesChecker = (req) => {
|
|
182
|
+
const params = new URL(req.url).searchParams;
|
|
183
|
+
return Object.entries(mockRequest.queries).every(([k, v]) => params.get(k) === v);
|
|
184
|
+
};
|
|
185
|
+
return (req) => bodyChecker(req).then((result) => result && queriesChecker(req) && headersChecker(req) && pathChecker(req) && methodChecker(req));
|
|
186
|
+
}
|
|
187
|
+
createMatcher(mockRequest, response) {
|
|
188
|
+
const matcher = this.requestMatcher(mockRequest);
|
|
189
|
+
return (request) => matcher(request).then((match) => _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
190
|
+
if (match) {
|
|
191
|
+
const { response: resp, delay } = response.build();
|
|
192
|
+
if (delay) {
|
|
193
|
+
yield future.Future.sleep(delay);
|
|
194
|
+
}
|
|
195
|
+
return resp;
|
|
196
|
+
}
|
|
197
|
+
}));
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Mocks network requests send via fetch API
|
|
202
|
+
*/
|
|
203
|
+
class MockNetworkService {
|
|
204
|
+
/**
|
|
205
|
+
* @param blockUnmatchedOrigins check whether to block requests sent to origins not specified as
|
|
206
|
+
* an API in this network service
|
|
207
|
+
*/
|
|
208
|
+
constructor(blockUnmatchedOrigins = true) {
|
|
209
|
+
this.blockUnmatchedOrigins = blockUnmatchedOrigins;
|
|
210
|
+
this.realFetch = global.fetch;
|
|
211
|
+
this.apis = new Map();
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Launches the network service to monitor requests
|
|
215
|
+
*/
|
|
216
|
+
start() {
|
|
217
|
+
this.future = future.Future.of((resolve, reject, signal) => {
|
|
218
|
+
signal.onabort = () => {
|
|
219
|
+
global.fetch = this.realFetch;
|
|
220
|
+
reject(new AbortError.AbortError());
|
|
221
|
+
};
|
|
222
|
+
global.fetch = (input, init) => {
|
|
223
|
+
const req = new Request(input, init);
|
|
224
|
+
const uri = new URL(req.url);
|
|
225
|
+
const api = this.apis.get(uri.origin);
|
|
226
|
+
if (!api && this.blockUnmatchedOrigins) {
|
|
227
|
+
return Promise.resolve(new Response(null, { status: http.HTTPStatus.NOT_FOUND }));
|
|
228
|
+
}
|
|
229
|
+
else if (!api) {
|
|
230
|
+
return this.realFetch(input, init);
|
|
231
|
+
}
|
|
232
|
+
return api.resolve(req);
|
|
233
|
+
};
|
|
234
|
+
}).schedule();
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Stops network monitoring
|
|
238
|
+
*/
|
|
239
|
+
stop() {
|
|
240
|
+
if (this.future) {
|
|
241
|
+
this.future.cancel();
|
|
242
|
+
this.future = undefined;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Creates an API service - used to intercept requests for a given origin
|
|
247
|
+
* @param origin
|
|
248
|
+
*/
|
|
249
|
+
createAPI(origin) {
|
|
250
|
+
const api = new API();
|
|
251
|
+
this.apis.set(origin, api);
|
|
252
|
+
return api;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Remove all created API services from network request routing
|
|
256
|
+
*/
|
|
257
|
+
reset() {
|
|
258
|
+
this.apis.clear();
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Validators
|
|
263
|
+
*/
|
|
264
|
+
/**
|
|
265
|
+
* Validates that the request was processed at least X times
|
|
266
|
+
* @param amount number of times the request was sent
|
|
267
|
+
*/
|
|
268
|
+
const atLeast = (amount) => (matches) => matches.length >= amount;
|
|
269
|
+
/**
|
|
270
|
+
* Validates that the request was process at most X times
|
|
271
|
+
* @param amount number of times the request was sent
|
|
272
|
+
*/
|
|
273
|
+
const atMost = (amount) => (matches) => matches.length <= amount;
|
|
274
|
+
/**
|
|
275
|
+
* Validates that the request was processed exactly X times
|
|
276
|
+
* @param amount number of times the request was sent
|
|
277
|
+
*/
|
|
278
|
+
const exact = (amount) => (matches) => matches.length === amount;
|
|
279
|
+
/**
|
|
280
|
+
* Validates that the request was processed within the given range of times
|
|
281
|
+
* @param lower minimum times the request matched
|
|
282
|
+
* @param upper maximum times the request matched
|
|
283
|
+
* @param includeUpper if true, the upper limit provided with be included. Otherwise, the maximum will be upper - 1
|
|
284
|
+
*/
|
|
285
|
+
const range = (lower, upper, includeUpper = false) => (matches) => matches.length >= lower && (includeUpper ? matches.length <= upper : matches.length < upper);
|
|
286
|
+
/**
|
|
287
|
+
* creates a request details instance that is used to track requests with a given set of criteria
|
|
288
|
+
*/
|
|
289
|
+
const request = () => new RequestDetails();
|
|
290
|
+
/**
|
|
291
|
+
* creates a response builder
|
|
292
|
+
*/
|
|
293
|
+
const response = () => new ResponseBuilder();
|
|
294
|
+
|
|
295
|
+
exports.MockNetworkService = MockNetworkService;
|
|
296
|
+
exports.atLeast = atLeast;
|
|
297
|
+
exports.atMost = atMost;
|
|
298
|
+
exports.exact = exact;
|
|
299
|
+
exports.range = range;
|
|
300
|
+
exports.request = request;
|
|
301
|
+
exports.response = response;
|
package/mocks.d.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { RequestType } from "./networking/types";
|
|
2
|
+
import { HTTPStatus } from "./networking";
|
|
3
|
+
import { WaitPeriod } from "./future";
|
|
4
|
+
type Validator = (matches: Response[]) => boolean;
|
|
5
|
+
/**
|
|
6
|
+
* Tracks specific details that should be used to match specific given network request
|
|
7
|
+
*/
|
|
8
|
+
declare class RequestDetails {
|
|
9
|
+
private method?;
|
|
10
|
+
private path?;
|
|
11
|
+
private headers;
|
|
12
|
+
private queries;
|
|
13
|
+
private body?;
|
|
14
|
+
constructor();
|
|
15
|
+
withMethod(method: RequestType): this;
|
|
16
|
+
withPath(path: string): this;
|
|
17
|
+
withHeader(key: string, value: string): this;
|
|
18
|
+
withHeaders(value: {
|
|
19
|
+
[string: string]: string;
|
|
20
|
+
}): this;
|
|
21
|
+
withQuery(key: string, value: string): this;
|
|
22
|
+
withQueries(value: {
|
|
23
|
+
[string: string]: string;
|
|
24
|
+
}): this;
|
|
25
|
+
withBody(value: string | any): this;
|
|
26
|
+
}
|
|
27
|
+
declare class ResponseBuilder {
|
|
28
|
+
private status;
|
|
29
|
+
private headers;
|
|
30
|
+
private body?;
|
|
31
|
+
private uri?;
|
|
32
|
+
private fragment?;
|
|
33
|
+
private delay?;
|
|
34
|
+
constructor();
|
|
35
|
+
withStatus(status: HTTPStatus): this;
|
|
36
|
+
withHeader(key: string, value: string): this;
|
|
37
|
+
withHeaders(value: {
|
|
38
|
+
[string: string]: string;
|
|
39
|
+
}): this;
|
|
40
|
+
withBody(value: string | Blob): this;
|
|
41
|
+
withURI(value: string): this;
|
|
42
|
+
withFragment(value: string): this;
|
|
43
|
+
withDelay(value: WaitPeriod): this;
|
|
44
|
+
build(): {
|
|
45
|
+
response: Response;
|
|
46
|
+
delay: WaitPeriod;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
declare class API {
|
|
50
|
+
private readonly matchers;
|
|
51
|
+
private processedRequests;
|
|
52
|
+
constructor();
|
|
53
|
+
private resolve;
|
|
54
|
+
when(request: RequestDetails, response: ResponseBuilder): this;
|
|
55
|
+
verify(request: RequestDetails, validator: Validator): Promise<void>;
|
|
56
|
+
reset(): void;
|
|
57
|
+
private findMatchingResponse;
|
|
58
|
+
private requestMatcher;
|
|
59
|
+
private createMatcher;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Mocks network requests send via fetch API
|
|
63
|
+
*/
|
|
64
|
+
export declare class MockNetworkService {
|
|
65
|
+
private readonly blockUnmatchedOrigins;
|
|
66
|
+
private readonly realFetch;
|
|
67
|
+
private future?;
|
|
68
|
+
private readonly apis;
|
|
69
|
+
/**
|
|
70
|
+
* @param blockUnmatchedOrigins check whether to block requests sent to origins not specified as
|
|
71
|
+
* an API in this network service
|
|
72
|
+
*/
|
|
73
|
+
constructor(blockUnmatchedOrigins?: boolean);
|
|
74
|
+
/**
|
|
75
|
+
* Launches the network service to monitor requests
|
|
76
|
+
*/
|
|
77
|
+
start(): void;
|
|
78
|
+
/**
|
|
79
|
+
* Stops network monitoring
|
|
80
|
+
*/
|
|
81
|
+
stop(): void;
|
|
82
|
+
/**
|
|
83
|
+
* Creates an API service - used to intercept requests for a given origin
|
|
84
|
+
* @param origin
|
|
85
|
+
*/
|
|
86
|
+
createAPI(origin: string): API;
|
|
87
|
+
/**
|
|
88
|
+
* Remove all created API services from network request routing
|
|
89
|
+
*/
|
|
90
|
+
reset(): void;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Validators
|
|
94
|
+
*/
|
|
95
|
+
/**
|
|
96
|
+
* Validates that the request was processed at least X times
|
|
97
|
+
* @param amount number of times the request was sent
|
|
98
|
+
*/
|
|
99
|
+
export declare const atLeast: (amount: number) => (matches: Response[]) => boolean;
|
|
100
|
+
/**
|
|
101
|
+
* Validates that the request was process at most X times
|
|
102
|
+
* @param amount number of times the request was sent
|
|
103
|
+
*/
|
|
104
|
+
export declare const atMost: (amount: number) => (matches: Response[]) => boolean;
|
|
105
|
+
/**
|
|
106
|
+
* Validates that the request was processed exactly X times
|
|
107
|
+
* @param amount number of times the request was sent
|
|
108
|
+
*/
|
|
109
|
+
export declare const exact: (amount: number) => (matches: Response[]) => boolean;
|
|
110
|
+
/**
|
|
111
|
+
* Validates that the request was processed within the given range of times
|
|
112
|
+
* @param lower minimum times the request matched
|
|
113
|
+
* @param upper maximum times the request matched
|
|
114
|
+
* @param includeUpper if true, the upper limit provided with be included. Otherwise, the maximum will be upper - 1
|
|
115
|
+
*/
|
|
116
|
+
export declare const range: (lower: number, upper: number, includeUpper?: boolean) => (matches: Response[]) => boolean;
|
|
117
|
+
/**
|
|
118
|
+
* creates a request details instance that is used to track requests with a given set of criteria
|
|
119
|
+
*/
|
|
120
|
+
export declare const request: () => RequestDetails;
|
|
121
|
+
/**
|
|
122
|
+
* creates a response builder
|
|
123
|
+
*/
|
|
124
|
+
export declare const response: () => ResponseBuilder;
|
|
125
|
+
export {};
|