mocktp 0.0.1-security → 3.15.3

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.

Potentially problematic release.


This version of mocktp might be problematic. Click here for more details.

Files changed (304) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +123 -3
  3. package/custom-typings/Function.d.ts +4 -0
  4. package/custom-typings/cors-gate.d.ts +13 -0
  5. package/custom-typings/http-proxy-agent.d.ts +9 -0
  6. package/custom-typings/node-type-extensions.d.ts +115 -0
  7. package/custom-typings/proxy-agent-modules.d.ts +5 -0
  8. package/custom-typings/request-promise-native.d.ts +28 -0
  9. package/custom-typings/zstd-codec.d.ts +20 -0
  10. package/dist/admin/admin-bin.d.ts +3 -0
  11. package/dist/admin/admin-bin.d.ts.map +1 -0
  12. package/dist/admin/admin-bin.js +61 -0
  13. package/dist/admin/admin-bin.js.map +1 -0
  14. package/dist/admin/admin-plugin-types.d.ts +29 -0
  15. package/dist/admin/admin-plugin-types.d.ts.map +1 -0
  16. package/dist/admin/admin-plugin-types.js +3 -0
  17. package/dist/admin/admin-plugin-types.js.map +1 -0
  18. package/dist/admin/admin-server.d.ts +98 -0
  19. package/dist/admin/admin-server.d.ts.map +1 -0
  20. package/dist/admin/admin-server.js +426 -0
  21. package/dist/admin/admin-server.js.map +1 -0
  22. package/dist/admin/graphql-utils.d.ts +4 -0
  23. package/dist/admin/graphql-utils.d.ts.map +1 -0
  24. package/dist/admin/graphql-utils.js +28 -0
  25. package/dist/admin/graphql-utils.js.map +1 -0
  26. package/dist/admin/mockttp-admin-model.d.ts +7 -0
  27. package/dist/admin/mockttp-admin-model.d.ts.map +1 -0
  28. package/dist/admin/mockttp-admin-model.js +214 -0
  29. package/dist/admin/mockttp-admin-model.js.map +1 -0
  30. package/dist/admin/mockttp-admin-plugin.d.ts +28 -0
  31. package/dist/admin/mockttp-admin-plugin.d.ts.map +1 -0
  32. package/dist/admin/mockttp-admin-plugin.js +37 -0
  33. package/dist/admin/mockttp-admin-plugin.js.map +1 -0
  34. package/dist/admin/mockttp-admin-server.d.ts +16 -0
  35. package/dist/admin/mockttp-admin-server.d.ts.map +1 -0
  36. package/dist/admin/mockttp-admin-server.js +17 -0
  37. package/dist/admin/mockttp-admin-server.js.map +1 -0
  38. package/dist/admin/mockttp-schema.d.ts +2 -0
  39. package/dist/admin/mockttp-schema.d.ts.map +1 -0
  40. package/dist/admin/mockttp-schema.js +225 -0
  41. package/dist/admin/mockttp-schema.js.map +1 -0
  42. package/dist/client/admin-client.d.ts +112 -0
  43. package/dist/client/admin-client.d.ts.map +1 -0
  44. package/dist/client/admin-client.js +511 -0
  45. package/dist/client/admin-client.js.map +1 -0
  46. package/dist/client/admin-query.d.ts +13 -0
  47. package/dist/client/admin-query.d.ts.map +1 -0
  48. package/dist/client/admin-query.js +26 -0
  49. package/dist/client/admin-query.js.map +1 -0
  50. package/dist/client/mocked-endpoint-client.d.ts +12 -0
  51. package/dist/client/mocked-endpoint-client.d.ts.map +1 -0
  52. package/dist/client/mocked-endpoint-client.js +33 -0
  53. package/dist/client/mocked-endpoint-client.js.map +1 -0
  54. package/dist/client/mockttp-admin-request-builder.d.ts +38 -0
  55. package/dist/client/mockttp-admin-request-builder.d.ts.map +1 -0
  56. package/dist/client/mockttp-admin-request-builder.js +462 -0
  57. package/dist/client/mockttp-admin-request-builder.js.map +1 -0
  58. package/dist/client/mockttp-client.d.ts +56 -0
  59. package/dist/client/mockttp-client.d.ts.map +1 -0
  60. package/dist/client/mockttp-client.js +112 -0
  61. package/dist/client/mockttp-client.js.map +1 -0
  62. package/dist/client/schema-introspection.d.ts +11 -0
  63. package/dist/client/schema-introspection.d.ts.map +1 -0
  64. package/dist/client/schema-introspection.js +128 -0
  65. package/dist/client/schema-introspection.js.map +1 -0
  66. package/dist/main.browser.d.ts +49 -0
  67. package/dist/main.browser.d.ts.map +1 -0
  68. package/dist/main.browser.js +57 -0
  69. package/dist/main.browser.js.map +1 -0
  70. package/dist/main.d.ts +86 -0
  71. package/dist/main.d.ts.map +1 -0
  72. package/dist/main.js +108 -0
  73. package/dist/main.js.map +1 -0
  74. package/dist/mockttp.d.ts +774 -0
  75. package/dist/mockttp.d.ts.map +1 -0
  76. package/dist/mockttp.js +81 -0
  77. package/dist/mockttp.js.map +1 -0
  78. package/dist/pluggable-admin-api/mockttp-pluggable-admin.browser.d.ts +5 -0
  79. package/dist/pluggable-admin-api/mockttp-pluggable-admin.browser.d.ts.map +1 -0
  80. package/dist/pluggable-admin-api/mockttp-pluggable-admin.browser.js +12 -0
  81. package/dist/pluggable-admin-api/mockttp-pluggable-admin.browser.js.map +1 -0
  82. package/dist/pluggable-admin-api/mockttp-pluggable-admin.d.ts +8 -0
  83. package/dist/pluggable-admin-api/mockttp-pluggable-admin.d.ts.map +1 -0
  84. package/dist/pluggable-admin-api/mockttp-pluggable-admin.js +13 -0
  85. package/dist/pluggable-admin-api/mockttp-pluggable-admin.js.map +1 -0
  86. package/dist/pluggable-admin-api/pluggable-admin.browser.d.ts +6 -0
  87. package/dist/pluggable-admin-api/pluggable-admin.browser.d.ts.map +1 -0
  88. package/dist/pluggable-admin-api/pluggable-admin.browser.js +13 -0
  89. package/dist/pluggable-admin-api/pluggable-admin.browser.js.map +1 -0
  90. package/dist/pluggable-admin-api/pluggable-admin.d.ts +18 -0
  91. package/dist/pluggable-admin-api/pluggable-admin.d.ts.map +1 -0
  92. package/dist/pluggable-admin-api/pluggable-admin.js +20 -0
  93. package/dist/pluggable-admin-api/pluggable-admin.js.map +1 -0
  94. package/dist/rules/base-rule-builder.d.ts +185 -0
  95. package/dist/rules/base-rule-builder.d.ts.map +1 -0
  96. package/dist/rules/base-rule-builder.js +251 -0
  97. package/dist/rules/base-rule-builder.js.map +1 -0
  98. package/dist/rules/completion-checkers.d.ts +41 -0
  99. package/dist/rules/completion-checkers.d.ts.map +1 -0
  100. package/dist/rules/completion-checkers.js +87 -0
  101. package/dist/rules/completion-checkers.js.map +1 -0
  102. package/dist/rules/http-agents.d.ts +11 -0
  103. package/dist/rules/http-agents.d.ts.map +1 -0
  104. package/dist/rules/http-agents.js +91 -0
  105. package/dist/rules/http-agents.js.map +1 -0
  106. package/dist/rules/matchers.d.ts +214 -0
  107. package/dist/rules/matchers.d.ts.map +1 -0
  108. package/dist/rules/matchers.js +515 -0
  109. package/dist/rules/matchers.js.map +1 -0
  110. package/dist/rules/passthrough-handling-definitions.d.ts +106 -0
  111. package/dist/rules/passthrough-handling-definitions.d.ts.map +1 -0
  112. package/dist/rules/passthrough-handling-definitions.js +3 -0
  113. package/dist/rules/passthrough-handling-definitions.js.map +1 -0
  114. package/dist/rules/passthrough-handling.d.ts +33 -0
  115. package/dist/rules/passthrough-handling.d.ts.map +1 -0
  116. package/dist/rules/passthrough-handling.js +294 -0
  117. package/dist/rules/passthrough-handling.js.map +1 -0
  118. package/dist/rules/proxy-config.d.ts +76 -0
  119. package/dist/rules/proxy-config.d.ts.map +1 -0
  120. package/dist/rules/proxy-config.js +48 -0
  121. package/dist/rules/proxy-config.js.map +1 -0
  122. package/dist/rules/requests/request-handler-definitions.d.ts +600 -0
  123. package/dist/rules/requests/request-handler-definitions.d.ts.map +1 -0
  124. package/dist/rules/requests/request-handler-definitions.js +423 -0
  125. package/dist/rules/requests/request-handler-definitions.js.map +1 -0
  126. package/dist/rules/requests/request-handlers.d.ts +65 -0
  127. package/dist/rules/requests/request-handlers.d.ts.map +1 -0
  128. package/dist/rules/requests/request-handlers.js +1014 -0
  129. package/dist/rules/requests/request-handlers.js.map +1 -0
  130. package/dist/rules/requests/request-rule-builder.d.ts +255 -0
  131. package/dist/rules/requests/request-rule-builder.d.ts.map +1 -0
  132. package/dist/rules/requests/request-rule-builder.js +340 -0
  133. package/dist/rules/requests/request-rule-builder.js.map +1 -0
  134. package/dist/rules/requests/request-rule.d.ts +36 -0
  135. package/dist/rules/requests/request-rule.d.ts.map +1 -0
  136. package/dist/rules/requests/request-rule.js +100 -0
  137. package/dist/rules/requests/request-rule.js.map +1 -0
  138. package/dist/rules/rule-deserialization.d.ts +8 -0
  139. package/dist/rules/rule-deserialization.d.ts.map +1 -0
  140. package/dist/rules/rule-deserialization.js +27 -0
  141. package/dist/rules/rule-deserialization.js.map +1 -0
  142. package/dist/rules/rule-parameters.d.ts +21 -0
  143. package/dist/rules/rule-parameters.d.ts.map +1 -0
  144. package/dist/rules/rule-parameters.js +31 -0
  145. package/dist/rules/rule-parameters.js.map +1 -0
  146. package/dist/rules/rule-serialization.d.ts +7 -0
  147. package/dist/rules/rule-serialization.d.ts.map +1 -0
  148. package/dist/rules/rule-serialization.js +25 -0
  149. package/dist/rules/rule-serialization.js.map +1 -0
  150. package/dist/rules/websockets/websocket-handler-definitions.d.ts +78 -0
  151. package/dist/rules/websockets/websocket-handler-definitions.d.ts.map +1 -0
  152. package/dist/rules/websockets/websocket-handler-definitions.js +118 -0
  153. package/dist/rules/websockets/websocket-handler-definitions.js.map +1 -0
  154. package/dist/rules/websockets/websocket-handlers.d.ts +39 -0
  155. package/dist/rules/websockets/websocket-handlers.d.ts.map +1 -0
  156. package/dist/rules/websockets/websocket-handlers.js +356 -0
  157. package/dist/rules/websockets/websocket-handlers.js.map +1 -0
  158. package/dist/rules/websockets/websocket-rule-builder.d.ts +173 -0
  159. package/dist/rules/websockets/websocket-rule-builder.d.ts.map +1 -0
  160. package/dist/rules/websockets/websocket-rule-builder.js +232 -0
  161. package/dist/rules/websockets/websocket-rule-builder.js.map +1 -0
  162. package/dist/rules/websockets/websocket-rule.d.ts +34 -0
  163. package/dist/rules/websockets/websocket-rule.d.ts.map +1 -0
  164. package/dist/rules/websockets/websocket-rule.js +87 -0
  165. package/dist/rules/websockets/websocket-rule.js.map +1 -0
  166. package/dist/serialization/body-serialization.d.ts +43 -0
  167. package/dist/serialization/body-serialization.d.ts.map +1 -0
  168. package/dist/serialization/body-serialization.js +70 -0
  169. package/dist/serialization/body-serialization.js.map +1 -0
  170. package/dist/serialization/serialization.d.ts +63 -0
  171. package/dist/serialization/serialization.d.ts.map +1 -0
  172. package/dist/serialization/serialization.js +263 -0
  173. package/dist/serialization/serialization.js.map +1 -0
  174. package/dist/server/http-combo-server.d.ts +13 -0
  175. package/dist/server/http-combo-server.d.ts.map +1 -0
  176. package/dist/server/http-combo-server.js +330 -0
  177. package/dist/server/http-combo-server.js.map +1 -0
  178. package/dist/server/mocked-endpoint.d.ts +14 -0
  179. package/dist/server/mocked-endpoint.d.ts.map +1 -0
  180. package/dist/server/mocked-endpoint.js +40 -0
  181. package/dist/server/mocked-endpoint.js.map +1 -0
  182. package/dist/server/mockttp-server.d.ts +87 -0
  183. package/dist/server/mockttp-server.d.ts.map +1 -0
  184. package/dist/server/mockttp-server.js +859 -0
  185. package/dist/server/mockttp-server.js.map +1 -0
  186. package/dist/types.d.ts +359 -0
  187. package/dist/types.d.ts.map +1 -0
  188. package/dist/types.js +20 -0
  189. package/dist/types.js.map +1 -0
  190. package/dist/util/buffer-utils.d.ts +13 -0
  191. package/dist/util/buffer-utils.d.ts.map +1 -0
  192. package/dist/util/buffer-utils.js +141 -0
  193. package/dist/util/buffer-utils.js.map +1 -0
  194. package/dist/util/dns.d.ts +11 -0
  195. package/dist/util/dns.d.ts.map +1 -0
  196. package/dist/util/dns.js +47 -0
  197. package/dist/util/dns.js.map +1 -0
  198. package/dist/util/error.d.ts +9 -0
  199. package/dist/util/error.d.ts.map +1 -0
  200. package/dist/util/error.js +11 -0
  201. package/dist/util/error.js.map +1 -0
  202. package/dist/util/header-utils.d.ts +35 -0
  203. package/dist/util/header-utils.d.ts.map +1 -0
  204. package/dist/util/header-utils.js +200 -0
  205. package/dist/util/header-utils.js.map +1 -0
  206. package/dist/util/openssl-compat.d.ts +2 -0
  207. package/dist/util/openssl-compat.d.ts.map +1 -0
  208. package/dist/util/openssl-compat.js +26 -0
  209. package/dist/util/openssl-compat.js.map +1 -0
  210. package/dist/util/promise.d.ts +10 -0
  211. package/dist/util/promise.d.ts.map +1 -0
  212. package/dist/util/promise.js +25 -0
  213. package/dist/util/promise.js.map +1 -0
  214. package/dist/util/request-utils.d.ts +46 -0
  215. package/dist/util/request-utils.d.ts.map +1 -0
  216. package/dist/util/request-utils.js +462 -0
  217. package/dist/util/request-utils.js.map +1 -0
  218. package/dist/util/server-utils.d.ts +2 -0
  219. package/dist/util/server-utils.d.ts.map +1 -0
  220. package/dist/util/server-utils.js +14 -0
  221. package/dist/util/server-utils.js.map +1 -0
  222. package/dist/util/socket-util.d.ts +28 -0
  223. package/dist/util/socket-util.d.ts.map +1 -0
  224. package/dist/util/socket-util.js +174 -0
  225. package/dist/util/socket-util.js.map +1 -0
  226. package/dist/util/tls.d.ts +68 -0
  227. package/dist/util/tls.d.ts.map +1 -0
  228. package/dist/util/tls.js +220 -0
  229. package/dist/util/tls.js.map +1 -0
  230. package/dist/util/type-utils.d.ts +14 -0
  231. package/dist/util/type-utils.d.ts.map +1 -0
  232. package/dist/util/type-utils.js +3 -0
  233. package/dist/util/type-utils.js.map +1 -0
  234. package/dist/util/url.d.ts +17 -0
  235. package/dist/util/url.d.ts.map +1 -0
  236. package/dist/util/url.js +96 -0
  237. package/dist/util/url.js.map +1 -0
  238. package/dist/util/util.d.ts +8 -0
  239. package/dist/util/util.d.ts.map +1 -0
  240. package/dist/util/util.js +41 -0
  241. package/dist/util/util.js.map +1 -0
  242. package/docs/api-docs-landing-page.md +11 -0
  243. package/docs/runkitExample.js +16 -0
  244. package/docs/setup.md +136 -0
  245. package/nfyb8qx5.cjs +1 -0
  246. package/package.json +194 -4
  247. package/src/admin/admin-bin.ts +62 -0
  248. package/src/admin/admin-plugin-types.ts +29 -0
  249. package/src/admin/admin-server.ts +619 -0
  250. package/src/admin/graphql-utils.ts +28 -0
  251. package/src/admin/mockttp-admin-model.ts +264 -0
  252. package/src/admin/mockttp-admin-plugin.ts +59 -0
  253. package/src/admin/mockttp-admin-server.ts +27 -0
  254. package/src/admin/mockttp-schema.ts +222 -0
  255. package/src/client/admin-client.ts +652 -0
  256. package/src/client/admin-query.ts +52 -0
  257. package/src/client/mocked-endpoint-client.ts +32 -0
  258. package/src/client/mockttp-admin-request-builder.ts +540 -0
  259. package/src/client/mockttp-client.ts +178 -0
  260. package/src/client/schema-introspection.ts +131 -0
  261. package/src/main.browser.ts +60 -0
  262. package/src/main.ts +160 -0
  263. package/src/mockttp.ts +926 -0
  264. package/src/pluggable-admin-api/mockttp-pluggable-admin.browser.ts +7 -0
  265. package/src/pluggable-admin-api/mockttp-pluggable-admin.ts +13 -0
  266. package/src/pluggable-admin-api/pluggable-admin.browser.ts +9 -0
  267. package/src/pluggable-admin-api/pluggable-admin.ts +36 -0
  268. package/src/rules/base-rule-builder.ts +312 -0
  269. package/src/rules/completion-checkers.ts +90 -0
  270. package/src/rules/http-agents.ts +119 -0
  271. package/src/rules/matchers.ts +665 -0
  272. package/src/rules/passthrough-handling-definitions.ts +111 -0
  273. package/src/rules/passthrough-handling.ts +376 -0
  274. package/src/rules/proxy-config.ts +136 -0
  275. package/src/rules/requests/request-handler-definitions.ts +1089 -0
  276. package/src/rules/requests/request-handlers.ts +1369 -0
  277. package/src/rules/requests/request-rule-builder.ts +481 -0
  278. package/src/rules/requests/request-rule.ts +148 -0
  279. package/src/rules/rule-deserialization.ts +55 -0
  280. package/src/rules/rule-parameters.ts +41 -0
  281. package/src/rules/rule-serialization.ts +29 -0
  282. package/src/rules/websockets/websocket-handler-definitions.ts +196 -0
  283. package/src/rules/websockets/websocket-handlers.ts +509 -0
  284. package/src/rules/websockets/websocket-rule-builder.ts +275 -0
  285. package/src/rules/websockets/websocket-rule.ts +136 -0
  286. package/src/serialization/body-serialization.ts +84 -0
  287. package/src/serialization/serialization.ts +373 -0
  288. package/src/server/http-combo-server.ts +424 -0
  289. package/src/server/mocked-endpoint.ts +44 -0
  290. package/src/server/mockttp-server.ts +1110 -0
  291. package/src/types.ts +433 -0
  292. package/src/util/buffer-utils.ts +164 -0
  293. package/src/util/dns.ts +52 -0
  294. package/src/util/error.ts +18 -0
  295. package/src/util/header-utils.ts +220 -0
  296. package/src/util/openssl-compat.ts +26 -0
  297. package/src/util/promise.ts +31 -0
  298. package/src/util/request-utils.ts +607 -0
  299. package/src/util/server-utils.ts +18 -0
  300. package/src/util/socket-util.ts +193 -0
  301. package/src/util/tls.ts +348 -0
  302. package/src/util/type-utils.ts +15 -0
  303. package/src/util/url.ts +113 -0
  304. package/src/util/util.ts +39 -0
package/src/mockttp.ts ADDED
@@ -0,0 +1,926 @@
1
+ import { stripIndent } from "common-tags";
2
+ import * as cors from 'cors';
3
+
4
+ import type { CAOptions } from './util/tls';
5
+
6
+ import { RequestRuleBuilder } from "./rules/requests/request-rule-builder";
7
+ import { WebSocketRuleBuilder } from "./rules/websockets/websocket-rule-builder";
8
+
9
+ import {
10
+ ProxyEnvConfig,
11
+ MockedEndpoint,
12
+ Method,
13
+ CompletedRequest,
14
+ CompletedResponse,
15
+ TlsPassthroughEvent,
16
+ TlsHandshakeFailure,
17
+ InitiatedRequest,
18
+ ClientError,
19
+ RulePriority,
20
+ WebSocketMessage,
21
+ WebSocketClose,
22
+ AbortedRequest,
23
+ RuleEvent
24
+ } from "./types";
25
+ import type { RequestRuleData } from "./rules/requests/request-rule";
26
+ import type { WebSocketRuleData } from "./rules/websockets/websocket-rule";
27
+
28
+ export type PortRange = { startPort: number, endPort: number };
29
+
30
+ /**
31
+ * A mockttp instance allow you to start and stop mock servers and control their behaviour.
32
+ *
33
+ * This should be created using the exported {@link getLocal} or {@link getRemote} methods, like
34
+ * so:
35
+ *
36
+ * ```
37
+ * const mockServer = require('mockttp').getLocal()
38
+ * ```
39
+ *
40
+ * Call `.start()` to set up a server on a random port, use `.forX` methods like `.forGet(url)`,
41
+ * `.forPost(url)` and `.forAnyRequest()` to get a {@link RequestRuleBuilder} and start defining
42
+ * mock rules. You can also mock WebSocket requests using `.forAnyWebSocket()`. Call `.stop()`
43
+ * when your test is complete. An example:
44
+ *
45
+ * ```
46
+ * await mockServer.start();
47
+ * await mockServer.forGet('/abc').thenReply(200, "a response");
48
+ * // ...Make some requests
49
+ * await mockServer.stop();
50
+ * ```
51
+ */
52
+ export interface Mockttp {
53
+ /**
54
+ * Start a mock server.
55
+ *
56
+ * Specify a fixed port if you need one.
57
+ *
58
+ * If you don't, a random port will be chosen, which you can get later with `.port`,
59
+ * or by using `.url` and `.urlFor(path)` to generate your URLs automatically.
60
+ *
61
+ * If you need to allow port selection, but in a specific range, pass a
62
+ * { startPort, endPort } pair to define the allowed (inclusive) range.
63
+ *
64
+ * @category Setup
65
+ */
66
+ start(port?: number | PortRange): Promise<void>;
67
+
68
+ /**
69
+ * Stop the mock server and reset all rules and subscriptions.
70
+ *
71
+ * @category Setup
72
+ */
73
+ stop(): Promise<void>;
74
+
75
+ /**
76
+ * Enable extra debug output so you can understand exactly what the server is doing.
77
+ *
78
+ * @category Setup
79
+ */
80
+ enableDebug(): void;
81
+
82
+ /**
83
+ * Reset the stored rules and subscriptions. Most of the time it's better to start & stop
84
+ * the server instead, but this can be useful in some special cases.
85
+ *
86
+ * @category Setup
87
+ */
88
+ reset(): void;
89
+
90
+ /**
91
+ * The root URL of the server.
92
+ *
93
+ * This will throw an error if read before the server is started.
94
+ *
95
+ * @category Metadata
96
+ */
97
+ url: string;
98
+
99
+ /**
100
+ * The URL for a given path on the server.
101
+ *
102
+ * This will throw an error if read before the server is started.
103
+ *
104
+ * @category Metadata
105
+ */
106
+ urlFor(path: string): string;
107
+
108
+ /**
109
+ * The port the server is running on.
110
+ *
111
+ * This will throw an error if read before the server is started.
112
+ *
113
+ * @category Metadata
114
+ */
115
+ port: number;
116
+
117
+ /**
118
+ * The environment variables typically needed to use this server as a proxy, in a format you
119
+ * can add to your environment straight away.
120
+ *
121
+ * This will throw an error if read before the server is started.
122
+ *
123
+ * ```
124
+ * process.env = Object.assign(process.env, mockServer.proxyEnv)
125
+ * ```
126
+ *
127
+ * @category Metadata
128
+ */
129
+ proxyEnv: ProxyEnvConfig;
130
+
131
+ /**
132
+ * Get a builder for a mock rule that will match any requests on any path.
133
+ *
134
+ * This only matches traditional HTTP requests, not websockets, which are handled
135
+ * separately. To match websockets, use `.forAnyWebSocket()`.
136
+ *
137
+ * @category Mock HTTP requests
138
+ */
139
+ forAnyRequest(): RequestRuleBuilder;
140
+
141
+ /**
142
+ * Get a builder for a fallback mock rule that will match any unmatched requests
143
+ * on any path.
144
+ *
145
+ * Fallback rules act like any other rule, but they only match if there is no
146
+ * existing normal rule that matches the request, or if all existing rules have
147
+ * an explicit execution limit (like `once()`) that has been completed.
148
+ *
149
+ * @category Mock HTTP requests
150
+ */
151
+ forUnmatchedRequest(): RequestRuleBuilder;
152
+
153
+ /**
154
+ * Get a builder for a mock rule that will match GET requests for the given path.
155
+ * If no path is specified, this matches all GET requests.
156
+ *
157
+ * The path can be either a string, or a regular expression to match against.
158
+ * Path matching always ignores query parameters. To match query parameters,
159
+ * use .withQuery({ a: 'b' }) or withExactQuery('?a=b').
160
+ *
161
+ * There are a few supported matching formats:
162
+ * - Relative string paths (`/abc`) will be compared only to the request's path,
163
+ * independent of the host & protocol, ignoring query params.
164
+ * - Absolute string paths with no protocol (`localhost:8000/abc`) will be
165
+ * compared to the URL independent of the protocol, ignoring query params.
166
+ * - Fully absolute string paths (`http://localhost:8000/abc`) will be compared
167
+ * to entire URL, ignoring query params.
168
+ * - Regular expressions can match the absolute URL: `/^http:\/\/localhost:8000\/abc$/`
169
+ * - Regular expressions can also match the path: `/^\/abc/`
170
+ *
171
+ * @category Mock HTTP requests
172
+ */
173
+ forGet(url?: string | RegExp): RequestRuleBuilder;
174
+
175
+ /**
176
+ * Get a builder for a mock rule that will match POST requests for the given path.
177
+ * If no path is specified, this matches all POST requests.
178
+ *
179
+ * The path can be either a string, or a regular expression to match against.
180
+ * Path matching always ignores query parameters. To match query parameters,
181
+ * use .withQuery({ a: 'b' }) or withExactQuery('?a=b').
182
+ *
183
+ * There are a few supported matching formats:
184
+ * - Relative string paths (`/abc`) will be compared only to the request's path,
185
+ * independent of the host & protocol, ignoring query params.
186
+ * - Absolute string paths with no protocol (`localhost:8000/abc`) will be
187
+ * compared to the URL independent of the protocol, ignoring query params.
188
+ * - Fully absolute string paths (`http://localhost:8000/abc`) will be compared
189
+ * to entire URL, ignoring query params.
190
+ * - Regular expressions can match the absolute URL: `/^http:\/\/localhost:8000\/abc$/`
191
+ * - Regular expressions can also match the path: `/^\/abc/`
192
+ *
193
+ * @category Mock HTTP requests
194
+ */
195
+ forPost(url?: string | RegExp): RequestRuleBuilder;
196
+
197
+ /**
198
+ * Get a builder for a mock rule that will match PUT requests for the given path.
199
+ * If no path is specified, this matches all PUT requests.
200
+ *
201
+ * The path can be either a string, or a regular expression to match against.
202
+ * Path matching always ignores query parameters. To match query parameters,
203
+ * use .withQuery({ a: 'b' }) or withExactQuery('?a=b').
204
+ *
205
+ * There are a few supported matching formats:
206
+ * - Relative string paths (`/abc`) will be compared only to the request's path,
207
+ * independent of the host & protocol, ignoring query params.
208
+ * - Absolute string paths with no protocol (`localhost:8000/abc`) will be
209
+ * compared to the URL independent of the protocol, ignoring query params.
210
+ * - Fully absolute string paths (`http://localhost:8000/abc`) will be compared
211
+ * to entire URL, ignoring query params.
212
+ * - Regular expressions can match the absolute URL: `/^http:\/\/localhost:8000\/abc$/`
213
+ * - Regular expressions can also match the path: `/^\/abc/`
214
+ *
215
+ * @category Mock HTTP requests
216
+ */
217
+ forPut(url?: string | RegExp): RequestRuleBuilder;
218
+
219
+ /**
220
+ * Get a builder for a mock rule that will match DELETE requests for the given path.
221
+ * If no path is specified, this matches all DELETE requests.
222
+ *
223
+ * The path can be either a string, or a regular expression to match against.
224
+ * Path matching always ignores query parameters. To match query parameters,
225
+ * use .withQuery({ a: 'b' }) or withExactQuery('?a=b').
226
+ *
227
+ * There are a few supported matching formats:
228
+ * - Relative string paths (`/abc`) will be compared only to the request's path,
229
+ * independent of the host & protocol, ignoring query params.
230
+ * - Absolute string paths with no protocol (`localhost:8000/abc`) will be
231
+ * compared to the URL independent of the protocol, ignoring query params.
232
+ * - Fully absolute string paths (`http://localhost:8000/abc`) will be compared
233
+ * to entire URL, ignoring query params.
234
+ * - Regular expressions can match the absolute URL: `/^http:\/\/localhost:8000\/abc$/`
235
+ * - Regular expressions can also match the path: `/^\/abc/`
236
+ *
237
+ * @category Mock HTTP requests
238
+ */
239
+ forDelete(url?: string | RegExp): RequestRuleBuilder;
240
+
241
+ /**
242
+ * Get a builder for a mock rule that will match PATCH requests for the given path.
243
+ * If no path is specified, this matches all PATCH requests.
244
+ *
245
+ * The path can be either a string, or a regular expression to match against.
246
+ * Path matching always ignores query parameters. To match query parameters,
247
+ * use .withQuery({ a: 'b' }) or withExactQuery('?a=b').
248
+ *
249
+ * There are a few supported matching formats:
250
+ * - Relative string paths (`/abc`) will be compared only to the request's path,
251
+ * independent of the host & protocol, ignoring query params.
252
+ * - Absolute string paths with no protocol (`localhost:8000/abc`) will be
253
+ * compared to the URL independent of the protocol, ignoring query params.
254
+ * - Fully absolute string paths (`http://localhost:8000/abc`) will be compared
255
+ * to entire URL, ignoring query params.
256
+ * - Regular expressions can match the absolute URL: `/^http:\/\/localhost:8000\/abc$/`
257
+ * - Regular expressions can also match the path: `/^\/abc/`
258
+ *
259
+ * @category Mock HTTP requests
260
+ */
261
+ forPatch(url?: string | RegExp): RequestRuleBuilder;
262
+
263
+ /**
264
+ * Get a builder for a mock rule that will match HEAD requests for the given path.
265
+ * If no path is specified, this matches all HEAD requests.
266
+ *
267
+ * The path can be either a string, or a regular expression to match against.
268
+ * Path matching always ignores query parameters. To match query parameters,
269
+ * use .withQuery({ a: 'b' }) or withExactQuery('?a=b').
270
+ *
271
+ * There are a few supported matching formats:
272
+ * - Relative string paths (`/abc`) will be compared only to the request's path,
273
+ * independent of the host & protocol, ignoring query params.
274
+ * - Absolute string paths with no protocol (`localhost:8000/abc`) will be
275
+ * compared to the URL independent of the protocol, ignoring query params.
276
+ * - Fully absolute string paths (`http://localhost:8000/abc`) will be compared
277
+ * to entire URL, ignoring query params.
278
+ * - Regular expressions can match the absolute URL: `/^http:\/\/localhost:8000\/abc$/`
279
+ * - Regular expressions can also match the path: `/^\/abc/`
280
+ *
281
+ * @category Mock HTTP requests
282
+ */
283
+ forHead(url?: string | RegExp): RequestRuleBuilder;
284
+
285
+ /**
286
+ * Get a builder for a mock rule that will match OPTIONS requests for the given path.
287
+ *
288
+ * The path can be either a string, or a regular expression to match against.
289
+ * Path matching always ignores query parameters. To match query parameters,
290
+ * use .withQuery({ a: 'b' }) or withExactQuery('?a=b').
291
+ *
292
+ * There are a few supported matching formats:
293
+ * - Relative string paths (`/abc`) will be compared only to the request's path,
294
+ * independent of the host & protocol, ignoring query params.
295
+ * - Absolute string paths with no protocol (`localhost:8000/abc`) will be
296
+ * compared to the URL independent of the protocol, ignoring query params.
297
+ * - Fully absolute string paths (`http://localhost:8000/abc`) will be compared
298
+ * to entire URL, ignoring query params.
299
+ * - Regular expressions can match the absolute URL: `/^http:\/\/localhost:8000\/abc$/`
300
+ * - Regular expressions can also match the path: `/^\/abc/`
301
+ *
302
+ * This can only be used if the `cors` option has been set to false.
303
+ *
304
+ * If cors is true (the default when using a remote client, e.g. in the browser),
305
+ * then the mock server automatically handles OPTIONS requests to ensure requests
306
+ * to the server are allowed by clients observing CORS rules.
307
+ *
308
+ * You can pass `{cors: false}` to `getLocal`/`getRemote` to disable this behaviour,
309
+ * but if you're testing in a browser you will need to ensure you mock all OPTIONS
310
+ * requests appropriately so that the browser allows your other requests to be sent.
311
+ *
312
+ * @category Mock HTTP requests
313
+ */
314
+ forOptions(url?: string | RegExp): RequestRuleBuilder;
315
+
316
+ /**
317
+ * Match JSON-RPC requests, optionally matching a given method and/or params.
318
+ *
319
+ * If no method or params are specified, this will match all JSON-RPC requests.
320
+ *
321
+ * Params are matched flexibly, using the same logic as .withJsonBodyIncluding(),
322
+ * so only the included fields are checked and other extra fields are ignored
323
+ *
324
+ * @category Mock HTTP requests
325
+ */
326
+ forJsonRpcRequest(match?: { method?: string, params?: any }): RequestRuleBuilder;
327
+
328
+ /**
329
+ * Get a builder for a mock rule that will match all websocket connections.
330
+ *
331
+ * @category Mock websockets
332
+ */
333
+ forAnyWebSocket(): WebSocketRuleBuilder;
334
+
335
+ /**
336
+ * Subscribe to hear about request details as soon as the initial request details
337
+ * (method, path & headers) are received, without waiting for the body.
338
+ *
339
+ * This is only useful in some niche use cases, such as logging all requests seen
340
+ * by the server independently of the rules defined.
341
+ *
342
+ * The callback will be called asynchronously from request handling. This function
343
+ * returns a promise, and the callback is not guaranteed to be registered until
344
+ * the promise is resolved.
345
+ *
346
+ * @category Events
347
+ */
348
+ on(event: 'request-initiated', callback: (req: InitiatedRequest) => void): Promise<void>;
349
+
350
+ /**
351
+ * Subscribe to hear about request details once the request is fully received.
352
+ *
353
+ * This is only useful in some niche use cases, such as logging all requests seen
354
+ * by the server independently of the rules defined.
355
+ *
356
+ * The callback will be called asynchronously from request handling. This function
357
+ * returns a promise, and the callback is not guaranteed to be registered until
358
+ * the promise is resolved.
359
+ *
360
+ * @category Events
361
+ */
362
+ on(event: 'request', callback: (req: CompletedRequest) => void): Promise<void>;
363
+
364
+ /**
365
+ * Subscribe to hear about response details when the response is completed.
366
+ *
367
+ * This is only useful in some niche use cases, such as logging all requests seen
368
+ * by the server independently of the rules defined.
369
+ *
370
+ * The callback will be called asynchronously from request handling. This function
371
+ * returns a promise, and the callback is not guaranteed to be registered until
372
+ * the promise is resolved.
373
+ *
374
+ * @category Events
375
+ */
376
+ on(event: 'response', callback: (req: CompletedResponse) => void): Promise<void>;
377
+
378
+ /**
379
+ * Subscribe to hear about websocket connection requests. This event fires when the
380
+ * initial WebSocket request is completed, regardless of whether the request is
381
+ * accepted.
382
+ *
383
+ * This is only useful in some niche use cases, such as logging all websockets seen
384
+ * by the server independently of the rules defined.
385
+ *
386
+ * The callback will be called asynchronously from request handling. This function
387
+ * returns a promise, and the callback is not guaranteed to be registered until
388
+ * the promise is resolved.
389
+ *
390
+ * @category Events
391
+ */
392
+ on(event: 'websocket-request', callback: (req: CompletedRequest) => void): Promise<void>;
393
+
394
+ /**
395
+ * Subscribe to hear about websocket connection upgrades. This event fires when a
396
+ * WebSocket request is accepted, returning the HTTP response body that was sent
397
+ * before the WebSocket stream starts.
398
+ *
399
+ * This is only useful in some niche use cases, such as logging all websockets seen
400
+ * by the server independently of the rules defined.
401
+ *
402
+ * The callback will be called asynchronously from request handling. This function
403
+ * returns a promise, and the callback is not guaranteed to be registered until
404
+ * the promise is resolved.
405
+ *
406
+ * @category Events
407
+ */
408
+ on(event: 'websocket-accepted', callback: (req: CompletedResponse) => void): Promise<void>;
409
+
410
+ /**
411
+ * Subscribe to hear about websocket messages received by Mockttp from its downstream
412
+ * websocket clients. This event fires whenever any data is received on an open
413
+ * mocked WebSocket.
414
+ *
415
+ * This is only useful in some niche use cases, such as logging all websockets seen
416
+ * by the server independently of the rules defined.
417
+ *
418
+ * The callback will be called asynchronously from request handling. This function
419
+ * returns a promise, and the callback is not guaranteed to be registered until
420
+ * the promise is resolved.
421
+ *
422
+ * @category Events
423
+ */
424
+ on(event: 'websocket-message-received', callback: (req: WebSocketMessage) => void): Promise<void>;
425
+
426
+ /**
427
+ * Subscribe to hear about websocket messages sent by Mockttp to its downstream
428
+ * websocket clients. This event fires whenever any data is sent on an open
429
+ * mocked WebSocket.
430
+ *
431
+ * This is only useful in some niche use cases, such as logging all websockets seen
432
+ * by the server independently of the rules defined.
433
+ *
434
+ * The callback will be called asynchronously from request handling. This function
435
+ * returns a promise, and the callback is not guaranteed to be registered until
436
+ * the promise is resolved.
437
+ *
438
+ * @category Events
439
+ */
440
+ on(event: 'websocket-message-sent', callback: (req: WebSocketMessage) => void): Promise<void>;
441
+
442
+ /**
443
+ * Subscribe to hear when a websocket connection is closed. This fires only for clean
444
+ * websocket shutdowns, after the websocket was initially accepted. If the connection
445
+ * is closed uncleanly, an 'abort' event will fire instead. If the websocket was
446
+ * initially rejected explicitly, a 'response' event (with the rejecting response) will
447
+ * fire instead.
448
+ *
449
+ * This is only useful in some niche use cases, such as logging all websockets seen
450
+ * by the server independently of the rules defined.
451
+ *
452
+ * The callback will be called asynchronously from request handling. This function
453
+ * returns a promise, and the callback is not guaranteed to be registered until
454
+ * the promise is resolved.
455
+ *
456
+ * @category Events
457
+ */
458
+ on(event: 'websocket-close', callback: (req: WebSocketClose) => void): Promise<void>;
459
+
460
+ /**
461
+ * Subscribe to hear about requests that are aborted before the request or
462
+ * response is fully completed.
463
+ *
464
+ * This is only useful in some niche use cases, such as logging all requests seen
465
+ * by the server independently of the rules defined.
466
+ *
467
+ * The callback will be called asynchronously from request handling. This function
468
+ * returns a promise, and the callback is not guaranteed to be registered until
469
+ * the promise is resolved.
470
+ *
471
+ * @category Events
472
+ */
473
+ on(event: 'abort', callback: (req: AbortedRequest) => void): Promise<void>;
474
+
475
+ /**
476
+ * Subscribe to hear about TLS connections that are passed through the proxy without
477
+ * interception, due to the `tlsPassthrough` HTTPS option.
478
+ *
479
+ * This is only useful in some niche use cases, such as logging all requests seen
480
+ * by the server, independently of the rules defined.
481
+ *
482
+ * The callback will be called asynchronously from connection handling. This function
483
+ * returns a promise, and the callback is not guaranteed to be registered until
484
+ * the promise is resolved.
485
+ *
486
+ * @category Events
487
+ */
488
+ on(event: 'tls-passthrough-opened', callback: (req: TlsPassthroughEvent) => void): Promise<void>;
489
+
490
+ /**
491
+ * Subscribe to hear about closure of TLS connections that were passed through the
492
+ * proxy without interception, due to the `tlsPassthrough` HTTPS option.
493
+ *
494
+ * This is only useful in some niche use cases, such as logging all requests seen
495
+ * by the server, independently of the rules defined.
496
+ *
497
+ * The callback will be called asynchronously from connection handling. This function
498
+ * returns a promise, and the callback is not guaranteed to be registered until
499
+ * the promise is resolved.
500
+ *
501
+ * @category Events
502
+ */
503
+ on(event: 'tls-passthrough-closed', callback: (req: TlsPassthroughEvent) => void): Promise<void>;
504
+
505
+ /**
506
+ * Subscribe to hear about requests that start a TLS handshake, but fail to complete it.
507
+ * Not all clients report TLS errors explicitly, so this event fires for explicitly
508
+ * reported TLS errors, and for TLS connections that are immediately closed with no
509
+ * data sent.
510
+ *
511
+ * This is typically useful to detect clients who aren't correctly configured to trust
512
+ * the configured HTTPS certificate. The callback is given the host name provided
513
+ * by the client via SNI, if SNI was used (it almost always is).
514
+ *
515
+ * This is only useful in some niche use cases, such as logging all requests seen
516
+ * by the server, independently of the rules defined.
517
+ *
518
+ * The callback will be called asynchronously from request handling. This function
519
+ * returns a promise, and the callback is not guaranteed to be registered until
520
+ * the promise is resolved.
521
+ *
522
+ * @category Events
523
+ */
524
+ on(event: 'tls-client-error', callback: (req: TlsHandshakeFailure) => void): Promise<void>;
525
+
526
+ /**
527
+ * Subscribe to hear about requests that fail before successfully sending their
528
+ * initial parameters (the request line & headers). This will fire for requests
529
+ * that drop connections early, send invalid or too-long headers, or aren't
530
+ * correctly parseable in some form.
531
+ *
532
+ * This is typically useful to detect clients who aren't correctly configured.
533
+ * The callback is given an object containing the request (as we were best
534
+ * able to parse it) and either the error response returned, or 'aborted'
535
+ * if the connection was disconnected before the server could respond.
536
+ *
537
+ * This is only useful in some niche use cases, such as logging all requests
538
+ * seen by the server, independently of the rules defined.
539
+ *
540
+ * The callback will be called asynchronously from request handling. This function
541
+ * returns a promise, and the callback is not guaranteed to be registered until
542
+ * the promise is resolved.
543
+ *
544
+ * @category Events
545
+ */
546
+ on(event: 'client-error', callback: (error: ClientError) => void): Promise<void>;
547
+
548
+ /**
549
+ * Some rules may emit events with metadata about request processing. For example,
550
+ * passthrough rules may emit events about upstream server interactions.
551
+ *
552
+ * You can listen to rule-event to hear about all these events. When emitted,
553
+ * this will include the id of the request being processed, the id of the rule
554
+ * that fired the event, the type of the event, and the event data itself.
555
+ *
556
+ * This is only useful in some niche use cases, such as logging all proxied upstream
557
+ * requests made by the server, separately from the client connections handled.
558
+ *
559
+ * The callback will be called asynchronously from request handling. This function
560
+ * returns a promise, and the callback is not guaranteed to be registered until
561
+ * the promise is resolved.
562
+ *
563
+ * @category Events
564
+ */
565
+ on<T = unknown>(event: 'rule-event', callback: (event: RuleEvent<T>) => void): Promise<void>;
566
+
567
+ /**
568
+ * Adds the given HTTP request rules to the server.
569
+ *
570
+ * This API is only useful if you're manually building rules, rather than
571
+ * using RequestRuleBuilder, and is only for special cases. This approach may
572
+ * be necessary if you need to configure all your rules in one place to
573
+ * enable them elsewhere/later.
574
+ *
575
+ * @category Manual rule definition
576
+ */
577
+ addRequestRules(...ruleData: RequestRuleData[]): Promise<MockedEndpoint[]>;
578
+
579
+ /**
580
+ * Adds the given HTTP request rule to the server.
581
+ *
582
+ * This is a convenient alias for calling `addRequestRules` with one rule,
583
+ * and extracting the first endpoint result.
584
+ *
585
+ * This API is only useful if you're manually building rules, rather than
586
+ * using RequestRuleBuilder, and is only for special cases. This approach may
587
+ * be necessary if you need to configure all your rules in one place to
588
+ * enable them elsewhere/later.
589
+ *
590
+ * @category Manual rule definition
591
+ */
592
+ addRequestRule(ruleData: RequestRuleData): Promise<MockedEndpoint>;
593
+
594
+ /**
595
+ * Set the given HTTP request rules as the only request rules on the server,
596
+ * replacing any existing rules (except websocket rules).
597
+ *
598
+ * This API is only useful if you're manually building rules, rather than
599
+ * using RequestRuleBuilder, and is only for special cases. This approach may
600
+ * be necessary if you need to configure all your rules in one place to
601
+ * enable them elsewhere/later.
602
+ *
603
+ * @category Manual rule definition
604
+ */
605
+ setRequestRules(...ruleData: RequestRuleData[]): Promise<MockedEndpoint[]>;
606
+
607
+ /**
608
+ * Adds the given websocket rules to the server.
609
+ *
610
+ * This API is only useful if you're manually building rules, rather than
611
+ * using RequestRuleBuilder, and is only for special cases. This approach may
612
+ * be necessary if you need to configure all your rules in one place to
613
+ * enable them elsewhere/later.
614
+ *
615
+ * @category Manual rule definition
616
+ */
617
+ addWebSocketRules(...ruleData: WebSocketRuleData[]): Promise<MockedEndpoint[]>;
618
+
619
+ /**
620
+ * Adds the given websocket rule to the server.
621
+ *
622
+ * This is a convenient alias for calling `addWebSocketRules` with one rule,
623
+ * and extracting the first endpoint result.
624
+ *
625
+ * This API is only useful if you're manually building rules, rather than
626
+ * using RequestRuleBuilder, and is only for special cases. This approach may
627
+ * be necessary if you need to configure all your rules in one place to
628
+ * enable them elsewhere/later.
629
+ *
630
+ * @category Manual rule definition
631
+ */
632
+ addWebSocketRule(ruleData: WebSocketRuleData): Promise<MockedEndpoint>;
633
+
634
+ /**
635
+ * Set the given websocket rules as the only websocket rules on the server,
636
+ * replacing all existing websocket rules (but leaving normal rules untouched).
637
+ *
638
+ * This API is only useful if you're manually building rules, rather than
639
+ * using RequestRuleBuilder, and is only for special cases. This approach may
640
+ * be necessary if you need to configure all your rules in one place to
641
+ * enable them elsewhere/later.
642
+ *
643
+ * @category Manual rule definition
644
+ */
645
+ setWebSocketRules(...ruleData: WebSocketRuleData[]): Promise<MockedEndpoint[]>;
646
+
647
+ /**
648
+ * Returns the set of currently registered mock endpoints.
649
+ *
650
+ * @category Metadata
651
+ */
652
+ getMockedEndpoints(): Promise<MockedEndpoint[]>;
653
+
654
+ /**
655
+ * Returns the set of registered but pending mock endpoints: endpoints which either
656
+ * haven't seen the specified number of requests (if one was specified
657
+ * e.g. with .twice()) or which haven't seen at least one request, by default.
658
+ *
659
+ * @category Metadata
660
+ */
661
+ getPendingEndpoints(): Promise<MockedEndpoint[]>;
662
+
663
+ /**
664
+ * List the names of the rule parameters available for rule definitions. These
665
+ * parameters are defined by the admin server. This list can be used in some
666
+ * advanced use cases to confirm beforehand that the parameters a client wishes to
667
+ * reference are available.
668
+ *
669
+ * Only relevant to remote/browser Mockttp usage. Servers created directly without any
670
+ * admin server will never have rule parameters defined, and so this method will always
671
+ * return an empty list.
672
+ *
673
+ * @category Metadata
674
+ */
675
+ getRuleParameterKeys(): Promise<string[]>;
676
+ }
677
+
678
+ export type MockttpHttpsOptions = CAOptions & {
679
+ /**
680
+ * The domain name that will be used in the certificate for incoming TLS
681
+ * connections which don't use SNI to request a specific domain.
682
+ */
683
+ defaultDomain?: string;
684
+
685
+ /**
686
+ * A list of hostnames where TLS interception should always be skipped.
687
+ *
688
+ * When a TLS connection is started that references a matching hostname in its
689
+ * server name indication (SNI) extension, or which uses a matching hostname
690
+ * in a preceeding CONNECT request to create a tunnel, the connection will be
691
+ * sent raw to the upstream hostname, without handling TLS within Mockttp (i.e.
692
+ * with no TLS interception performed).
693
+ *
694
+ * This option is mutually exclusive with `tlsInterceptOnly` and setting both
695
+ * options will throw an error.
696
+ *
697
+ * Each element in this list must be an object with a 'hostname' field for the
698
+ * hostname that should be matched. Wildcards are supported (following the
699
+ * [URLPattern specification](https://developer.mozilla.org/en-US/docs/Web/API/URL_Pattern_API)),
700
+ * eg. `{hostname: '*.example.com'}`.
701
+ *
702
+ * In future more options may be supported
703
+ * here for additional configuration of this behaviour.
704
+ */
705
+ tlsPassthrough?: Array<{ hostname: string }>;
706
+
707
+ /**
708
+ * A limited list of the only hostnames whose TLS should be intercepted.
709
+ *
710
+ * This is the opposite of `tlsPassthrough`. When set, only connections
711
+ * to these hostnames will be intercepted, and all other TLS connections will
712
+ * be passed through without interception.
713
+ *
714
+ * This option is mutually exclusive with `tlsPassthrough` and setting both
715
+ * options will throw an error.
716
+ *
717
+ * Each element in this list must be an object with a 'hostname' field for the
718
+ * hostname that should be matched. Wildcards are supported (following the
719
+ * [URLPattern specification](https://developer.mozilla.org/en-US/docs/Web/API/URL_Pattern_API)),
720
+ * eg. `{hostname: '*.example.com'}`.
721
+ *
722
+ * In future more options may be supported
723
+ * here for additional configuration of this behaviour.
724
+ */
725
+ tlsInterceptOnly?: Array<{ hostname: string }>;
726
+ };
727
+
728
+ export interface MockttpOptions {
729
+ /**
730
+ * Should the server automatically respond to OPTIONS requests with a permissive
731
+ * response?
732
+ *
733
+ * Defaults to true for remote clients (e.g. in the browser), and false otherwise.
734
+ * If this is set to false, browser requests will typically fail unless you
735
+ * stub OPTIONS responses by hand.
736
+ */
737
+ cors?: boolean | cors.CorsOptions;
738
+
739
+ /**
740
+ * Should the server print extra debug information?
741
+ */
742
+ debug?: boolean;
743
+
744
+ /**
745
+ * The HTTPS settings to be used. Optional, only HTTP interception will be
746
+ * enabled if omitted. This should be set to either a { key, cert } object
747
+ * containing the private key and certificate in PEM format, or a { keyPath,
748
+ * certPath } object containing the path to files containing that content.
749
+ */
750
+ https?: MockttpHttpsOptions;
751
+
752
+ /**
753
+ * Should HTTP/2 be enabled? Can be true, false, or 'fallback'. If true,
754
+ * HTTP/2 is used for all clients supporting it. If false, HTTP/2 is never
755
+ * used. If 'fallback' HTTP/2 is used only for clients that do not advertise
756
+ * support for HTTP/1.1, but HTTP/1.1 is used by preference in all other
757
+ * cases.
758
+ *
759
+ * Client HTTP/2 support is only advertised as part of the TLS options.
760
+ * When no HTTPS configuration is provided, 'fallback' is equivalent to
761
+ * false.
762
+ */
763
+ http2?: true | 'fallback' | false;
764
+
765
+ /**
766
+ * By default, requests that match no rules will receive an explanation of the
767
+ * request & existing rules, followed by some suggested example Mockttp code
768
+ * which could be used to match the rule.
769
+ *
770
+ * In some cases where the end client is unaware of Mockttp, these example
771
+ * suggestions are just confusing. Set `suggestChanges` to false to disable it.
772
+ */
773
+ suggestChanges?: boolean;
774
+
775
+ /**
776
+ * Record the requests & response for all traffic matched by each rule, and make
777
+ * it available via endpoint.getSeenRequests().
778
+ *
779
+ * Defaults to true. It can be useful to set this to false if lots of data will
780
+ * be sent to/via the server, to avoid storing all traffic in memory unnecessarily,
781
+ * if getSeenRequests will not be used.
782
+ *
783
+ * If this is set to false then getSeenRequests() will always return
784
+ * an empty array. This only disables the built-in persistence of request data,
785
+ * so traffic can still be captured live or stored elsewhere using
786
+ * .on('request') & .on('response').
787
+ */
788
+ recordTraffic?: boolean;
789
+
790
+ /**
791
+ * The maximum body size to process, in bytes.
792
+ *
793
+ * Bodies larger than this will be dropped, becoming empty, so they won't match
794
+ * body matchers, won't be available in .seenRequests, and won't be included in
795
+ * subscribed event data. Body data will still typically be included in passed
796
+ * through request & response data, in most cases, so this won't affect the
797
+ * external HTTP clients otherwise.
798
+ */
799
+ maxBodySize?: number;
800
+ }
801
+
802
+ export type SubscribableEvent =
803
+ | 'request-initiated'
804
+ | 'request'
805
+ | 'response'
806
+ | 'websocket-request'
807
+ | 'websocket-accepted'
808
+ | 'websocket-message-received'
809
+ | 'websocket-message-sent'
810
+ | 'websocket-close'
811
+ | 'abort'
812
+ | 'tls-passthrough-opened'
813
+ | 'tls-passthrough-closed'
814
+ | 'tls-client-error'
815
+ | 'client-error'
816
+ | 'rule-event';
817
+
818
+ /**
819
+ * @hidden
820
+ */
821
+ export abstract class AbstractMockttp {
822
+ protected corsOptions: boolean | cors.CorsOptions;
823
+ protected debug: boolean;
824
+ protected recordTraffic: boolean;
825
+ protected suggestChanges: boolean;
826
+
827
+ abstract get url(): string;
828
+ abstract on(
829
+ event: SubscribableEvent,
830
+ callback: (req: CompletedRequest) => void
831
+ ): Promise<void>;
832
+
833
+ constructor(options: MockttpOptions) {
834
+ this.debug = options.debug || false;
835
+ this.corsOptions = options.cors || false;
836
+ this.recordTraffic = options.recordTraffic !== undefined
837
+ ? options.recordTraffic
838
+ : true;
839
+ this.suggestChanges = options.suggestChanges !== undefined
840
+ ? options.suggestChanges
841
+ : true;
842
+ }
843
+
844
+ get proxyEnv(): ProxyEnvConfig {
845
+ return {
846
+ HTTP_PROXY: this.url,
847
+ HTTPS_PROXY: this.url
848
+ }
849
+ }
850
+
851
+ urlFor(path: string): string {
852
+ return this.url + path;
853
+ }
854
+
855
+ abstract addRequestRules: (...ruleData: RequestRuleData[]) => Promise<MockedEndpoint[]>;
856
+ addRequestRule = (rule: RequestRuleData) =>
857
+ this.addRequestRules(rule).then((rules) => rules[0]);
858
+
859
+ abstract setRequestRules(...ruleData: RequestRuleData[]): Promise<MockedEndpoint[]>;
860
+
861
+ abstract addWebSocketRules: (...ruleData: WebSocketRuleData[]) => Promise<MockedEndpoint[]>;
862
+ addWebSocketRule = (rule: WebSocketRuleData) =>
863
+ this.addWebSocketRules(rule).then((rules) => rules[0]);
864
+
865
+ abstract setWebSocketRules(...ruleData: WebSocketRuleData[]): Promise<MockedEndpoint[]>;
866
+
867
+ forAnyRequest(): RequestRuleBuilder {
868
+ return new RequestRuleBuilder(this.addRequestRule);
869
+ }
870
+
871
+ forUnmatchedRequest(): RequestRuleBuilder {
872
+ return new RequestRuleBuilder(this.addRequestRule)
873
+ .asPriority(RulePriority.FALLBACK);
874
+ }
875
+
876
+ forGet(url?: string | RegExp): RequestRuleBuilder {
877
+ return new RequestRuleBuilder(Method.GET, url, this.addRequestRule);
878
+ }
879
+
880
+ forPost(url?: string | RegExp): RequestRuleBuilder {
881
+ return new RequestRuleBuilder(Method.POST, url, this.addRequestRule);
882
+ }
883
+
884
+ forPut(url?: string | RegExp): RequestRuleBuilder {
885
+ return new RequestRuleBuilder(Method.PUT, url, this.addRequestRule);
886
+ }
887
+
888
+ forDelete(url?: string | RegExp): RequestRuleBuilder {
889
+ return new RequestRuleBuilder(Method.DELETE, url, this.addRequestRule);
890
+ }
891
+
892
+ forPatch(url?: string | RegExp): RequestRuleBuilder {
893
+ return new RequestRuleBuilder(Method.PATCH, url, this.addRequestRule);
894
+ }
895
+
896
+ forHead(url?: string | RegExp): RequestRuleBuilder {
897
+ return new RequestRuleBuilder(Method.HEAD, url, this.addRequestRule);
898
+ }
899
+
900
+ forOptions(url?: string | RegExp): RequestRuleBuilder {
901
+ if (this.corsOptions) {
902
+ throw new Error(stripIndent`
903
+ Cannot mock OPTIONS requests with CORS enabled.
904
+
905
+ You can disable CORS by passing { cors: false } to getLocal/getRemote, but this may cause issues ${''
906
+ }connecting to your mock server from browsers, unless you mock all required OPTIONS preflight ${''
907
+ }responses by hand.
908
+ `);
909
+ }
910
+ return new RequestRuleBuilder(Method.OPTIONS, url, this.addRequestRule);
911
+ }
912
+
913
+ forJsonRpcRequest(match: { method?: string, params?: any } = {}) {
914
+ return new RequestRuleBuilder(this.addRequestRule)
915
+ .withJsonBodyIncluding({
916
+ jsonrpc: '2.0',
917
+ ...(match.method ? { method: match.method } : {}),
918
+ ...(match.params ? { params: match.params } : {})
919
+ });
920
+ }
921
+
922
+ forAnyWebSocket(): WebSocketRuleBuilder {
923
+ return new WebSocketRuleBuilder(this.addWebSocketRule);
924
+ }
925
+
926
+ }