@jaredwray/mockhttp 0.9.0 → 1.0.1
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/README.md +323 -0
- package/dist/index.d.ts +130 -2
- package/dist/index.js +29 -2772
- package/package.json +11 -11
package/dist/index.js
CHANGED
|
@@ -1,964 +1,4 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
-
|
|
4
|
-
// src/index.ts
|
|
5
|
-
import process from "process";
|
|
6
|
-
|
|
7
|
-
// src/mock-http.ts
|
|
8
|
-
import path from "path";
|
|
9
|
-
import fastifyCookie from "@fastify/cookie";
|
|
10
|
-
import fastifyHelmet from "@fastify/helmet";
|
|
11
|
-
import fastifyStatic from "@fastify/static";
|
|
12
|
-
import { fastifySwagger } from "@fastify/swagger";
|
|
13
|
-
import { detect } from "detect-port";
|
|
14
|
-
import Fastify from "fastify";
|
|
15
|
-
import { Hookified } from "hookified";
|
|
16
|
-
|
|
17
|
-
// src/fastify-config.ts
|
|
18
|
-
var fastifyConfig = {
|
|
19
|
-
logger: {
|
|
20
|
-
transport: {
|
|
21
|
-
target: "pino-pretty",
|
|
22
|
-
options: {
|
|
23
|
-
colorize: true,
|
|
24
|
-
translateTime: true,
|
|
25
|
-
ignore: "pid,hostname",
|
|
26
|
-
singleLine: true
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
// src/routes/anything/index.ts
|
|
33
|
-
var anythingSchema = {
|
|
34
|
-
description: "Will return anything that you send it",
|
|
35
|
-
tags: [
|
|
36
|
-
"Anything"
|
|
37
|
-
],
|
|
38
|
-
response: {
|
|
39
|
-
200: {
|
|
40
|
-
type: "object",
|
|
41
|
-
properties: {
|
|
42
|
-
// Echo back all your params under `args`
|
|
43
|
-
args: {
|
|
44
|
-
type: "object",
|
|
45
|
-
description: "Route parameters",
|
|
46
|
-
additionalProperties: true
|
|
47
|
-
},
|
|
48
|
-
// Echo back whatever was in the request.body
|
|
49
|
-
data: {
|
|
50
|
-
type: "object",
|
|
51
|
-
description: "Request body data",
|
|
52
|
-
additionalProperties: true
|
|
53
|
-
},
|
|
54
|
-
// If you’re using fastify-multipart, you’ll get
|
|
55
|
-
// files via request.files(); this just allows
|
|
56
|
-
// you to mirror them back
|
|
57
|
-
files: {
|
|
58
|
-
type: "object",
|
|
59
|
-
description: "Uploaded files",
|
|
60
|
-
additionalProperties: true
|
|
61
|
-
},
|
|
62
|
-
// If you parse application/x-www-form-urlencoded
|
|
63
|
-
// these fields land in request.body too, but you can
|
|
64
|
-
// mirror them separately if you like
|
|
65
|
-
form: {
|
|
66
|
-
type: "object",
|
|
67
|
-
description: "Form data",
|
|
68
|
-
additionalProperties: true
|
|
69
|
-
},
|
|
70
|
-
headers: {
|
|
71
|
-
type: "object",
|
|
72
|
-
description: "Request headers",
|
|
73
|
-
additionalProperties: true
|
|
74
|
-
},
|
|
75
|
-
json: {
|
|
76
|
-
type: "object",
|
|
77
|
-
description: "Parsed JSON body",
|
|
78
|
-
additionalProperties: true
|
|
79
|
-
},
|
|
80
|
-
method: {
|
|
81
|
-
type: "string",
|
|
82
|
-
description: "HTTP method used"
|
|
83
|
-
},
|
|
84
|
-
origin: {
|
|
85
|
-
type: "string",
|
|
86
|
-
description: "Origin of the request"
|
|
87
|
-
},
|
|
88
|
-
url: {
|
|
89
|
-
type: "string",
|
|
90
|
-
description: "Request URL"
|
|
91
|
-
}
|
|
92
|
-
},
|
|
93
|
-
// Don’t allow any other top-level keys in your response
|
|
94
|
-
additionalProperties: false
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
};
|
|
98
|
-
var anythingRoute = /* @__PURE__ */ __name((fastify) => {
|
|
99
|
-
const handler = /* @__PURE__ */ __name(async (request, reply) => {
|
|
100
|
-
const response = {
|
|
101
|
-
args: request.query,
|
|
102
|
-
data: request.body ?? {},
|
|
103
|
-
// biome-ignore lint/suspicious/noExplicitAny: expected
|
|
104
|
-
files: request.raw?.files ?? {},
|
|
105
|
-
form: request.body ?? {},
|
|
106
|
-
headers: request.headers,
|
|
107
|
-
json: request.body ?? {},
|
|
108
|
-
method: request.method,
|
|
109
|
-
origin: request.headers.origin ?? request.headers.host ?? "",
|
|
110
|
-
url: request.url
|
|
111
|
-
};
|
|
112
|
-
await reply.send(response);
|
|
113
|
-
}, "handler");
|
|
114
|
-
const noValidation = {
|
|
115
|
-
schema: anythingSchema,
|
|
116
|
-
// Skip AJV on *this* route
|
|
117
|
-
validatorCompiler: /* @__PURE__ */ __name(() => () => true, "validatorCompiler")
|
|
118
|
-
};
|
|
119
|
-
fastify.get("/anything", noValidation, handler);
|
|
120
|
-
fastify.post("/anything", noValidation, handler);
|
|
121
|
-
fastify.put("/anything", noValidation, handler);
|
|
122
|
-
fastify.patch("/anything", noValidation, handler);
|
|
123
|
-
fastify.delete("/anything", noValidation, handler);
|
|
124
|
-
}, "anythingRoute");
|
|
125
|
-
|
|
126
|
-
// src/routes/auth/basic.ts
|
|
127
|
-
import { Buffer as Buffer2 } from "buffer";
|
|
128
|
-
import { timingSafeEqual } from "crypto";
|
|
129
|
-
var parseBasic = /* @__PURE__ */ __name((header) => {
|
|
130
|
-
if (!header || typeof header !== "string") {
|
|
131
|
-
return void 0;
|
|
132
|
-
}
|
|
133
|
-
const spaceIndex = header.indexOf(" ");
|
|
134
|
-
if (spaceIndex === -1) {
|
|
135
|
-
return void 0;
|
|
136
|
-
}
|
|
137
|
-
const scheme = header.slice(0, spaceIndex);
|
|
138
|
-
const value = header.slice(spaceIndex + 1);
|
|
139
|
-
if (scheme.toLowerCase() !== "basic" || !value) {
|
|
140
|
-
return void 0;
|
|
141
|
-
}
|
|
142
|
-
try {
|
|
143
|
-
const decoded = Buffer2.from(value, "base64").toString("utf8");
|
|
144
|
-
const idx = decoded.indexOf(":");
|
|
145
|
-
if (idx === -1) {
|
|
146
|
-
return void 0;
|
|
147
|
-
}
|
|
148
|
-
const username = decoded.slice(0, idx);
|
|
149
|
-
const password = decoded.slice(idx + 1);
|
|
150
|
-
if (username === "") {
|
|
151
|
-
return void 0;
|
|
152
|
-
}
|
|
153
|
-
return {
|
|
154
|
-
username,
|
|
155
|
-
password
|
|
156
|
-
};
|
|
157
|
-
} catch {
|
|
158
|
-
return void 0;
|
|
159
|
-
}
|
|
160
|
-
}, "parseBasic");
|
|
161
|
-
var safeCompare = /* @__PURE__ */ __name((a, b) => {
|
|
162
|
-
if (a.length !== b.length) {
|
|
163
|
-
return false;
|
|
164
|
-
}
|
|
165
|
-
try {
|
|
166
|
-
return timingSafeEqual(Buffer2.from(a, "utf8"), Buffer2.from(b, "utf8"));
|
|
167
|
-
} catch {
|
|
168
|
-
return false;
|
|
169
|
-
}
|
|
170
|
-
}, "safeCompare");
|
|
171
|
-
var basicAuthSchema = {
|
|
172
|
-
description: "HTTP Basic authentication. Succeeds only if the user/pass provided in the path matches the Basic Authorization header.",
|
|
173
|
-
tags: [
|
|
174
|
-
"Auth"
|
|
175
|
-
],
|
|
176
|
-
params: {
|
|
177
|
-
type: "object",
|
|
178
|
-
properties: {
|
|
179
|
-
user: {
|
|
180
|
-
type: "string"
|
|
181
|
-
},
|
|
182
|
-
passwd: {
|
|
183
|
-
type: "string"
|
|
184
|
-
}
|
|
185
|
-
},
|
|
186
|
-
required: [
|
|
187
|
-
"user",
|
|
188
|
-
"passwd"
|
|
189
|
-
]
|
|
190
|
-
},
|
|
191
|
-
response: {
|
|
192
|
-
200: {
|
|
193
|
-
type: "object",
|
|
194
|
-
properties: {
|
|
195
|
-
authenticated: {
|
|
196
|
-
type: "boolean"
|
|
197
|
-
},
|
|
198
|
-
user: {
|
|
199
|
-
type: "string"
|
|
200
|
-
}
|
|
201
|
-
},
|
|
202
|
-
required: [
|
|
203
|
-
"authenticated",
|
|
204
|
-
"user"
|
|
205
|
-
]
|
|
206
|
-
},
|
|
207
|
-
401: {
|
|
208
|
-
type: "object",
|
|
209
|
-
properties: {
|
|
210
|
-
message: {
|
|
211
|
-
type: "string"
|
|
212
|
-
}
|
|
213
|
-
},
|
|
214
|
-
required: [
|
|
215
|
-
"message"
|
|
216
|
-
]
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
};
|
|
220
|
-
var basicAuthRoute = /* @__PURE__ */ __name((fastify) => {
|
|
221
|
-
fastify.get("/basic-auth/:user/:passwd", {
|
|
222
|
-
schema: basicAuthSchema
|
|
223
|
-
}, async (request, reply) => {
|
|
224
|
-
const { user, passwd } = request.params;
|
|
225
|
-
if (!user || typeof user !== "string" || typeof passwd !== "string") {
|
|
226
|
-
void reply.header("WWW-Authenticate", 'Basic realm="mockhttp"');
|
|
227
|
-
return reply.status(401).send({
|
|
228
|
-
message: "Unauthorized"
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
const parsed = parseBasic(request.headers.authorization);
|
|
232
|
-
if (!parsed || !safeCompare(parsed.username, user) || !safeCompare(parsed.password, passwd)) {
|
|
233
|
-
void reply.header("WWW-Authenticate", 'Basic realm="mockhttp"');
|
|
234
|
-
return reply.status(401).send({
|
|
235
|
-
message: "Unauthorized"
|
|
236
|
-
});
|
|
237
|
-
}
|
|
238
|
-
return reply.send({
|
|
239
|
-
authenticated: true,
|
|
240
|
-
user
|
|
241
|
-
});
|
|
242
|
-
});
|
|
243
|
-
}, "basicAuthRoute");
|
|
244
|
-
|
|
245
|
-
// src/routes/auth/bearer.ts
|
|
246
|
-
var parseBearer = /* @__PURE__ */ __name((header) => {
|
|
247
|
-
if (!header) {
|
|
248
|
-
return void 0;
|
|
249
|
-
}
|
|
250
|
-
const [scheme, token] = header.split(" ");
|
|
251
|
-
if (!scheme || scheme.toLowerCase() !== "bearer" || !token) {
|
|
252
|
-
return void 0;
|
|
253
|
-
}
|
|
254
|
-
return token;
|
|
255
|
-
}, "parseBearer");
|
|
256
|
-
var bearerAuthSchema = {
|
|
257
|
-
description: "HTTP Bearer authentication. Succeeds if any Bearer token is present unless ?required=true is provided.",
|
|
258
|
-
tags: [
|
|
259
|
-
"Auth"
|
|
260
|
-
],
|
|
261
|
-
querystring: {
|
|
262
|
-
type: "object",
|
|
263
|
-
properties: {
|
|
264
|
-
required: {
|
|
265
|
-
type: "boolean",
|
|
266
|
-
default: true
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
},
|
|
270
|
-
response: {
|
|
271
|
-
200: {
|
|
272
|
-
type: "object",
|
|
273
|
-
properties: {
|
|
274
|
-
authenticated: {
|
|
275
|
-
type: "boolean"
|
|
276
|
-
},
|
|
277
|
-
token: {
|
|
278
|
-
type: "string"
|
|
279
|
-
}
|
|
280
|
-
},
|
|
281
|
-
required: [
|
|
282
|
-
"authenticated",
|
|
283
|
-
"token"
|
|
284
|
-
]
|
|
285
|
-
},
|
|
286
|
-
401: {
|
|
287
|
-
type: "object",
|
|
288
|
-
properties: {
|
|
289
|
-
message: {
|
|
290
|
-
type: "string"
|
|
291
|
-
}
|
|
292
|
-
},
|
|
293
|
-
required: [
|
|
294
|
-
"message"
|
|
295
|
-
]
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
};
|
|
299
|
-
var bearerAuthRoute = /* @__PURE__ */ __name((fastify) => {
|
|
300
|
-
fastify.get("/bearer", {
|
|
301
|
-
schema: bearerAuthSchema
|
|
302
|
-
}, async (request, reply) => {
|
|
303
|
-
const token = parseBearer(request.headers.authorization);
|
|
304
|
-
const qs = request.query;
|
|
305
|
-
const required = qs?.required ?? true;
|
|
306
|
-
if (!token && required) {
|
|
307
|
-
void reply.header("WWW-Authenticate", "Bearer");
|
|
308
|
-
return reply.status(401).send({
|
|
309
|
-
message: "Unauthorized"
|
|
310
|
-
});
|
|
311
|
-
}
|
|
312
|
-
if (!token) {
|
|
313
|
-
return reply.send({
|
|
314
|
-
authenticated: false,
|
|
315
|
-
token: ""
|
|
316
|
-
});
|
|
317
|
-
}
|
|
318
|
-
return reply.send({
|
|
319
|
-
authenticated: true,
|
|
320
|
-
token
|
|
321
|
-
});
|
|
322
|
-
});
|
|
323
|
-
}, "bearerAuthRoute");
|
|
324
|
-
|
|
325
|
-
// src/routes/auth/digest.ts
|
|
326
|
-
import crypto from "crypto";
|
|
327
|
-
var makeNonce = /* @__PURE__ */ __name(() => crypto.randomBytes(16).toString("hex"), "makeNonce");
|
|
328
|
-
var algorithms = /* @__PURE__ */ new Set([
|
|
329
|
-
"MD5",
|
|
330
|
-
"md5",
|
|
331
|
-
"SHA-256",
|
|
332
|
-
"sha-256",
|
|
333
|
-
"SHA-512",
|
|
334
|
-
"sha-512"
|
|
335
|
-
]);
|
|
336
|
-
var hashAlg = /* @__PURE__ */ __name((alg) => {
|
|
337
|
-
if (!alg) {
|
|
338
|
-
return "md5";
|
|
339
|
-
}
|
|
340
|
-
const a = alg.toLowerCase();
|
|
341
|
-
if (a === "md5") {
|
|
342
|
-
return "md5";
|
|
343
|
-
}
|
|
344
|
-
if (a === "sha-256") {
|
|
345
|
-
return "sha256";
|
|
346
|
-
}
|
|
347
|
-
if (a === "sha-512") {
|
|
348
|
-
return "sha512";
|
|
349
|
-
}
|
|
350
|
-
return "md5";
|
|
351
|
-
}, "hashAlg");
|
|
352
|
-
var h = /* @__PURE__ */ __name((algo, data) => crypto.createHash(algo).update(data).digest("hex"), "h");
|
|
353
|
-
var parseDigest = /* @__PURE__ */ __name((header) => {
|
|
354
|
-
if (!header) {
|
|
355
|
-
return void 0;
|
|
356
|
-
}
|
|
357
|
-
const spaceIndex = header.indexOf(" ");
|
|
358
|
-
if (spaceIndex === -1) {
|
|
359
|
-
return void 0;
|
|
360
|
-
}
|
|
361
|
-
const scheme = header.slice(0, spaceIndex);
|
|
362
|
-
const rest = header.slice(spaceIndex + 1);
|
|
363
|
-
if (!scheme || scheme.toLowerCase() !== "digest" || !rest) {
|
|
364
|
-
return void 0;
|
|
365
|
-
}
|
|
366
|
-
const out = {};
|
|
367
|
-
const parts = [];
|
|
368
|
-
let current = "";
|
|
369
|
-
let inQuotes = false;
|
|
370
|
-
for (const char of rest) {
|
|
371
|
-
if (char === '"') {
|
|
372
|
-
inQuotes = !inQuotes;
|
|
373
|
-
current += char;
|
|
374
|
-
} else if (char === "," && !inQuotes) {
|
|
375
|
-
if (current.trim()) {
|
|
376
|
-
parts.push(current.trim());
|
|
377
|
-
}
|
|
378
|
-
current = "";
|
|
379
|
-
} else {
|
|
380
|
-
current += char;
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
if (current.trim()) {
|
|
384
|
-
parts.push(current.trim());
|
|
385
|
-
}
|
|
386
|
-
if (parts.length === 0) {
|
|
387
|
-
return out;
|
|
388
|
-
}
|
|
389
|
-
for (const p of parts) {
|
|
390
|
-
const equalIndex = p.indexOf("=");
|
|
391
|
-
if (equalIndex === -1) {
|
|
392
|
-
continue;
|
|
393
|
-
}
|
|
394
|
-
const k = p.slice(0, equalIndex).trim();
|
|
395
|
-
const v = p.slice(equalIndex + 1).trim();
|
|
396
|
-
let value = v;
|
|
397
|
-
if (value.startsWith('"') && value.endsWith('"')) {
|
|
398
|
-
value = value.slice(1, -1);
|
|
399
|
-
}
|
|
400
|
-
out[k] = value;
|
|
401
|
-
}
|
|
402
|
-
return out;
|
|
403
|
-
}, "parseDigest");
|
|
404
|
-
var digestAuthSchema = {
|
|
405
|
-
description: "HTTP Digest authentication. Mirrors httpbin behavior for testing clients.",
|
|
406
|
-
tags: [
|
|
407
|
-
"Auth"
|
|
408
|
-
],
|
|
409
|
-
params: {
|
|
410
|
-
type: "object",
|
|
411
|
-
properties: {
|
|
412
|
-
qop: {
|
|
413
|
-
type: "string"
|
|
414
|
-
},
|
|
415
|
-
user: {
|
|
416
|
-
type: "string"
|
|
417
|
-
},
|
|
418
|
-
passwd: {
|
|
419
|
-
type: "string"
|
|
420
|
-
},
|
|
421
|
-
algorithm: {
|
|
422
|
-
type: "string"
|
|
423
|
-
},
|
|
424
|
-
stale_after: {
|
|
425
|
-
type: "string"
|
|
426
|
-
}
|
|
427
|
-
},
|
|
428
|
-
required: [
|
|
429
|
-
"qop",
|
|
430
|
-
"user",
|
|
431
|
-
"passwd"
|
|
432
|
-
]
|
|
433
|
-
}
|
|
434
|
-
};
|
|
435
|
-
var digestAuthRoute = /* @__PURE__ */ __name((fastify) => {
|
|
436
|
-
fastify.get("/digest-auth/:qop/:user/:passwd", {
|
|
437
|
-
schema: digestAuthSchema
|
|
438
|
-
}, async (request, reply) => {
|
|
439
|
-
const { user, passwd } = request.params;
|
|
440
|
-
const realm = "mockhttp";
|
|
441
|
-
const nonce = makeNonce();
|
|
442
|
-
const auth = parseDigest(request.headers.authorization);
|
|
443
|
-
if (!auth) {
|
|
444
|
-
void reply.header("WWW-Authenticate", `Digest realm="${realm}", qop="auth,auth-int", nonce="${nonce}", opaque="${makeNonce()}"`);
|
|
445
|
-
return reply.status(401).send({
|
|
446
|
-
message: "Unauthorized"
|
|
447
|
-
});
|
|
448
|
-
}
|
|
449
|
-
if (auth.username !== user) {
|
|
450
|
-
void reply.header("WWW-Authenticate", `Digest realm="${realm}", qop="auth,auth-int", nonce="${nonce}", opaque="${makeNonce()}"`);
|
|
451
|
-
return reply.status(401).send({
|
|
452
|
-
message: "Unauthorized"
|
|
453
|
-
});
|
|
454
|
-
}
|
|
455
|
-
const method = "GET";
|
|
456
|
-
const uri = auth.uri ?? request.url.replace(/^https?:\/\/[^/]+/, "");
|
|
457
|
-
const algo = hashAlg(auth.algorithm);
|
|
458
|
-
const ha1 = h(algo, `${user}:${realm}:${passwd}`);
|
|
459
|
-
const ha2 = h(algo, `${method}:${uri}`);
|
|
460
|
-
let expected = "";
|
|
461
|
-
expected = auth.qop ? h(algo, `${ha1}:${auth.nonce}:${auth.nc}:${auth.cnonce}:${auth.qop}:${ha2}`) : h(algo, `${ha1}:${auth.nonce}:${ha2}`);
|
|
462
|
-
if (!auth.response || auth.response !== expected) {
|
|
463
|
-
void reply.header("WWW-Authenticate", `Digest realm="${realm}", qop="auth,auth-int", nonce="${nonce}", opaque="${makeNonce()}"`);
|
|
464
|
-
return reply.status(401).send({
|
|
465
|
-
message: "Unauthorized"
|
|
466
|
-
});
|
|
467
|
-
}
|
|
468
|
-
return reply.send({
|
|
469
|
-
authenticated: true,
|
|
470
|
-
user
|
|
471
|
-
});
|
|
472
|
-
});
|
|
473
|
-
fastify.get("/digest-auth/:qop/:user/:passwd/:algorithm", {
|
|
474
|
-
schema: digestAuthSchema
|
|
475
|
-
}, async (request, reply) => {
|
|
476
|
-
const { user, passwd, algorithm } = request.params;
|
|
477
|
-
const realm = "mockhttp";
|
|
478
|
-
const algoHeader = algorithms.has(String(algorithm)) ? String(algorithm) : "MD5";
|
|
479
|
-
const nonce = makeNonce();
|
|
480
|
-
const auth = parseDigest(request.headers.authorization);
|
|
481
|
-
if (!auth) {
|
|
482
|
-
void reply.header("WWW-Authenticate", `Digest realm="${realm}", qop="auth,auth-int", algorithm=${algoHeader}, nonce="${nonce}", opaque="${makeNonce()}"`);
|
|
483
|
-
return reply.status(401).send({
|
|
484
|
-
message: "Unauthorized"
|
|
485
|
-
});
|
|
486
|
-
}
|
|
487
|
-
if (auth.username !== user) {
|
|
488
|
-
void reply.header("WWW-Authenticate", `Digest realm="${realm}", qop="auth,auth-int", algorithm=${algoHeader}, nonce="${nonce}", opaque="${makeNonce()}"`);
|
|
489
|
-
return reply.status(401).send({
|
|
490
|
-
message: "Unauthorized"
|
|
491
|
-
});
|
|
492
|
-
}
|
|
493
|
-
const method = "GET";
|
|
494
|
-
const uri = auth.uri ?? request.url.replace(/^https?:\/\/[^/]+/, "");
|
|
495
|
-
const algo = hashAlg(auth.algorithm ?? algoHeader);
|
|
496
|
-
const ha1 = h(algo, `${user}:${realm}:${passwd}`);
|
|
497
|
-
const ha2 = h(algo, `${method}:${uri}`);
|
|
498
|
-
const expected = auth.qop ? h(algo, `${ha1}:${auth.nonce}:${auth.nc}:${auth.cnonce}:${auth.qop}:${ha2}`) : h(algo, `${ha1}:${auth.nonce}:${ha2}`);
|
|
499
|
-
if (!auth.response || auth.response !== expected) {
|
|
500
|
-
void reply.header("WWW-Authenticate", `Digest realm="${realm}", qop="auth,auth-int", algorithm=${algoHeader}, nonce="${nonce}", opaque="${makeNonce()}"`);
|
|
501
|
-
return reply.status(401).send({
|
|
502
|
-
message: "Unauthorized"
|
|
503
|
-
});
|
|
504
|
-
}
|
|
505
|
-
return reply.send({
|
|
506
|
-
authenticated: true,
|
|
507
|
-
user
|
|
508
|
-
});
|
|
509
|
-
});
|
|
510
|
-
}, "digestAuthRoute");
|
|
511
|
-
|
|
512
|
-
// src/routes/auth/hidden-basic.ts
|
|
513
|
-
import { Buffer as Buffer3 } from "buffer";
|
|
514
|
-
var parseBasic2 = /* @__PURE__ */ __name((header) => {
|
|
515
|
-
if (!header) {
|
|
516
|
-
return void 0;
|
|
517
|
-
}
|
|
518
|
-
const [scheme, value] = header.split(" ");
|
|
519
|
-
if (!scheme || scheme.toLowerCase() !== "basic" || !value) {
|
|
520
|
-
return void 0;
|
|
521
|
-
}
|
|
522
|
-
try {
|
|
523
|
-
const decoded = Buffer3.from(value, "base64").toString("utf8");
|
|
524
|
-
const idx = decoded.indexOf(":");
|
|
525
|
-
if (idx === -1) {
|
|
526
|
-
return void 0;
|
|
527
|
-
}
|
|
528
|
-
return {
|
|
529
|
-
username: decoded.slice(0, idx),
|
|
530
|
-
password: decoded.slice(idx + 1)
|
|
531
|
-
};
|
|
532
|
-
} catch {
|
|
533
|
-
return void 0;
|
|
534
|
-
}
|
|
535
|
-
}, "parseBasic");
|
|
536
|
-
var hiddenBasicAuthSchema = {
|
|
537
|
-
description: "HTTP Basic authentication with docs hidden. Mirrors /basic-auth but hides from schema.",
|
|
538
|
-
tags: [
|
|
539
|
-
"Auth"
|
|
540
|
-
],
|
|
541
|
-
hide: true,
|
|
542
|
-
params: {
|
|
543
|
-
type: "object",
|
|
544
|
-
properties: {
|
|
545
|
-
user: {
|
|
546
|
-
type: "string"
|
|
547
|
-
},
|
|
548
|
-
passwd: {
|
|
549
|
-
type: "string"
|
|
550
|
-
}
|
|
551
|
-
},
|
|
552
|
-
required: [
|
|
553
|
-
"user",
|
|
554
|
-
"passwd"
|
|
555
|
-
]
|
|
556
|
-
}
|
|
557
|
-
};
|
|
558
|
-
var hiddenBasicAuthRoute = /* @__PURE__ */ __name((fastify) => {
|
|
559
|
-
fastify.get("/hidden-basic-auth/:user/:passwd", {
|
|
560
|
-
schema: hiddenBasicAuthSchema
|
|
561
|
-
}, async (request, reply) => {
|
|
562
|
-
const { user, passwd } = request.params;
|
|
563
|
-
const parsed = parseBasic2(request.headers.authorization);
|
|
564
|
-
if (!parsed || parsed.username !== user || parsed.password !== passwd) {
|
|
565
|
-
void reply.header("WWW-Authenticate", 'Basic realm="mockhttp"');
|
|
566
|
-
return reply.status(401).send({
|
|
567
|
-
message: "Unauthorized"
|
|
568
|
-
});
|
|
569
|
-
}
|
|
570
|
-
return reply.send({
|
|
571
|
-
authenticated: true,
|
|
572
|
-
user
|
|
573
|
-
});
|
|
574
|
-
});
|
|
575
|
-
}, "hiddenBasicAuthRoute");
|
|
576
|
-
|
|
577
|
-
// src/routes/cookies/delete.ts
|
|
578
|
-
var cookiesDeleteSchema = {
|
|
579
|
-
description: "Delete a cookie",
|
|
580
|
-
tags: [
|
|
581
|
-
"Cookies"
|
|
582
|
-
],
|
|
583
|
-
querystring: {
|
|
584
|
-
type: "object",
|
|
585
|
-
properties: {
|
|
586
|
-
name: {
|
|
587
|
-
type: "string"
|
|
588
|
-
}
|
|
589
|
-
},
|
|
590
|
-
required: [
|
|
591
|
-
"name"
|
|
592
|
-
],
|
|
593
|
-
additionalProperties: false
|
|
594
|
-
},
|
|
595
|
-
response: {
|
|
596
|
-
204: {
|
|
597
|
-
type: "null"
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
};
|
|
601
|
-
var deleteCookieRoute = /* @__PURE__ */ __name((fastify) => {
|
|
602
|
-
fastify.delete("/cookies", {
|
|
603
|
-
schema: cookiesDeleteSchema
|
|
604
|
-
}, async (request, reply) => {
|
|
605
|
-
const { name } = request.query;
|
|
606
|
-
reply.clearCookie(name, {
|
|
607
|
-
path: "/"
|
|
608
|
-
});
|
|
609
|
-
return reply.status(204).send();
|
|
610
|
-
});
|
|
611
|
-
}, "deleteCookieRoute");
|
|
612
|
-
|
|
613
|
-
// src/routes/cookies/get.ts
|
|
614
|
-
var getCookiesRouteSchema = {
|
|
615
|
-
description: "Return cookies from the request",
|
|
616
|
-
tags: [
|
|
617
|
-
"Cookies"
|
|
618
|
-
],
|
|
619
|
-
response: {
|
|
620
|
-
200: {
|
|
621
|
-
type: "object",
|
|
622
|
-
properties: {
|
|
623
|
-
cookies: {
|
|
624
|
-
type: "object",
|
|
625
|
-
additionalProperties: {
|
|
626
|
-
type: "string"
|
|
627
|
-
}
|
|
628
|
-
}
|
|
629
|
-
},
|
|
630
|
-
required: [
|
|
631
|
-
"cookies"
|
|
632
|
-
]
|
|
633
|
-
}
|
|
634
|
-
}
|
|
635
|
-
};
|
|
636
|
-
var getCookiesRoute = /* @__PURE__ */ __name((fastify) => {
|
|
637
|
-
fastify.get("/cookies", {
|
|
638
|
-
schema: getCookiesRouteSchema
|
|
639
|
-
}, async (request, reply) => {
|
|
640
|
-
reply.type("application/json");
|
|
641
|
-
await reply.send({
|
|
642
|
-
cookies: request.cookies
|
|
643
|
-
});
|
|
644
|
-
});
|
|
645
|
-
}, "getCookiesRoute");
|
|
646
|
-
|
|
647
|
-
// src/routes/cookies/post.ts
|
|
648
|
-
var postCookieRouteSchema = {
|
|
649
|
-
description: "Set a cookie",
|
|
650
|
-
tags: [
|
|
651
|
-
"Cookies"
|
|
652
|
-
],
|
|
653
|
-
body: {
|
|
654
|
-
type: "object",
|
|
655
|
-
properties: {
|
|
656
|
-
name: {
|
|
657
|
-
type: "string"
|
|
658
|
-
},
|
|
659
|
-
value: {
|
|
660
|
-
type: "string"
|
|
661
|
-
},
|
|
662
|
-
expires: {
|
|
663
|
-
type: "string",
|
|
664
|
-
format: "date-time"
|
|
665
|
-
}
|
|
666
|
-
},
|
|
667
|
-
required: [
|
|
668
|
-
"name",
|
|
669
|
-
"value"
|
|
670
|
-
],
|
|
671
|
-
additionalProperties: false
|
|
672
|
-
},
|
|
673
|
-
response: {
|
|
674
|
-
200: {
|
|
675
|
-
type: "object",
|
|
676
|
-
properties: {
|
|
677
|
-
cookies: {
|
|
678
|
-
type: "object",
|
|
679
|
-
additionalProperties: {
|
|
680
|
-
type: "string"
|
|
681
|
-
}
|
|
682
|
-
}
|
|
683
|
-
},
|
|
684
|
-
required: [
|
|
685
|
-
"cookies"
|
|
686
|
-
]
|
|
687
|
-
}
|
|
688
|
-
}
|
|
689
|
-
};
|
|
690
|
-
var postCookieRoute = /* @__PURE__ */ __name((fastify) => {
|
|
691
|
-
fastify.post("/cookies", {
|
|
692
|
-
schema: postCookieRouteSchema
|
|
693
|
-
}, async (request, reply) => {
|
|
694
|
-
const { name, value, expires } = request.body;
|
|
695
|
-
if (expires) {
|
|
696
|
-
const date = new Date(expires);
|
|
697
|
-
reply.setCookie(name, value, {
|
|
698
|
-
path: "/",
|
|
699
|
-
expires: date
|
|
700
|
-
});
|
|
701
|
-
} else {
|
|
702
|
-
reply.setCookie(name, value, {
|
|
703
|
-
path: "/"
|
|
704
|
-
});
|
|
705
|
-
}
|
|
706
|
-
return reply.status(200).send();
|
|
707
|
-
});
|
|
708
|
-
}, "postCookieRoute");
|
|
709
|
-
|
|
710
|
-
// src/routes/http-methods/delete.ts
|
|
711
|
-
var deleteSchema = {
|
|
712
|
-
description: "Handles a DELETE request and returns request information",
|
|
713
|
-
tags: [
|
|
714
|
-
"HTTP Methods"
|
|
715
|
-
],
|
|
716
|
-
body: {
|
|
717
|
-
type: "object",
|
|
718
|
-
additionalProperties: true,
|
|
719
|
-
description: "The body of the DELETE request"
|
|
720
|
-
},
|
|
721
|
-
response: {
|
|
722
|
-
200: {
|
|
723
|
-
type: "object",
|
|
724
|
-
properties: {
|
|
725
|
-
method: {
|
|
726
|
-
type: "string"
|
|
727
|
-
},
|
|
728
|
-
headers: {
|
|
729
|
-
type: "object",
|
|
730
|
-
additionalProperties: {
|
|
731
|
-
type: "string"
|
|
732
|
-
}
|
|
733
|
-
},
|
|
734
|
-
body: {
|
|
735
|
-
type: "object",
|
|
736
|
-
additionalProperties: true
|
|
737
|
-
}
|
|
738
|
-
},
|
|
739
|
-
required: [
|
|
740
|
-
"method",
|
|
741
|
-
"headers",
|
|
742
|
-
"body"
|
|
743
|
-
]
|
|
744
|
-
}
|
|
745
|
-
}
|
|
746
|
-
};
|
|
747
|
-
var deleteRoute = /* @__PURE__ */ __name((fastify) => {
|
|
748
|
-
fastify.delete("/delete", {
|
|
749
|
-
schema: deleteSchema
|
|
750
|
-
}, async (request) => ({
|
|
751
|
-
method: "DELETE",
|
|
752
|
-
headers: request.headers,
|
|
753
|
-
body: request.body
|
|
754
|
-
}));
|
|
755
|
-
}, "deleteRoute");
|
|
756
|
-
|
|
757
|
-
// src/routes/http-methods/get.ts
|
|
758
|
-
var getSchema = {
|
|
759
|
-
description: "Returns request information for GET requests",
|
|
760
|
-
tags: [
|
|
761
|
-
"HTTP Methods"
|
|
762
|
-
],
|
|
763
|
-
querystring: {
|
|
764
|
-
type: "object",
|
|
765
|
-
additionalProperties: true,
|
|
766
|
-
properties: {},
|
|
767
|
-
description: "Query parameters sent in the request"
|
|
768
|
-
},
|
|
769
|
-
headers: {
|
|
770
|
-
type: "object",
|
|
771
|
-
additionalProperties: true,
|
|
772
|
-
properties: {},
|
|
773
|
-
description: "Headers sent in the request"
|
|
774
|
-
},
|
|
775
|
-
response: {
|
|
776
|
-
200: {
|
|
777
|
-
type: "object",
|
|
778
|
-
properties: {
|
|
779
|
-
method: {
|
|
780
|
-
type: "string"
|
|
781
|
-
},
|
|
782
|
-
headers: {
|
|
783
|
-
type: "object",
|
|
784
|
-
additionalProperties: {
|
|
785
|
-
type: "string"
|
|
786
|
-
}
|
|
787
|
-
},
|
|
788
|
-
queryParams: {
|
|
789
|
-
type: "object",
|
|
790
|
-
additionalProperties: {
|
|
791
|
-
type: "string"
|
|
792
|
-
}
|
|
793
|
-
}
|
|
794
|
-
},
|
|
795
|
-
required: [
|
|
796
|
-
"method",
|
|
797
|
-
"headers",
|
|
798
|
-
"queryParams"
|
|
799
|
-
]
|
|
800
|
-
}
|
|
801
|
-
}
|
|
802
|
-
};
|
|
803
|
-
var getRoute = /* @__PURE__ */ __name((fastify) => {
|
|
804
|
-
fastify.get("/get", {
|
|
805
|
-
schema: getSchema
|
|
806
|
-
}, async (request, _reply) => ({
|
|
807
|
-
method: "GET",
|
|
808
|
-
headers: request.headers,
|
|
809
|
-
queryParams: request.query
|
|
810
|
-
}));
|
|
811
|
-
}, "getRoute");
|
|
812
|
-
|
|
813
|
-
// src/routes/http-methods/patch.ts
|
|
814
|
-
var patchSchema = {
|
|
815
|
-
description: "Handles a PATCH request and returns request information",
|
|
816
|
-
tags: [
|
|
817
|
-
"HTTP Methods"
|
|
818
|
-
],
|
|
819
|
-
body: {
|
|
820
|
-
type: "object",
|
|
821
|
-
additionalProperties: true,
|
|
822
|
-
description: "The body of the PATCH request"
|
|
823
|
-
},
|
|
824
|
-
response: {
|
|
825
|
-
200: {
|
|
826
|
-
type: "object",
|
|
827
|
-
properties: {
|
|
828
|
-
method: {
|
|
829
|
-
type: "string"
|
|
830
|
-
},
|
|
831
|
-
headers: {
|
|
832
|
-
type: "object",
|
|
833
|
-
additionalProperties: {
|
|
834
|
-
type: "string"
|
|
835
|
-
}
|
|
836
|
-
},
|
|
837
|
-
body: {
|
|
838
|
-
type: "object",
|
|
839
|
-
additionalProperties: true
|
|
840
|
-
}
|
|
841
|
-
},
|
|
842
|
-
required: [
|
|
843
|
-
"method",
|
|
844
|
-
"headers",
|
|
845
|
-
"body"
|
|
846
|
-
]
|
|
847
|
-
}
|
|
848
|
-
}
|
|
849
|
-
};
|
|
850
|
-
var patchRoute = /* @__PURE__ */ __name((fastify) => {
|
|
851
|
-
fastify.patch("/patch", {
|
|
852
|
-
schema: patchSchema
|
|
853
|
-
}, async (request) => ({
|
|
854
|
-
method: "PATCH",
|
|
855
|
-
headers: request.headers,
|
|
856
|
-
body: request.body
|
|
857
|
-
}));
|
|
858
|
-
}, "patchRoute");
|
|
859
|
-
|
|
860
|
-
// src/routes/http-methods/post.ts
|
|
861
|
-
var postSchema = {
|
|
862
|
-
description: "Handles a POST request and returns request information",
|
|
863
|
-
tags: [
|
|
864
|
-
"HTTP Methods"
|
|
865
|
-
],
|
|
866
|
-
body: {
|
|
867
|
-
type: "object",
|
|
868
|
-
additionalProperties: true,
|
|
869
|
-
description: "The body of the POST request"
|
|
870
|
-
},
|
|
871
|
-
response: {
|
|
872
|
-
200: {
|
|
873
|
-
type: "object",
|
|
874
|
-
properties: {
|
|
875
|
-
method: {
|
|
876
|
-
type: "string"
|
|
877
|
-
},
|
|
878
|
-
headers: {
|
|
879
|
-
type: "object",
|
|
880
|
-
additionalProperties: {
|
|
881
|
-
type: "string"
|
|
882
|
-
}
|
|
883
|
-
},
|
|
884
|
-
body: {
|
|
885
|
-
type: "object",
|
|
886
|
-
additionalProperties: true
|
|
887
|
-
}
|
|
888
|
-
},
|
|
889
|
-
required: [
|
|
890
|
-
"method",
|
|
891
|
-
"headers",
|
|
892
|
-
"body"
|
|
893
|
-
]
|
|
894
|
-
}
|
|
895
|
-
}
|
|
896
|
-
};
|
|
897
|
-
var postRoute = /* @__PURE__ */ __name((fastify) => {
|
|
898
|
-
fastify.post("/post", {
|
|
899
|
-
schema: postSchema
|
|
900
|
-
}, async (request) => ({
|
|
901
|
-
method: "POST",
|
|
902
|
-
headers: request.headers,
|
|
903
|
-
body: request.body
|
|
904
|
-
}));
|
|
905
|
-
}, "postRoute");
|
|
906
|
-
|
|
907
|
-
// src/routes/http-methods/put.ts
|
|
908
|
-
var putSchema = {
|
|
909
|
-
description: "Handles a PUT request and returns request information",
|
|
910
|
-
tags: [
|
|
911
|
-
"HTTP Methods"
|
|
912
|
-
],
|
|
913
|
-
body: {
|
|
914
|
-
type: "object",
|
|
915
|
-
additionalProperties: true,
|
|
916
|
-
description: "The body of the PUT request"
|
|
917
|
-
},
|
|
918
|
-
response: {
|
|
919
|
-
200: {
|
|
920
|
-
type: "object",
|
|
921
|
-
properties: {
|
|
922
|
-
method: {
|
|
923
|
-
type: "string"
|
|
924
|
-
},
|
|
925
|
-
headers: {
|
|
926
|
-
type: "object",
|
|
927
|
-
additionalProperties: {
|
|
928
|
-
type: "string"
|
|
929
|
-
}
|
|
930
|
-
},
|
|
931
|
-
body: {
|
|
932
|
-
type: "object",
|
|
933
|
-
additionalProperties: true
|
|
934
|
-
}
|
|
935
|
-
},
|
|
936
|
-
required: [
|
|
937
|
-
"method",
|
|
938
|
-
"headers",
|
|
939
|
-
"body"
|
|
940
|
-
]
|
|
941
|
-
}
|
|
942
|
-
}
|
|
943
|
-
};
|
|
944
|
-
var putRoute = /* @__PURE__ */ __name((fastify) => {
|
|
945
|
-
fastify.put("/put", {
|
|
946
|
-
schema: putSchema
|
|
947
|
-
}, async (request) => ({
|
|
948
|
-
method: "PUT",
|
|
949
|
-
headers: request.headers,
|
|
950
|
-
body: request.body
|
|
951
|
-
}));
|
|
952
|
-
}, "putRoute");
|
|
953
|
-
|
|
954
|
-
// src/routes/index.ts
|
|
955
|
-
var indexRoute = /* @__PURE__ */ __name((fastify) => {
|
|
956
|
-
fastify.get("/", {
|
|
957
|
-
schema: {
|
|
958
|
-
hide: true
|
|
959
|
-
}
|
|
960
|
-
}, async (_request, reply) => {
|
|
961
|
-
const redocHtml = `
|
|
1
|
+
var $e=Object.defineProperty;var r=(s,e)=>$e(s,"name",{value:e,configurable:!0});import I from"process";import qe from"path";import pt from"@fastify/cookie";import ht from"@fastify/helmet";import _e from"@fastify/static";import{fastifySwagger as mt}from"@fastify/swagger";import{detect as ut}from"detect-port";import He from"fastify";import{Hookified as lt}from"hookified";var ce={logger:{transport:{target:"pino-pretty",options:{colorize:!0,translateTime:!0,ignore:"pid,hostname",singleLine:!0}}}};var De={description:"Will return anything that you send it",tags:["Anything"],response:{200:{type:"object",properties:{args:{type:"object",description:"Route parameters",additionalProperties:!0},data:{type:"object",description:"Request body data",additionalProperties:!0},files:{type:"object",description:"Uploaded files",additionalProperties:!0},form:{type:"object",description:"Form data",additionalProperties:!0},headers:{type:"object",description:"Request headers",additionalProperties:!0},json:{type:"object",description:"Parsed JSON body",additionalProperties:!0},method:{type:"string",description:"HTTP method used"},origin:{type:"string",description:"Origin of the request"},url:{type:"string",description:"Request URL"}},additionalProperties:!1}}},de=r(s=>{let e=r(async(o,a)=>{let i={args:o.query,data:o.body??{},files:o.raw?.files??{},form:o.body??{},headers:o.headers,json:o.body??{},method:o.method,origin:o.headers.origin??o.headers.host??"",url:o.url};await a.send(i)},"handler"),t={schema:De,validatorCompiler:r(()=>()=>!0,"validatorCompiler")};s.get("/anything",t,e),s.post("/anything",t,e),s.put("/anything",t,e),s.patch("/anything",t,e),s.delete("/anything",t,e)},"anythingRoute");import{Buffer as U}from"buffer";import{timingSafeEqual as Me}from"crypto";var Ae=r(s=>{if(!s||typeof s!="string")return;let e=s.indexOf(" ");if(e===-1)return;let t=s.slice(0,e),o=s.slice(e+1);if(!(t.toLowerCase()!=="basic"||!o))try{let a=U.from(o,"base64").toString("utf8"),i=a.indexOf(":");if(i===-1)return;let n=a.slice(0,i),d=a.slice(i+1);return n===""?void 0:{username:n,password:d}}catch{return}},"parseBasic"),pe=r((s,e)=>{if(s.length!==e.length)return!1;try{return Me(U.from(s,"utf8"),U.from(e,"utf8"))}catch{return!1}},"safeCompare"),Ie={description:"HTTP Basic authentication. Succeeds only if the user/pass provided in the path matches the Basic Authorization header.",tags:["Auth"],params:{type:"object",properties:{user:{type:"string"},passwd:{type:"string"}},required:["user","passwd"]},response:{200:{type:"object",properties:{authenticated:{type:"boolean"},user:{type:"string"}},required:["authenticated","user"]},401:{type:"object",properties:{message:{type:"string"}},required:["message"]}}},E=r(s=>{s.get("/basic-auth/:user/:passwd",{schema:Ie},async(e,t)=>{let{user:o,passwd:a}=e.params;if(!o||typeof o!="string"||typeof a!="string")return t.header("WWW-Authenticate",'Basic realm="mockhttp"'),t.status(401).send({message:"Unauthorized"});let i=Ae(e.headers.authorization);return!i||!pe(i.username,o)||!pe(i.password,a)?(t.header("WWW-Authenticate",'Basic realm="mockhttp"'),t.status(401).send({message:"Unauthorized"})):t.send({authenticated:!0,user:o})})},"basicAuthRoute");var ze=r(s=>{if(!s)return;let[e,t]=s.split(" ");if(!(!e||e.toLowerCase()!=="bearer"||!t))return t},"parseBearer"),Le={description:"HTTP Bearer authentication. Succeeds if any Bearer token is present unless ?required=true is provided.",tags:["Auth"],querystring:{type:"object",properties:{required:{type:"boolean",default:!0}}},response:{200:{type:"object",properties:{authenticated:{type:"boolean"},token:{type:"string"}},required:["authenticated","token"]},401:{type:"object",properties:{message:{type:"string"}},required:["message"]}}},O=r(s=>{s.get("/bearer",{schema:Le},async(e,t)=>{let o=ze(e.headers.authorization),i=e.query?.required??!0;return!o&&i?(t.header("WWW-Authenticate","Bearer"),t.status(401).send({message:"Unauthorized"})):o?t.send({authenticated:!0,token:o}):t.send({authenticated:!1,token:""})})},"bearerAuthRoute");import le from"crypto";var w=r(()=>le.randomBytes(16).toString("hex"),"makeNonce"),Ue=new Set(["MD5","md5","SHA-256","sha-256","SHA-512","sha-512"]),he=r(s=>{if(!s)return"md5";let e=s.toLowerCase();return e==="md5"?"md5":e==="sha-256"?"sha256":e==="sha-512"?"sha512":"md5"},"hashAlg"),x=r((s,e)=>le.createHash(s).update(e).digest("hex"),"h"),me=r(s=>{if(!s)return;let e=s.indexOf(" ");if(e===-1)return;let t=s.slice(0,e),o=s.slice(e+1);if(!t||t.toLowerCase()!=="digest"||!o)return;let a={},i=[],n="",d=!1;for(let g of o)g==='"'?(d=!d,n+=g):g===","&&!d?(n.trim()&&i.push(n.trim()),n=""):n+=g;if(n.trim()&&i.push(n.trim()),i.length===0)return a;for(let g of i){let h=g.indexOf("=");if(h===-1)continue;let l=g.slice(0,h).trim(),p=g.slice(h+1).trim();p.startsWith('"')&&p.endsWith('"')&&(p=p.slice(1,-1)),a[l]=p}return a},"parseDigest"),ue={description:"HTTP Digest authentication. Mirrors httpbin behavior for testing clients.",tags:["Auth"],params:{type:"object",properties:{qop:{type:"string"},user:{type:"string"},passwd:{type:"string"},algorithm:{type:"string"},stale_after:{type:"string"}},required:["qop","user","passwd"]}},B=r(s=>{s.get("/digest-auth/:qop/:user/:passwd",{schema:ue},async(e,t)=>{let{user:o,passwd:a}=e.params,i="mockhttp",n=w(),d=me(e.headers.authorization);if(!d)return t.header("WWW-Authenticate",`Digest realm="${i}", qop="auth,auth-int", nonce="${n}", opaque="${w()}"`),t.status(401).send({message:"Unauthorized"});if(d.username!==o)return t.header("WWW-Authenticate",`Digest realm="${i}", qop="auth,auth-int", nonce="${n}", opaque="${w()}"`),t.status(401).send({message:"Unauthorized"});let g="GET",h=d.uri??e.url.replace(/^https?:\/\/[^/]+/,""),l=he(d.algorithm),c=x(l,`${o}:${i}:${a}`),p=x(l,`${g}:${h}`),u="";return u=d.qop?x(l,`${c}:${d.nonce}:${d.nc}:${d.cnonce}:${d.qop}:${p}`):x(l,`${c}:${d.nonce}:${p}`),!d.response||d.response!==u?(t.header("WWW-Authenticate",`Digest realm="${i}", qop="auth,auth-int", nonce="${n}", opaque="${w()}"`),t.status(401).send({message:"Unauthorized"})):t.send({authenticated:!0,user:o})}),s.get("/digest-auth/:qop/:user/:passwd/:algorithm",{schema:ue},async(e,t)=>{let{user:o,passwd:a,algorithm:i}=e.params,n="mockhttp",d=Ue.has(String(i))?String(i):"MD5",g=w(),h=me(e.headers.authorization);if(!h)return t.header("WWW-Authenticate",`Digest realm="${n}", qop="auth,auth-int", algorithm=${d}, nonce="${g}", opaque="${w()}"`),t.status(401).send({message:"Unauthorized"});if(h.username!==o)return t.header("WWW-Authenticate",`Digest realm="${n}", qop="auth,auth-int", algorithm=${d}, nonce="${g}", opaque="${w()}"`),t.status(401).send({message:"Unauthorized"});let l="GET",c=h.uri??e.url.replace(/^https?:\/\/[^/]+/,""),p=he(h.algorithm??d),u=x(p,`${o}:${n}:${a}`),m=x(p,`${l}:${c}`),y=h.qop?x(p,`${u}:${h.nonce}:${h.nc}:${h.cnonce}:${h.qop}:${m}`):x(p,`${u}:${h.nonce}:${m}`);return!h.response||h.response!==y?(t.header("WWW-Authenticate",`Digest realm="${n}", qop="auth,auth-int", algorithm=${d}, nonce="${g}", opaque="${w()}"`),t.status(401).send({message:"Unauthorized"})):t.send({authenticated:!0,user:o})})},"digestAuthRoute");import{Buffer as Ee}from"buffer";var Oe=r(s=>{if(!s)return;let[e,t]=s.split(" ");if(!(!e||e.toLowerCase()!=="basic"||!t))try{let o=Ee.from(t,"base64").toString("utf8"),a=o.indexOf(":");return a===-1?void 0:{username:o.slice(0,a),password:o.slice(a+1)}}catch{return}},"parseBasic"),Be={description:"HTTP Basic authentication with docs hidden. Mirrors /basic-auth but hides from schema.",tags:["Auth"],hide:!0,params:{type:"object",properties:{user:{type:"string"},passwd:{type:"string"}},required:["user","passwd"]}},F=r(s=>{s.get("/hidden-basic-auth/:user/:passwd",{schema:Be},async(e,t)=>{let{user:o,passwd:a}=e.params,i=Oe(e.headers.authorization);return!i||i.username!==o||i.password!==a?(t.header("WWW-Authenticate",'Basic realm="mockhttp"'),t.status(401).send({message:"Unauthorized"})):t.send({authenticated:!0,user:o})})},"hiddenBasicAuthRoute");var Fe={description:"Delete a cookie",tags:["Cookies"],querystring:{type:"object",properties:{name:{type:"string"}},required:["name"],additionalProperties:!1},response:{204:{type:"null"}}},W=r(s=>{s.delete("/cookies",{schema:Fe},async(e,t)=>{let{name:o}=e.query;return t.clearCookie(o,{path:"/"}),t.status(204).send()})},"deleteCookieRoute");var We={description:"Return cookies from the request",tags:["Cookies"],response:{200:{type:"object",properties:{cookies:{type:"object",additionalProperties:{type:"string"}}},required:["cookies"]}}},N=r(s=>{s.get("/cookies",{schema:We},async(e,t)=>{t.type("application/json"),await t.send({cookies:e.cookies})})},"getCookiesRoute");var Ne={description:"Set a cookie",tags:["Cookies"],body:{type:"object",properties:{name:{type:"string"},value:{type:"string"},expires:{type:"string",format:"date-time"}},required:["name","value"],additionalProperties:!1},response:{200:{type:"object",properties:{cookies:{type:"object",additionalProperties:{type:"string"}}},required:["cookies"]}}},G=r(s=>{s.post("/cookies",{schema:Ne},async(e,t)=>{let{name:o,value:a,expires:i}=e.body;if(i){let n=new Date(i);t.setCookie(o,a,{path:"/",expires:n})}else t.setCookie(o,a,{path:"/"});return t.status(200).send()})},"postCookieRoute");var Ge={description:"Handles a DELETE request and returns request information",tags:["HTTP Methods"],response:{200:{type:"object",properties:{method:{type:"string"},headers:{type:"object",additionalProperties:{type:"string"}},body:{oneOf:[{type:"object",additionalProperties:!0},{type:"string"},{type:"null"}]}},required:["method","headers"]}}},J=r(s=>{s.delete("/delete",{schema:Ge},async e=>({method:"DELETE",headers:e.headers,body:e.body}))},"deleteRoute");var Je={description:"Returns request information for GET requests",tags:["HTTP Methods"],querystring:{type:"object",additionalProperties:!0,properties:{},description:"Query parameters sent in the request"},headers:{type:"object",additionalProperties:!0,properties:{},description:"Headers sent in the request"},response:{200:{type:"object",properties:{method:{type:"string"},headers:{type:"object",additionalProperties:{type:"string"}},queryParams:{type:"object",additionalProperties:{type:"string"}}},required:["method","headers","queryParams"]}}},Y=r(s=>{s.get("/get",{schema:Je},async(e,t)=>({method:"GET",headers:e.headers,queryParams:e.query}))},"getRoute");var Ye={description:"Handles a PATCH request and returns request information",tags:["HTTP Methods"],body:{type:"object",additionalProperties:!0,description:"The body of the PATCH request"},response:{200:{type:"object",properties:{method:{type:"string"},headers:{type:"object",additionalProperties:{type:"string"}},body:{type:"object",additionalProperties:!0}},required:["method","headers","body"]}}},Z=r(s=>{s.patch("/patch",{schema:Ye},async e=>({method:"PATCH",headers:e.headers,body:e.body}))},"patchRoute");var Ze={description:"Handles a POST request and returns request information",tags:["HTTP Methods"],body:{type:"object",additionalProperties:!0,description:"The body of the POST request"},response:{200:{type:"object",properties:{method:{type:"string"},headers:{type:"object",additionalProperties:{type:"string"}},body:{type:"object",additionalProperties:!0}},required:["method","headers","body"]}}},V=r(s=>{s.post("/post",{schema:Ze},async e=>({method:"POST",headers:e.headers,body:e.body}))},"postRoute");var Ve={description:"Handles a PUT request and returns request information",tags:["HTTP Methods"],body:{type:"object",additionalProperties:!0,description:"The body of the PUT request"},response:{200:{type:"object",properties:{method:{type:"string"},headers:{type:"object",additionalProperties:{type:"string"}},body:{type:"object",additionalProperties:!0}},required:["method","headers","body"]}}},X=r(s=>{s.put("/put",{schema:Ve},async e=>({method:"PUT",headers:e.headers,body:e.body}))},"putRoute");var ge=r(s=>{s.get("/",{schema:{hide:!0}},async(e,t)=>{await t.type("text/html").send(`
|
|
962
2
|
<!doctype html>
|
|
963
3
|
<html>
|
|
964
4
|
<head>
|
|
@@ -1022,405 +62,23 @@ var indexRoute = /* @__PURE__ */ __name((fastify) => {
|
|
|
1022
62
|
<script src="/scalar/browser/standalone.js"></script>
|
|
1023
63
|
</body>
|
|
1024
64
|
</html>
|
|
1025
|
-
|
|
1026
|
-
await reply.type("text/html").send(redocHtml);
|
|
1027
|
-
});
|
|
1028
|
-
}, "indexRoute");
|
|
1029
|
-
|
|
1030
|
-
// src/routes/redirects/absolute-redirect.ts
|
|
1031
|
-
var absoluteRedirectSchema = {
|
|
1032
|
-
tags: [
|
|
1033
|
-
"Redirects"
|
|
1034
|
-
],
|
|
1035
|
-
description: "Redirects the request to the target URL using an absolute URL",
|
|
1036
|
-
params: {
|
|
1037
|
-
type: "object",
|
|
1038
|
-
properties: {
|
|
1039
|
-
value: {
|
|
1040
|
-
type: "integer",
|
|
1041
|
-
minimum: 1
|
|
1042
|
-
}
|
|
1043
|
-
},
|
|
1044
|
-
required: [
|
|
1045
|
-
"value"
|
|
1046
|
-
]
|
|
1047
|
-
},
|
|
1048
|
-
headers: {
|
|
1049
|
-
type: "object",
|
|
1050
|
-
properties: {
|
|
1051
|
-
host: {
|
|
1052
|
-
type: "string"
|
|
1053
|
-
}
|
|
1054
|
-
},
|
|
1055
|
-
required: [
|
|
1056
|
-
"host"
|
|
1057
|
-
]
|
|
1058
|
-
},
|
|
1059
|
-
response: {
|
|
1060
|
-
302: {
|
|
1061
|
-
type: "string"
|
|
1062
|
-
}
|
|
1063
|
-
}
|
|
1064
|
-
};
|
|
1065
|
-
var absoluteRedirectRoute = /* @__PURE__ */ __name((fastify) => {
|
|
1066
|
-
fastify.get("/absolute-redirect/:value", {
|
|
1067
|
-
schema: absoluteRedirectSchema
|
|
1068
|
-
}, async (request, reply) => {
|
|
1069
|
-
const { value } = request.params;
|
|
1070
|
-
const { host } = request.headers;
|
|
1071
|
-
const { protocol } = request;
|
|
1072
|
-
let url = `${protocol}://${host}/get`;
|
|
1073
|
-
let html = `
|
|
65
|
+
`)})},"indexRoute");var Xe={tags:["Redirects"],description:"Redirects the request to the target URL using an absolute URL",params:{type:"object",properties:{value:{type:"integer",minimum:1}},required:["value"]},headers:{type:"object",properties:{host:{type:"string"}},required:["host"]},response:{302:{type:"string"}}},K=r(s=>{s.get("/absolute-redirect/:value",{schema:Xe},async(e,t)=>{let{value:o}=e.params,{host:a}=e.headers,{protocol:i}=e,n=`${i}://${a}/get`,d=`
|
|
1074
66
|
<title>Redirecting...</title>
|
|
1075
67
|
<h1>Redirecting...</h1>
|
|
1076
|
-
<p>You should be redirected automatically to target URL: <a href="${
|
|
1077
|
-
`;
|
|
1078
|
-
if (value > 1) {
|
|
1079
|
-
url = `${protocol}://${host}/absolute-redirect/${value - 1}`;
|
|
1080
|
-
html = `
|
|
68
|
+
<p>You should be redirected automatically to target URL: <a href="${n}">${n}</a>. If not click the link.</p>
|
|
69
|
+
`;o>1&&(n=`${i}://${a}/absolute-redirect/${o-1}`,d=`
|
|
1081
70
|
<title>Redirecting...</title>
|
|
1082
71
|
<h1>Redirecting...</h1>
|
|
1083
|
-
<p>You should be redirected automatically to target URL: <a href="${
|
|
1084
|
-
|
|
1085
|
-
}
|
|
1086
|
-
await reply.header("Location", url).status(302).type("text/html").send(html);
|
|
1087
|
-
});
|
|
1088
|
-
}, "absoluteRedirectRoute");
|
|
1089
|
-
|
|
1090
|
-
// src/routes/redirects/redirect-to.ts
|
|
1091
|
-
var redirectToSchema = {
|
|
1092
|
-
tags: [
|
|
1093
|
-
"Redirects"
|
|
1094
|
-
],
|
|
1095
|
-
description: "Redirects the request to the target URL",
|
|
1096
|
-
querystring: {
|
|
1097
|
-
type: "object",
|
|
1098
|
-
properties: {
|
|
1099
|
-
url: {
|
|
1100
|
-
type: "string",
|
|
1101
|
-
format: "uri",
|
|
1102
|
-
description: "The URL to redirect to"
|
|
1103
|
-
},
|
|
1104
|
-
status_code: {
|
|
1105
|
-
type: "string",
|
|
1106
|
-
pattern: "^[1-5][0-9][0-9]$",
|
|
1107
|
-
description: "The HTTP status code to use for redirection"
|
|
1108
|
-
}
|
|
1109
|
-
},
|
|
1110
|
-
required: [
|
|
1111
|
-
"url"
|
|
1112
|
-
],
|
|
1113
|
-
additionalProperties: false
|
|
1114
|
-
}
|
|
1115
|
-
};
|
|
1116
|
-
var redirectToRoute = /* @__PURE__ */ __name((fastify) => {
|
|
1117
|
-
const handler = /* @__PURE__ */ __name(async (request, reply) => {
|
|
1118
|
-
const { url, status_code } = request.query;
|
|
1119
|
-
const statusCode = status_code ? Number(status_code) : 302;
|
|
1120
|
-
await reply.redirect(url, statusCode);
|
|
1121
|
-
}, "handler");
|
|
1122
|
-
fastify.get("/redirect-to", {
|
|
1123
|
-
schema: redirectToSchema
|
|
1124
|
-
}, handler);
|
|
1125
|
-
fastify.post("/redirect-to", {
|
|
1126
|
-
schema: redirectToSchema
|
|
1127
|
-
}, handler);
|
|
1128
|
-
fastify.put("/redirect-to", {
|
|
1129
|
-
schema: redirectToSchema
|
|
1130
|
-
}, handler);
|
|
1131
|
-
fastify.patch("/redirect-to", {
|
|
1132
|
-
schema: redirectToSchema
|
|
1133
|
-
}, handler);
|
|
1134
|
-
fastify.delete("/redirect-to", {
|
|
1135
|
-
schema: redirectToSchema
|
|
1136
|
-
}, handler);
|
|
1137
|
-
}, "redirectToRoute");
|
|
1138
|
-
|
|
1139
|
-
// src/routes/redirects/relative-redirect.ts
|
|
1140
|
-
var relativeRedirectSchema = {
|
|
1141
|
-
tags: [
|
|
1142
|
-
"Redirects"
|
|
1143
|
-
],
|
|
1144
|
-
description: "Redirects the request to the target URL using an relative URL",
|
|
1145
|
-
params: {
|
|
1146
|
-
type: "object",
|
|
1147
|
-
properties: {
|
|
1148
|
-
value: {
|
|
1149
|
-
type: "integer",
|
|
1150
|
-
minimum: 1
|
|
1151
|
-
}
|
|
1152
|
-
},
|
|
1153
|
-
required: [
|
|
1154
|
-
"value"
|
|
1155
|
-
]
|
|
1156
|
-
},
|
|
1157
|
-
headers: {
|
|
1158
|
-
type: "object",
|
|
1159
|
-
properties: {
|
|
1160
|
-
host: {
|
|
1161
|
-
type: "string"
|
|
1162
|
-
}
|
|
1163
|
-
},
|
|
1164
|
-
required: [
|
|
1165
|
-
"host"
|
|
1166
|
-
]
|
|
1167
|
-
},
|
|
1168
|
-
response: {
|
|
1169
|
-
302: {
|
|
1170
|
-
type: "string"
|
|
1171
|
-
}
|
|
1172
|
-
}
|
|
1173
|
-
};
|
|
1174
|
-
var relativeRedirectRoute = /* @__PURE__ */ __name((fastify) => {
|
|
1175
|
-
fastify.get("/relative-redirect/:value", {
|
|
1176
|
-
schema: relativeRedirectSchema
|
|
1177
|
-
}, async (request, reply) => {
|
|
1178
|
-
const { value } = request.params;
|
|
1179
|
-
let url = "/get";
|
|
1180
|
-
let html = `
|
|
72
|
+
<p>You should be redirected automatically to target URL: <a href="${n}">${n}</a>. If not click the link.</p>
|
|
73
|
+
`),await t.header("Location",n).status(302).type("text/html").send(d)})},"absoluteRedirectRoute");var T={tags:["Redirects"],description:"Redirects the request to the target URL",querystring:{type:"object",properties:{url:{type:"string",format:"uri",description:"The URL to redirect to"},status_code:{type:"string",pattern:"^[1-5][0-9][0-9]$",description:"The HTTP status code to use for redirection"}},required:["url"],additionalProperties:!1}},Q=r(s=>{let e=r(async(t,o)=>{let{url:a,status_code:i}=t.query,n=i?Number(i):302;await o.redirect(a,n)},"handler");s.get("/redirect-to",{schema:T},e),s.post("/redirect-to",{schema:T},e),s.put("/redirect-to",{schema:T},e),s.patch("/redirect-to",{schema:T},e),s.delete("/redirect-to",{schema:T},e)},"redirectToRoute");var Ke={tags:["Redirects"],description:"Redirects the request to the target URL using an relative URL",params:{type:"object",properties:{value:{type:"integer",minimum:1}},required:["value"]},headers:{type:"object",properties:{host:{type:"string"}},required:["host"]},response:{302:{type:"string"}}},ee=r(s=>{s.get("/relative-redirect/:value",{schema:Ke},async(e,t)=>{let{value:o}=e.params,a="/get",i=`
|
|
1181
74
|
<title>Redirecting...</title>
|
|
1182
75
|
<h1>Redirecting...</h1>
|
|
1183
|
-
<p>You should be redirected automatically to target URL: <a href="${
|
|
1184
|
-
`;
|
|
1185
|
-
if (value > 1) {
|
|
1186
|
-
url = `/relative-redirect/${value - 1}`;
|
|
1187
|
-
html = `
|
|
76
|
+
<p>You should be redirected automatically to target URL: <a href="${a}">${a}</a>. If not click the link.</p>
|
|
77
|
+
`;o>1&&(a=`/relative-redirect/${o-1}`,i=`
|
|
1188
78
|
<title>Redirecting...</title>
|
|
1189
79
|
<h1>Redirecting...</h1>
|
|
1190
|
-
<p>You should be redirected automatically to target URL: <a href="${
|
|
1191
|
-
|
|
1192
|
-
}
|
|
1193
|
-
await reply.header("Location", url).status(302).type("text/html").send(html);
|
|
1194
|
-
});
|
|
1195
|
-
}, "relativeRedirectRoute");
|
|
1196
|
-
|
|
1197
|
-
// src/routes/request-inspection/headers.ts
|
|
1198
|
-
var headersSchema = {
|
|
1199
|
-
description: "Returns all headers of the client request",
|
|
1200
|
-
tags: [
|
|
1201
|
-
"Request Inspection"
|
|
1202
|
-
],
|
|
1203
|
-
response: {
|
|
1204
|
-
200: {
|
|
1205
|
-
type: "object",
|
|
1206
|
-
properties: {
|
|
1207
|
-
headers: {
|
|
1208
|
-
type: "object",
|
|
1209
|
-
additionalProperties: {
|
|
1210
|
-
type: "string"
|
|
1211
|
-
},
|
|
1212
|
-
description: "All headers of the client request"
|
|
1213
|
-
}
|
|
1214
|
-
},
|
|
1215
|
-
required: [
|
|
1216
|
-
"headers"
|
|
1217
|
-
]
|
|
1218
|
-
}
|
|
1219
|
-
}
|
|
1220
|
-
};
|
|
1221
|
-
var headersRoute = /* @__PURE__ */ __name((fastify) => {
|
|
1222
|
-
fastify.get("/headers", {
|
|
1223
|
-
schema: headersSchema
|
|
1224
|
-
}, async (request) => {
|
|
1225
|
-
const { headers } = request;
|
|
1226
|
-
return {
|
|
1227
|
-
headers
|
|
1228
|
-
};
|
|
1229
|
-
});
|
|
1230
|
-
}, "headersRoute");
|
|
1231
|
-
|
|
1232
|
-
// src/routes/request-inspection/ip.ts
|
|
1233
|
-
var ipSchema = {
|
|
1234
|
-
description: "Returns the IP address of the client",
|
|
1235
|
-
tags: [
|
|
1236
|
-
"Request Inspection"
|
|
1237
|
-
],
|
|
1238
|
-
response: {
|
|
1239
|
-
200: {
|
|
1240
|
-
type: "object",
|
|
1241
|
-
properties: {
|
|
1242
|
-
ip: {
|
|
1243
|
-
type: "string",
|
|
1244
|
-
description: "The IP address of the client"
|
|
1245
|
-
}
|
|
1246
|
-
},
|
|
1247
|
-
required: [
|
|
1248
|
-
"ip"
|
|
1249
|
-
]
|
|
1250
|
-
}
|
|
1251
|
-
}
|
|
1252
|
-
};
|
|
1253
|
-
var ipRoute = /* @__PURE__ */ __name((fastify) => {
|
|
1254
|
-
fastify.get("/ip", {
|
|
1255
|
-
schema: ipSchema
|
|
1256
|
-
}, async (request) => {
|
|
1257
|
-
const cloudflareIp = request.headers["cf-connecting-ip"];
|
|
1258
|
-
const forwardedIp = request.headers["x-forwarded-for"];
|
|
1259
|
-
const remoteIp = request.ip;
|
|
1260
|
-
const ip = cloudflareIp ?? (typeof forwardedIp === "string" ? forwardedIp.split(",")[0].trim() : remoteIp);
|
|
1261
|
-
return {
|
|
1262
|
-
ip
|
|
1263
|
-
};
|
|
1264
|
-
});
|
|
1265
|
-
}, "ipRoute");
|
|
1266
|
-
|
|
1267
|
-
// src/routes/request-inspection/user-agent.ts
|
|
1268
|
-
var userAgentSchema = {
|
|
1269
|
-
description: "Returns the User-Agent header of the client request",
|
|
1270
|
-
tags: [
|
|
1271
|
-
"Request Inspection"
|
|
1272
|
-
],
|
|
1273
|
-
response: {
|
|
1274
|
-
200: {
|
|
1275
|
-
type: "object",
|
|
1276
|
-
properties: {
|
|
1277
|
-
userAgent: {
|
|
1278
|
-
type: "string",
|
|
1279
|
-
description: "The User-Agent header of the client request"
|
|
1280
|
-
}
|
|
1281
|
-
},
|
|
1282
|
-
required: [
|
|
1283
|
-
"userAgent"
|
|
1284
|
-
]
|
|
1285
|
-
}
|
|
1286
|
-
}
|
|
1287
|
-
};
|
|
1288
|
-
var userAgentRoute = /* @__PURE__ */ __name((fastify) => {
|
|
1289
|
-
fastify.get("/user-agent", {
|
|
1290
|
-
schema: userAgentSchema
|
|
1291
|
-
}, async (request) => {
|
|
1292
|
-
const userAgent = request.headers["user-agent"];
|
|
1293
|
-
return {
|
|
1294
|
-
userAgent
|
|
1295
|
-
};
|
|
1296
|
-
});
|
|
1297
|
-
}, "userAgentRoute");
|
|
1298
|
-
|
|
1299
|
-
// src/routes/response-formats/index.ts
|
|
1300
|
-
import { brotliCompressSync, deflateSync, gzipSync } from "zlib";
|
|
1301
|
-
var plainSchema = {
|
|
1302
|
-
description: "Returns random plain text content",
|
|
1303
|
-
tags: [
|
|
1304
|
-
"Response Formats"
|
|
1305
|
-
],
|
|
1306
|
-
response: {
|
|
1307
|
-
200: {
|
|
1308
|
-
type: "string",
|
|
1309
|
-
description: "Random plain text content"
|
|
1310
|
-
}
|
|
1311
|
-
}
|
|
1312
|
-
};
|
|
1313
|
-
var textSchema = {
|
|
1314
|
-
description: "Returns random text content",
|
|
1315
|
-
tags: [
|
|
1316
|
-
"Response Formats"
|
|
1317
|
-
],
|
|
1318
|
-
response: {
|
|
1319
|
-
200: {
|
|
1320
|
-
type: "string",
|
|
1321
|
-
description: "Random text content"
|
|
1322
|
-
}
|
|
1323
|
-
}
|
|
1324
|
-
};
|
|
1325
|
-
var htmlSchema = {
|
|
1326
|
-
description: "Returns random HTML content",
|
|
1327
|
-
tags: [
|
|
1328
|
-
"Response Formats"
|
|
1329
|
-
],
|
|
1330
|
-
response: {
|
|
1331
|
-
200: {
|
|
1332
|
-
type: "string",
|
|
1333
|
-
description: "Random HTML content"
|
|
1334
|
-
}
|
|
1335
|
-
}
|
|
1336
|
-
};
|
|
1337
|
-
var xmlSchema = {
|
|
1338
|
-
description: "Returns random XML content",
|
|
1339
|
-
tags: [
|
|
1340
|
-
"Response Formats"
|
|
1341
|
-
],
|
|
1342
|
-
response: {
|
|
1343
|
-
200: {
|
|
1344
|
-
type: "string",
|
|
1345
|
-
description: "Random XML content"
|
|
1346
|
-
}
|
|
1347
|
-
}
|
|
1348
|
-
};
|
|
1349
|
-
var jsonSchema = {
|
|
1350
|
-
description: "Returns random JSON content",
|
|
1351
|
-
tags: [
|
|
1352
|
-
"Response Formats"
|
|
1353
|
-
],
|
|
1354
|
-
response: {
|
|
1355
|
-
200: {
|
|
1356
|
-
type: "object",
|
|
1357
|
-
description: "Random JSON content",
|
|
1358
|
-
additionalProperties: true
|
|
1359
|
-
}
|
|
1360
|
-
}
|
|
1361
|
-
};
|
|
1362
|
-
var denySchema = {
|
|
1363
|
-
description: "Returns page denied by robots.txt rules",
|
|
1364
|
-
tags: [
|
|
1365
|
-
"Response Formats"
|
|
1366
|
-
],
|
|
1367
|
-
response: {
|
|
1368
|
-
403: {
|
|
1369
|
-
type: "string",
|
|
1370
|
-
description: "Page denied by robots.txt rules"
|
|
1371
|
-
}
|
|
1372
|
-
}
|
|
1373
|
-
};
|
|
1374
|
-
var gzipSchema = {
|
|
1375
|
-
description: "Returns GZip-encoded data",
|
|
1376
|
-
tags: [
|
|
1377
|
-
"Response Formats"
|
|
1378
|
-
],
|
|
1379
|
-
response: {
|
|
1380
|
-
200: {
|
|
1381
|
-
type: "string",
|
|
1382
|
-
description: "GZip-encoded content"
|
|
1383
|
-
}
|
|
1384
|
-
}
|
|
1385
|
-
};
|
|
1386
|
-
var deflateSchema = {
|
|
1387
|
-
description: "Returns Deflate-encoded data",
|
|
1388
|
-
tags: [
|
|
1389
|
-
"Response Formats"
|
|
1390
|
-
],
|
|
1391
|
-
response: {
|
|
1392
|
-
200: {
|
|
1393
|
-
type: "string",
|
|
1394
|
-
description: "Deflate-encoded content"
|
|
1395
|
-
}
|
|
1396
|
-
}
|
|
1397
|
-
};
|
|
1398
|
-
var brotliSchema = {
|
|
1399
|
-
description: "Returns Brotli-encoded data",
|
|
1400
|
-
tags: [
|
|
1401
|
-
"Response Formats"
|
|
1402
|
-
],
|
|
1403
|
-
response: {
|
|
1404
|
-
200: {
|
|
1405
|
-
type: "string",
|
|
1406
|
-
description: "Brotli-encoded content"
|
|
1407
|
-
}
|
|
1408
|
-
}
|
|
1409
|
-
};
|
|
1410
|
-
var randomTexts = [
|
|
1411
|
-
"The quick brown fox jumps over the lazy dog.",
|
|
1412
|
-
"Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
|
|
1413
|
-
"To be or not to be, that is the question.",
|
|
1414
|
-
"All work and no play makes Jack a dull boy.",
|
|
1415
|
-
"The only way to do great work is to love what you do.",
|
|
1416
|
-
"In the beginning was the Word, and the Word was with God.",
|
|
1417
|
-
"Once upon a time in a galaxy far, far away...",
|
|
1418
|
-
"It was the best of times, it was the worst of times.",
|
|
1419
|
-
"The journey of a thousand miles begins with a single step.",
|
|
1420
|
-
"Knowledge is power. France is bacon."
|
|
1421
|
-
];
|
|
1422
|
-
var randomHtmlContent = [
|
|
1423
|
-
`<!DOCTYPE html>
|
|
80
|
+
<p>You should be redirected automatically to target URL: <a href="${a}">${a}</a>. If not click the link.</p>
|
|
81
|
+
`),await t.header("Location",a).status(302).type("text/html").send(i)})},"relativeRedirectRoute");var Qe={description:"Returns all headers of the client request",tags:["Request Inspection"],response:{200:{type:"object",properties:{headers:{type:"object",additionalProperties:{type:"string"},description:"All headers of the client request"}},required:["headers"]}}},te=r(s=>{s.get("/headers",{schema:Qe},async e=>{let{headers:t}=e;return{headers:t}})},"headersRoute");var et={description:"Returns the IP address of the client",tags:["Request Inspection"],response:{200:{type:"object",properties:{ip:{type:"string",description:"The IP address of the client"}},required:["ip"]}}},se=r(s=>{s.get("/ip",{schema:et},async e=>{let t=e.headers["cf-connecting-ip"],o=e.headers["x-forwarded-for"],a=e.ip;return{ip:t??(typeof o=="string"?o.split(",")[0].trim():a)}})},"ipRoute");var tt={description:"Returns the User-Agent header of the client request",tags:["Request Inspection"],response:{200:{type:"object",properties:{userAgent:{type:"string",description:"The User-Agent header of the client request"}},required:["userAgent"]}}},oe=r(s=>{s.get("/user-agent",{schema:tt},async e=>({userAgent:e.headers["user-agent"]}))},"userAgentRoute");import{brotliCompressSync as fe,deflateSync as ye,gzipSync as be}from"zlib";var S={description:"Returns random plain text content",tags:["Response Formats"],response:{200:{type:"string",description:"Random plain text content"}}},j={description:"Returns random text content",tags:["Response Formats"],response:{200:{type:"string",description:"Random text content"}}},C={description:"Returns random HTML content",tags:["Response Formats"],response:{200:{type:"string",description:"Random HTML content"}}},P={description:"Returns random XML content",tags:["Response Formats"],response:{200:{type:"string",description:"Random XML content"}}},q={description:"Returns random JSON content",tags:["Response Formats"],response:{200:{type:"object",description:"Random JSON content",additionalProperties:!0}}},_={description:"Returns page denied by robots.txt rules",tags:["Response Formats"],response:{403:{type:"string",description:"Page denied by robots.txt rules"}}},H={description:"Returns GZip-encoded data",tags:["Response Formats"],response:{200:{type:"string",description:"GZip-encoded content"}}},$={description:"Returns Deflate-encoded data",tags:["Response Formats"],response:{200:{type:"string",description:"Deflate-encoded content"}}},D={description:"Returns Brotli-encoded data",tags:["Response Formats"],response:{200:{type:"string",description:"Brotli-encoded content"}}},z=["The quick brown fox jumps over the lazy dog.","Lorem ipsum dolor sit amet, consectetur adipiscing elit.","To be or not to be, that is the question.","All work and no play makes Jack a dull boy.","The only way to do great work is to love what you do.","In the beginning was the Word, and the Word was with God.","Once upon a time in a galaxy far, far away...","It was the best of times, it was the worst of times.","The journey of a thousand miles begins with a single step.","Knowledge is power. France is bacon."],Re=[`<!DOCTYPE html>
|
|
1424
82
|
<html lang="en">
|
|
1425
83
|
<head>
|
|
1426
84
|
<title>Sample Page</title>
|
|
@@ -1429,8 +87,7 @@ var randomHtmlContent = [
|
|
|
1429
87
|
<h1>Welcome to MockHTTP</h1>
|
|
1430
88
|
<p>This is a sample HTML page with random content.</p>
|
|
1431
89
|
</body>
|
|
1432
|
-
</html
|
|
1433
|
-
`<!DOCTYPE html>
|
|
90
|
+
</html>`,`<!DOCTYPE html>
|
|
1434
91
|
<html>
|
|
1435
92
|
<head>
|
|
1436
93
|
<meta charset="UTF-8">
|
|
@@ -1451,8 +108,7 @@ var randomHtmlContent = [
|
|
|
1451
108
|
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
|
|
1452
109
|
</main>
|
|
1453
110
|
</body>
|
|
1454
|
-
</html
|
|
1455
|
-
`<!DOCTYPE html>
|
|
111
|
+
</html>`,`<!DOCTYPE html>
|
|
1456
112
|
<html lang="en">
|
|
1457
113
|
<head>
|
|
1458
114
|
<title>Article Page</title>
|
|
@@ -1470,8 +126,7 @@ var randomHtmlContent = [
|
|
|
1470
126
|
</blockquote>
|
|
1471
127
|
</article>
|
|
1472
128
|
</body>
|
|
1473
|
-
</html
|
|
1474
|
-
`<!DOCTYPE html>
|
|
129
|
+
</html>`,`<!DOCTYPE html>
|
|
1475
130
|
<html>
|
|
1476
131
|
<head>
|
|
1477
132
|
<title>Form Example</title>
|
|
@@ -1488,8 +143,7 @@ var randomHtmlContent = [
|
|
|
1488
143
|
<button type="submit">Submit</button>
|
|
1489
144
|
</form>
|
|
1490
145
|
</body>
|
|
1491
|
-
</html
|
|
1492
|
-
`<!DOCTYPE html>
|
|
146
|
+
</html>`,`<!DOCTYPE html>
|
|
1493
147
|
<html lang="en">
|
|
1494
148
|
<head>
|
|
1495
149
|
<meta charset="UTF-8">
|
|
@@ -1503,24 +157,19 @@ var randomHtmlContent = [
|
|
|
1503
157
|
<img src="placeholder.jpg" alt="Placeholder" style="max-width: 100%; height: auto;">
|
|
1504
158
|
</div>
|
|
1505
159
|
</body>
|
|
1506
|
-
</html>`
|
|
1507
|
-
];
|
|
1508
|
-
var randomXmlContent = [
|
|
1509
|
-
`<?xml version="1.0" encoding="UTF-8"?>
|
|
160
|
+
</html>`],we=[`<?xml version="1.0" encoding="UTF-8"?>
|
|
1510
161
|
<root>
|
|
1511
162
|
<message>Hello from MockHTTP</message>
|
|
1512
|
-
<timestamp>${
|
|
163
|
+
<timestamp>${new Date().toISOString()}</timestamp>
|
|
1513
164
|
<status>success</status>
|
|
1514
|
-
</root
|
|
1515
|
-
`<?xml version="1.0" encoding="UTF-8"?>
|
|
165
|
+
</root>`,`<?xml version="1.0" encoding="UTF-8"?>
|
|
1516
166
|
<book>
|
|
1517
167
|
<title>The Great Gatsby</title>
|
|
1518
168
|
<author>F. Scott Fitzgerald</author>
|
|
1519
169
|
<year>1925</year>
|
|
1520
170
|
<publisher>Charles Scribner's Sons</publisher>
|
|
1521
171
|
<isbn>978-0-7432-7356-5</isbn>
|
|
1522
|
-
</book
|
|
1523
|
-
`<?xml version="1.0" encoding="UTF-8"?>
|
|
172
|
+
</book>`,`<?xml version="1.0" encoding="UTF-8"?>
|
|
1524
173
|
<users>
|
|
1525
174
|
<user id="1">
|
|
1526
175
|
<name>John Doe</name>
|
|
@@ -1532,8 +181,7 @@ var randomXmlContent = [
|
|
|
1532
181
|
<email>jane@example.com</email>
|
|
1533
182
|
<role>user</role>
|
|
1534
183
|
</user>
|
|
1535
|
-
</users
|
|
1536
|
-
`<?xml version="1.0" encoding="UTF-8"?>
|
|
184
|
+
</users>`,`<?xml version="1.0" encoding="UTF-8"?>
|
|
1537
185
|
<product>
|
|
1538
186
|
<id>12345</id>
|
|
1539
187
|
<name>Wireless Mouse</name>
|
|
@@ -1541,8 +189,7 @@ var randomXmlContent = [
|
|
|
1541
189
|
<price currency="USD">29.99</price>
|
|
1542
190
|
<stock>150</stock>
|
|
1543
191
|
<category>Electronics</category>
|
|
1544
|
-
</product
|
|
1545
|
-
`<?xml version="1.0" encoding="UTF-8"?>
|
|
192
|
+
</product>`,`<?xml version="1.0" encoding="UTF-8"?>
|
|
1546
193
|
<rss version="2.0">
|
|
1547
194
|
<channel>
|
|
1548
195
|
<title>Mock RSS Feed</title>
|
|
@@ -1552,210 +199,17 @@ var randomXmlContent = [
|
|
|
1552
199
|
<title>First Article</title>
|
|
1553
200
|
<link>http://example.com/article1</link>
|
|
1554
201
|
<description>This is the first article in our feed</description>
|
|
1555
|
-
<pubDate>${
|
|
202
|
+
<pubDate>${new Date().toUTCString()}</pubDate>
|
|
1556
203
|
</item>
|
|
1557
204
|
</channel>
|
|
1558
|
-
</rss>`
|
|
1559
|
-
];
|
|
1560
|
-
var randomJsonContent = [
|
|
1561
|
-
() => ({
|
|
1562
|
-
message: "Hello from MockHTTP",
|
|
1563
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1564
|
-
status: "success",
|
|
1565
|
-
code: 200
|
|
1566
|
-
}),
|
|
1567
|
-
() => ({
|
|
1568
|
-
user: {
|
|
1569
|
-
id: 1,
|
|
1570
|
-
name: "John Doe",
|
|
1571
|
-
email: "john@example.com",
|
|
1572
|
-
role: "admin",
|
|
1573
|
-
createdAt: "2024-01-15T10:30:00Z"
|
|
1574
|
-
},
|
|
1575
|
-
permissions: [
|
|
1576
|
-
"read",
|
|
1577
|
-
"write",
|
|
1578
|
-
"delete"
|
|
1579
|
-
]
|
|
1580
|
-
}),
|
|
1581
|
-
() => ({
|
|
1582
|
-
products: [
|
|
1583
|
-
{
|
|
1584
|
-
id: 101,
|
|
1585
|
-
name: "Laptop",
|
|
1586
|
-
price: 999.99,
|
|
1587
|
-
stock: 50
|
|
1588
|
-
},
|
|
1589
|
-
{
|
|
1590
|
-
id: 102,
|
|
1591
|
-
name: "Mouse",
|
|
1592
|
-
price: 29.99,
|
|
1593
|
-
stock: 200
|
|
1594
|
-
}
|
|
1595
|
-
],
|
|
1596
|
-
total: 2,
|
|
1597
|
-
page: 1
|
|
1598
|
-
}),
|
|
1599
|
-
() => ({
|
|
1600
|
-
config: {
|
|
1601
|
-
theme: "dark",
|
|
1602
|
-
language: "en",
|
|
1603
|
-
notifications: {
|
|
1604
|
-
email: true,
|
|
1605
|
-
push: false,
|
|
1606
|
-
sms: false
|
|
1607
|
-
},
|
|
1608
|
-
privacy: {
|
|
1609
|
-
profile: "public",
|
|
1610
|
-
activity: "friends"
|
|
1611
|
-
}
|
|
1612
|
-
}
|
|
1613
|
-
}),
|
|
1614
|
-
() => ({
|
|
1615
|
-
metrics: {
|
|
1616
|
-
cpu: 45.2,
|
|
1617
|
-
memory: 78.5,
|
|
1618
|
-
disk: 62.3,
|
|
1619
|
-
network: {
|
|
1620
|
-
inbound: "1.2MB/s",
|
|
1621
|
-
outbound: "0.8MB/s"
|
|
1622
|
-
},
|
|
1623
|
-
uptime: "15 days",
|
|
1624
|
-
services: [
|
|
1625
|
-
{
|
|
1626
|
-
name: "api",
|
|
1627
|
-
status: "healthy"
|
|
1628
|
-
},
|
|
1629
|
-
{
|
|
1630
|
-
name: "database",
|
|
1631
|
-
status: "healthy"
|
|
1632
|
-
},
|
|
1633
|
-
{
|
|
1634
|
-
name: "cache",
|
|
1635
|
-
status: "warning"
|
|
1636
|
-
}
|
|
1637
|
-
]
|
|
1638
|
-
}
|
|
1639
|
-
})
|
|
1640
|
-
];
|
|
1641
|
-
var responseFormatRoutes = /* @__PURE__ */ __name((fastify) => {
|
|
1642
|
-
const plainHandler = /* @__PURE__ */ __name(async (_request, reply) => {
|
|
1643
|
-
const randomIndex = Math.floor(Math.random() * randomTexts.length);
|
|
1644
|
-
const text = randomTexts[randomIndex];
|
|
1645
|
-
reply.type("text/plain");
|
|
1646
|
-
return text;
|
|
1647
|
-
}, "plainHandler");
|
|
1648
|
-
fastify.get("/plain", {
|
|
1649
|
-
schema: plainSchema
|
|
1650
|
-
}, plainHandler);
|
|
1651
|
-
fastify.post("/plain", {
|
|
1652
|
-
schema: plainSchema
|
|
1653
|
-
}, plainHandler);
|
|
1654
|
-
fastify.put("/plain", {
|
|
1655
|
-
schema: plainSchema
|
|
1656
|
-
}, plainHandler);
|
|
1657
|
-
fastify.patch("/plain", {
|
|
1658
|
-
schema: plainSchema
|
|
1659
|
-
}, plainHandler);
|
|
1660
|
-
fastify.delete("/plain", {
|
|
1661
|
-
schema: plainSchema
|
|
1662
|
-
}, plainHandler);
|
|
1663
|
-
const textHandler = /* @__PURE__ */ __name(async (_request, reply) => {
|
|
1664
|
-
const randomIndex = Math.floor(Math.random() * randomTexts.length);
|
|
1665
|
-
const text = randomTexts[randomIndex];
|
|
1666
|
-
reply.type("text/plain");
|
|
1667
|
-
return text;
|
|
1668
|
-
}, "textHandler");
|
|
1669
|
-
fastify.get("/text", {
|
|
1670
|
-
schema: textSchema
|
|
1671
|
-
}, textHandler);
|
|
1672
|
-
fastify.post("/text", {
|
|
1673
|
-
schema: textSchema
|
|
1674
|
-
}, textHandler);
|
|
1675
|
-
fastify.put("/text", {
|
|
1676
|
-
schema: textSchema
|
|
1677
|
-
}, textHandler);
|
|
1678
|
-
fastify.patch("/text", {
|
|
1679
|
-
schema: textSchema
|
|
1680
|
-
}, textHandler);
|
|
1681
|
-
fastify.delete("/text", {
|
|
1682
|
-
schema: textSchema
|
|
1683
|
-
}, textHandler);
|
|
1684
|
-
const htmlHandler = /* @__PURE__ */ __name(async (_request, reply) => {
|
|
1685
|
-
const randomIndex = Math.floor(Math.random() * randomHtmlContent.length);
|
|
1686
|
-
const html = randomHtmlContent[randomIndex];
|
|
1687
|
-
reply.type("text/html");
|
|
1688
|
-
return html;
|
|
1689
|
-
}, "htmlHandler");
|
|
1690
|
-
fastify.get("/html", {
|
|
1691
|
-
schema: htmlSchema
|
|
1692
|
-
}, htmlHandler);
|
|
1693
|
-
fastify.post("/html", {
|
|
1694
|
-
schema: htmlSchema
|
|
1695
|
-
}, htmlHandler);
|
|
1696
|
-
fastify.put("/html", {
|
|
1697
|
-
schema: htmlSchema
|
|
1698
|
-
}, htmlHandler);
|
|
1699
|
-
fastify.patch("/html", {
|
|
1700
|
-
schema: htmlSchema
|
|
1701
|
-
}, htmlHandler);
|
|
1702
|
-
fastify.delete("/html", {
|
|
1703
|
-
schema: htmlSchema
|
|
1704
|
-
}, htmlHandler);
|
|
1705
|
-
const xmlHandler = /* @__PURE__ */ __name(async (_request, reply) => {
|
|
1706
|
-
const randomIndex = Math.floor(Math.random() * randomXmlContent.length);
|
|
1707
|
-
const xml = randomXmlContent[randomIndex];
|
|
1708
|
-
reply.type("application/xml");
|
|
1709
|
-
return xml;
|
|
1710
|
-
}, "xmlHandler");
|
|
1711
|
-
fastify.get("/xml", {
|
|
1712
|
-
schema: xmlSchema
|
|
1713
|
-
}, xmlHandler);
|
|
1714
|
-
fastify.post("/xml", {
|
|
1715
|
-
schema: xmlSchema
|
|
1716
|
-
}, xmlHandler);
|
|
1717
|
-
fastify.put("/xml", {
|
|
1718
|
-
schema: xmlSchema
|
|
1719
|
-
}, xmlHandler);
|
|
1720
|
-
fastify.patch("/xml", {
|
|
1721
|
-
schema: xmlSchema
|
|
1722
|
-
}, xmlHandler);
|
|
1723
|
-
fastify.delete("/xml", {
|
|
1724
|
-
schema: xmlSchema
|
|
1725
|
-
}, xmlHandler);
|
|
1726
|
-
const jsonHandler = /* @__PURE__ */ __name(async (_request, reply) => {
|
|
1727
|
-
const randomIndex = Math.floor(Math.random() * randomJsonContent.length);
|
|
1728
|
-
const jsonGenerator = randomJsonContent[randomIndex];
|
|
1729
|
-
const json = jsonGenerator();
|
|
1730
|
-
reply.type("application/json");
|
|
1731
|
-
return json;
|
|
1732
|
-
}, "jsonHandler");
|
|
1733
|
-
fastify.get("/json", {
|
|
1734
|
-
schema: jsonSchema
|
|
1735
|
-
}, jsonHandler);
|
|
1736
|
-
fastify.post("/json", {
|
|
1737
|
-
schema: jsonSchema
|
|
1738
|
-
}, jsonHandler);
|
|
1739
|
-
fastify.put("/json", {
|
|
1740
|
-
schema: jsonSchema
|
|
1741
|
-
}, jsonHandler);
|
|
1742
|
-
fastify.patch("/json", {
|
|
1743
|
-
schema: jsonSchema
|
|
1744
|
-
}, jsonHandler);
|
|
1745
|
-
fastify.delete("/json", {
|
|
1746
|
-
schema: jsonSchema
|
|
1747
|
-
}, jsonHandler);
|
|
1748
|
-
const denyHandler = /* @__PURE__ */ __name(async (_request, reply) => {
|
|
1749
|
-
const denyMessages = [
|
|
1750
|
-
`<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
|
|
205
|
+
</rss>`],xe=[()=>({message:"Hello from MockHTTP",timestamp:new Date().toISOString(),status:"success",code:200}),()=>({user:{id:1,name:"John Doe",email:"john@example.com",role:"admin",createdAt:"2024-01-15T10:30:00Z"},permissions:["read","write","delete"]}),()=>({products:[{id:101,name:"Laptop",price:999.99,stock:50},{id:102,name:"Mouse",price:29.99,stock:200}],total:2,page:1}),()=>({config:{theme:"dark",language:"en",notifications:{email:!0,push:!1,sms:!1},privacy:{profile:"public",activity:"friends"}}}),()=>({metrics:{cpu:45.2,memory:78.5,disk:62.3,network:{inbound:"1.2MB/s",outbound:"0.8MB/s"},uptime:"15 days",services:[{name:"api",status:"healthy"},{name:"database",status:"healthy"},{name:"cache",status:"warning"}]}})],ke=r(s=>{let e=r(async(l,c)=>{let p=Math.floor(Math.random()*z.length),u=z[p];return c.type("text/plain"),u},"plainHandler");s.get("/plain",{schema:S},e),s.post("/plain",{schema:S},e),s.put("/plain",{schema:S},e),s.patch("/plain",{schema:S},e),s.delete("/plain",{schema:S},e);let t=r(async(l,c)=>{let p=Math.floor(Math.random()*z.length),u=z[p];return c.type("text/plain"),u},"textHandler");s.get("/text",{schema:j},t),s.post("/text",{schema:j},t),s.put("/text",{schema:j},t),s.patch("/text",{schema:j},t),s.delete("/text",{schema:j},t);let o=r(async(l,c)=>{let p=Math.floor(Math.random()*Re.length),u=Re[p];return c.type("text/html"),u},"htmlHandler");s.get("/html",{schema:C},o),s.post("/html",{schema:C},o),s.put("/html",{schema:C},o),s.patch("/html",{schema:C},o),s.delete("/html",{schema:C},o);let a=r(async(l,c)=>{let p=Math.floor(Math.random()*we.length),u=we[p];return c.type("application/xml"),u},"xmlHandler");s.get("/xml",{schema:P},a),s.post("/xml",{schema:P},a),s.put("/xml",{schema:P},a),s.patch("/xml",{schema:P},a),s.delete("/xml",{schema:P},a);let i=r(async(l,c)=>{let p=Math.floor(Math.random()*xe.length),u=xe[p],m=u();return c.type("application/json"),m},"jsonHandler");s.get("/json",{schema:q},i),s.post("/json",{schema:q},i),s.put("/json",{schema:q},i),s.patch("/json",{schema:q},i),s.delete("/json",{schema:q},i);let n=r(async(l,c)=>{let p=[`<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
|
|
1751
206
|
<html><head>
|
|
1752
207
|
<title>403 Forbidden</title>
|
|
1753
208
|
</head><body>
|
|
1754
209
|
<h1>Forbidden</h1>
|
|
1755
210
|
<p>Access denied by robots.txt rules.</p>
|
|
1756
211
|
<p>The requested resource is disallowed by robots.txt.</p>
|
|
1757
|
-
</body></html
|
|
1758
|
-
`<!DOCTYPE html>
|
|
212
|
+
</body></html>`,`<!DOCTYPE html>
|
|
1759
213
|
<html lang="en">
|
|
1760
214
|
<head>
|
|
1761
215
|
<meta charset="UTF-8">
|
|
@@ -1764,10 +218,9 @@ var responseFormatRoutes = /* @__PURE__ */ __name((fastify) => {
|
|
|
1764
218
|
<body>
|
|
1765
219
|
<h1>403 Forbidden</h1>
|
|
1766
220
|
<p>This page is blocked by robots.txt rules.</p>
|
|
1767
|
-
<p>User-agent: *<br>Disallow: ${
|
|
221
|
+
<p>User-agent: *<br>Disallow: ${l.url}</p>
|
|
1768
222
|
</body>
|
|
1769
|
-
</html
|
|
1770
|
-
`<!DOCTYPE html>
|
|
223
|
+
</html>`,`<!DOCTYPE html>
|
|
1771
224
|
<html>
|
|
1772
225
|
<head>
|
|
1773
226
|
<title>403 - Access Denied</title>
|
|
@@ -1779,842 +232,14 @@ var responseFormatRoutes = /* @__PURE__ */ __name((fastify) => {
|
|
|
1779
232
|
<hr>
|
|
1780
233
|
<small>MockHTTP Server</small>
|
|
1781
234
|
</body>
|
|
1782
|
-
</html>`
|
|
1783
|
-
];
|
|
1784
|
-
const randomIndex = Math.floor(Math.random() * denyMessages.length);
|
|
1785
|
-
const html = denyMessages[randomIndex];
|
|
1786
|
-
reply.code(403);
|
|
1787
|
-
reply.type("text/html");
|
|
1788
|
-
return html;
|
|
1789
|
-
}, "denyHandler");
|
|
1790
|
-
fastify.get("/deny", {
|
|
1791
|
-
schema: denySchema
|
|
1792
|
-
}, denyHandler);
|
|
1793
|
-
fastify.post("/deny", {
|
|
1794
|
-
schema: denySchema
|
|
1795
|
-
}, denyHandler);
|
|
1796
|
-
fastify.put("/deny", {
|
|
1797
|
-
schema: denySchema
|
|
1798
|
-
}, denyHandler);
|
|
1799
|
-
fastify.patch("/deny", {
|
|
1800
|
-
schema: denySchema
|
|
1801
|
-
}, denyHandler);
|
|
1802
|
-
fastify.delete("/deny", {
|
|
1803
|
-
schema: denySchema
|
|
1804
|
-
}, denyHandler);
|
|
1805
|
-
const gzipHandler = /* @__PURE__ */ __name(async (_request, reply) => {
|
|
1806
|
-
const sampleData = [
|
|
1807
|
-
{
|
|
1808
|
-
message: "This is GZip compressed data from MockHTTP",
|
|
1809
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1810
|
-
compression: "gzip",
|
|
1811
|
-
originalSize: 0,
|
|
1812
|
-
compressedSize: 0
|
|
1813
|
-
},
|
|
1814
|
-
{
|
|
1815
|
-
title: "Sample Document",
|
|
1816
|
-
content: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. ".repeat(50),
|
|
1817
|
-
metadata: {
|
|
1818
|
-
author: "MockHTTP Server",
|
|
1819
|
-
format: "gzip",
|
|
1820
|
-
version: "1.0"
|
|
1821
|
-
}
|
|
1822
|
-
},
|
|
1823
|
-
{
|
|
1824
|
-
data: Array.from({
|
|
1825
|
-
length: 100
|
|
1826
|
-
}, (_, i) => ({
|
|
1827
|
-
id: i + 1,
|
|
1828
|
-
value: `Item ${i + 1}`,
|
|
1829
|
-
description: `This is item number ${i + 1} in the dataset`
|
|
1830
|
-
})),
|
|
1831
|
-
info: "Large dataset compressed with GZip"
|
|
1832
|
-
}
|
|
1833
|
-
];
|
|
1834
|
-
const randomIndex = Math.floor(Math.random() * sampleData.length);
|
|
1835
|
-
const data = sampleData[randomIndex];
|
|
1836
|
-
const jsonString = JSON.stringify(data);
|
|
1837
|
-
if (data.originalSize !== void 0) {
|
|
1838
|
-
data.originalSize = Buffer.byteLength(jsonString);
|
|
1839
|
-
}
|
|
1840
|
-
const compressed = gzipSync(jsonString);
|
|
1841
|
-
if (data.compressedSize !== void 0) {
|
|
1842
|
-
const updatedData = {
|
|
1843
|
-
...data,
|
|
1844
|
-
compressedSize: compressed.length
|
|
1845
|
-
};
|
|
1846
|
-
const updatedJsonString = JSON.stringify(updatedData);
|
|
1847
|
-
const updatedCompressed = gzipSync(updatedJsonString);
|
|
1848
|
-
reply.header("Content-Encoding", "gzip");
|
|
1849
|
-
reply.header("Content-Type", "application/json");
|
|
1850
|
-
reply.type("application/octet-stream");
|
|
1851
|
-
return updatedCompressed;
|
|
1852
|
-
}
|
|
1853
|
-
reply.header("Content-Encoding", "gzip");
|
|
1854
|
-
reply.header("Content-Type", "application/json");
|
|
1855
|
-
reply.type("application/octet-stream");
|
|
1856
|
-
return compressed;
|
|
1857
|
-
}, "gzipHandler");
|
|
1858
|
-
fastify.get("/gzip", {
|
|
1859
|
-
schema: gzipSchema
|
|
1860
|
-
}, gzipHandler);
|
|
1861
|
-
fastify.post("/gzip", {
|
|
1862
|
-
schema: gzipSchema
|
|
1863
|
-
}, gzipHandler);
|
|
1864
|
-
fastify.put("/gzip", {
|
|
1865
|
-
schema: gzipSchema
|
|
1866
|
-
}, gzipHandler);
|
|
1867
|
-
fastify.patch("/gzip", {
|
|
1868
|
-
schema: gzipSchema
|
|
1869
|
-
}, gzipHandler);
|
|
1870
|
-
fastify.delete("/gzip", {
|
|
1871
|
-
schema: gzipSchema
|
|
1872
|
-
}, gzipHandler);
|
|
1873
|
-
const deflateHandler = /* @__PURE__ */ __name(async (_request, reply) => {
|
|
1874
|
-
const sampleData = [
|
|
1875
|
-
{
|
|
1876
|
-
message: "This is Deflate compressed data from MockHTTP",
|
|
1877
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1878
|
-
compression: "deflate",
|
|
1879
|
-
originalSize: 0,
|
|
1880
|
-
compressedSize: 0
|
|
1881
|
-
},
|
|
1882
|
-
{
|
|
1883
|
-
title: "Deflate Compressed Document",
|
|
1884
|
-
content: "The deflate algorithm is used for compression. ".repeat(40),
|
|
1885
|
-
details: {
|
|
1886
|
-
algorithm: "DEFLATE",
|
|
1887
|
-
method: "zlib deflate",
|
|
1888
|
-
server: "MockHTTP"
|
|
1889
|
-
}
|
|
1890
|
-
},
|
|
1891
|
-
{
|
|
1892
|
-
items: Array.from({
|
|
1893
|
-
length: 50
|
|
1894
|
-
}, (_, i) => ({
|
|
1895
|
-
index: i,
|
|
1896
|
-
name: `Entry ${i}`,
|
|
1897
|
-
data: `This is test data for entry number ${i}`,
|
|
1898
|
-
timestamp: new Date(Date.now() - i * 1e6).toISOString()
|
|
1899
|
-
})),
|
|
1900
|
-
metadata: {
|
|
1901
|
-
compression: "deflate",
|
|
1902
|
-
count: 50
|
|
1903
|
-
}
|
|
1904
|
-
}
|
|
1905
|
-
];
|
|
1906
|
-
const randomIndex = Math.floor(Math.random() * sampleData.length);
|
|
1907
|
-
const data = sampleData[randomIndex];
|
|
1908
|
-
const jsonString = JSON.stringify(data);
|
|
1909
|
-
if (data.originalSize !== void 0) {
|
|
1910
|
-
data.originalSize = Buffer.byteLength(jsonString);
|
|
1911
|
-
}
|
|
1912
|
-
const compressed = deflateSync(jsonString);
|
|
1913
|
-
if (data.compressedSize !== void 0) {
|
|
1914
|
-
const updatedData = {
|
|
1915
|
-
...data,
|
|
1916
|
-
compressedSize: compressed.length
|
|
1917
|
-
};
|
|
1918
|
-
const updatedJsonString = JSON.stringify(updatedData);
|
|
1919
|
-
const updatedCompressed = deflateSync(updatedJsonString);
|
|
1920
|
-
reply.header("Content-Encoding", "deflate");
|
|
1921
|
-
reply.header("Content-Type", "application/json");
|
|
1922
|
-
reply.type("application/octet-stream");
|
|
1923
|
-
return updatedCompressed;
|
|
1924
|
-
}
|
|
1925
|
-
reply.header("Content-Encoding", "deflate");
|
|
1926
|
-
reply.header("Content-Type", "application/json");
|
|
1927
|
-
reply.type("application/octet-stream");
|
|
1928
|
-
return compressed;
|
|
1929
|
-
}, "deflateHandler");
|
|
1930
|
-
fastify.get("/deflate", {
|
|
1931
|
-
schema: deflateSchema
|
|
1932
|
-
}, deflateHandler);
|
|
1933
|
-
fastify.post("/deflate", {
|
|
1934
|
-
schema: deflateSchema
|
|
1935
|
-
}, deflateHandler);
|
|
1936
|
-
fastify.put("/deflate", {
|
|
1937
|
-
schema: deflateSchema
|
|
1938
|
-
}, deflateHandler);
|
|
1939
|
-
fastify.patch("/deflate", {
|
|
1940
|
-
schema: deflateSchema
|
|
1941
|
-
}, deflateHandler);
|
|
1942
|
-
fastify.delete("/deflate", {
|
|
1943
|
-
schema: deflateSchema
|
|
1944
|
-
}, deflateHandler);
|
|
1945
|
-
const brotliHandler = /* @__PURE__ */ __name(async (_request, reply) => {
|
|
1946
|
-
const sampleData = [
|
|
1947
|
-
{
|
|
1948
|
-
message: "This is Brotli compressed data from MockHTTP",
|
|
1949
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1950
|
-
compression: "br",
|
|
1951
|
-
algorithm: "Brotli",
|
|
1952
|
-
originalSize: 0,
|
|
1953
|
-
compressedSize: 0
|
|
1954
|
-
},
|
|
1955
|
-
{
|
|
1956
|
-
title: "Brotli Compression Example",
|
|
1957
|
-
description: "Brotli is a generic-purpose lossless compression algorithm that compresses data using a combination of a modern variant of the LZ77 algorithm, Huffman coding and 2nd order context modeling.",
|
|
1958
|
-
content: "Brotli compression typically achieves 20-26% better compression ratios than gzip. ".repeat(10),
|
|
1959
|
-
metadata: {
|
|
1960
|
-
encoding: "br",
|
|
1961
|
-
quality: 11,
|
|
1962
|
-
server: "MockHTTP"
|
|
1963
|
-
}
|
|
1964
|
-
},
|
|
1965
|
-
{
|
|
1966
|
-
benchmark: {
|
|
1967
|
-
title: "Compression Benchmark Data",
|
|
1968
|
-
results: Array.from({
|
|
1969
|
-
length: 75
|
|
1970
|
-
}, (_, i) => ({
|
|
1971
|
-
id: `test-${i}`,
|
|
1972
|
-
name: `Benchmark Test ${i}`,
|
|
1973
|
-
value: Math.random() * 1e3,
|
|
1974
|
-
unit: "ms",
|
|
1975
|
-
timestamp: new Date(Date.now() - i * 6e4).toISOString()
|
|
1976
|
-
})),
|
|
1977
|
-
summary: {
|
|
1978
|
-
compression: "brotli",
|
|
1979
|
-
totalTests: 75,
|
|
1980
|
-
encoding: "br"
|
|
1981
|
-
}
|
|
1982
|
-
}
|
|
1983
|
-
}
|
|
1984
|
-
];
|
|
1985
|
-
const randomIndex = Math.floor(Math.random() * sampleData.length);
|
|
1986
|
-
const data = sampleData[randomIndex];
|
|
1987
|
-
const jsonString = JSON.stringify(data);
|
|
1988
|
-
if (data.originalSize !== void 0) {
|
|
1989
|
-
data.originalSize = Buffer.byteLength(jsonString);
|
|
1990
|
-
}
|
|
1991
|
-
const compressed = brotliCompressSync(jsonString);
|
|
1992
|
-
if (data.compressedSize !== void 0) {
|
|
1993
|
-
const updatedData = {
|
|
1994
|
-
...data,
|
|
1995
|
-
compressedSize: compressed.length
|
|
1996
|
-
};
|
|
1997
|
-
const updatedJsonString = JSON.stringify(updatedData);
|
|
1998
|
-
const updatedCompressed = brotliCompressSync(updatedJsonString);
|
|
1999
|
-
reply.header("Content-Encoding", "br");
|
|
2000
|
-
reply.header("Content-Type", "application/json");
|
|
2001
|
-
reply.type("application/octet-stream");
|
|
2002
|
-
return updatedCompressed;
|
|
2003
|
-
}
|
|
2004
|
-
reply.header("Content-Encoding", "br");
|
|
2005
|
-
reply.header("Content-Type", "application/json");
|
|
2006
|
-
reply.type("application/octet-stream");
|
|
2007
|
-
return compressed;
|
|
2008
|
-
}, "brotliHandler");
|
|
2009
|
-
fastify.get("/brotli", {
|
|
2010
|
-
schema: brotliSchema
|
|
2011
|
-
}, brotliHandler);
|
|
2012
|
-
fastify.post("/brotli", {
|
|
2013
|
-
schema: brotliSchema
|
|
2014
|
-
}, brotliHandler);
|
|
2015
|
-
fastify.put("/brotli", {
|
|
2016
|
-
schema: brotliSchema
|
|
2017
|
-
}, brotliHandler);
|
|
2018
|
-
fastify.patch("/brotli", {
|
|
2019
|
-
schema: brotliSchema
|
|
2020
|
-
}, brotliHandler);
|
|
2021
|
-
fastify.delete("/brotli", {
|
|
2022
|
-
schema: brotliSchema
|
|
2023
|
-
}, brotliHandler);
|
|
2024
|
-
}, "responseFormatRoutes");
|
|
2025
|
-
|
|
2026
|
-
// src/routes/response-inspection/cache.ts
|
|
2027
|
-
var cacheGetSchema = {
|
|
2028
|
-
description: "Handles cache validation and retrieval.",
|
|
2029
|
-
tags: [
|
|
2030
|
-
"Response Inspection"
|
|
2031
|
-
],
|
|
2032
|
-
response: {
|
|
2033
|
-
200: {
|
|
2034
|
-
type: "string",
|
|
2035
|
-
description: "The cached content or resource."
|
|
2036
|
-
},
|
|
2037
|
-
304: {
|
|
2038
|
-
type: "null",
|
|
2039
|
-
description: "Indicates the resource has not been modified."
|
|
2040
|
-
}
|
|
2041
|
-
}
|
|
2042
|
-
};
|
|
2043
|
-
var cacheSetSchema = {
|
|
2044
|
-
description: "Sets a Cache-Control header for n seconds.",
|
|
2045
|
-
tags: [
|
|
2046
|
-
"Response Inspection"
|
|
2047
|
-
],
|
|
2048
|
-
params: {
|
|
2049
|
-
type: "object",
|
|
2050
|
-
properties: {
|
|
2051
|
-
value: {
|
|
2052
|
-
type: "integer",
|
|
2053
|
-
description: "Number of seconds to cache the resource."
|
|
2054
|
-
}
|
|
2055
|
-
},
|
|
2056
|
-
required: [
|
|
2057
|
-
"value"
|
|
2058
|
-
]
|
|
2059
|
-
},
|
|
2060
|
-
response: {
|
|
2061
|
-
200: {
|
|
2062
|
-
type: "string",
|
|
2063
|
-
description: "Confirmation message for setting cache."
|
|
2064
|
-
}
|
|
2065
|
-
}
|
|
2066
|
-
};
|
|
2067
|
-
var cacheRoutes = /* @__PURE__ */ __name((fastify) => {
|
|
2068
|
-
fastify.get("/cache", {
|
|
2069
|
-
schema: cacheGetSchema
|
|
2070
|
-
}, async (request, reply) => {
|
|
2071
|
-
const ifModifiedSince = request.headers["if-modified-since"];
|
|
2072
|
-
const ifNoneMatch = request.headers["if-none-match"];
|
|
2073
|
-
if (ifModifiedSince ?? ifNoneMatch) {
|
|
2074
|
-
await reply.status(304).send();
|
|
2075
|
-
return;
|
|
2076
|
-
}
|
|
2077
|
-
await reply.send("Cache content or resource response here.");
|
|
2078
|
-
});
|
|
2079
|
-
fastify.get("/cache/:value", {
|
|
2080
|
-
schema: cacheSetSchema
|
|
2081
|
-
}, async (request, reply) => {
|
|
2082
|
-
const { value } = request.params;
|
|
2083
|
-
await reply.header("Cache-Control", `max-age=${value}`).send(`Cache-Control set for ${value} seconds.`);
|
|
2084
|
-
});
|
|
2085
|
-
}, "cacheRoutes");
|
|
2086
|
-
|
|
2087
|
-
// src/routes/response-inspection/etag.ts
|
|
2088
|
-
var etagSchema = {
|
|
2089
|
-
description: "Handles ETag-based conditional requests.",
|
|
2090
|
-
tags: [
|
|
2091
|
-
"Response Inspection"
|
|
2092
|
-
],
|
|
2093
|
-
params: {
|
|
2094
|
-
type: "object",
|
|
2095
|
-
properties: {
|
|
2096
|
-
etag: {
|
|
2097
|
-
type: "string",
|
|
2098
|
-
description: "The ETag value for the resource."
|
|
2099
|
-
}
|
|
2100
|
-
},
|
|
2101
|
-
required: [
|
|
2102
|
-
"etag"
|
|
2103
|
-
]
|
|
2104
|
-
},
|
|
2105
|
-
response: {
|
|
2106
|
-
200: {
|
|
2107
|
-
type: "string",
|
|
2108
|
-
description: "Resource content for matching ETag."
|
|
2109
|
-
},
|
|
2110
|
-
304: {
|
|
2111
|
-
type: "null",
|
|
2112
|
-
description: "Indicates the resource has not been modified."
|
|
2113
|
-
},
|
|
2114
|
-
412: {
|
|
2115
|
-
type: "null",
|
|
2116
|
-
description: "Indicates a precondition failed due to mismatched ETag."
|
|
2117
|
-
}
|
|
2118
|
-
}
|
|
2119
|
-
};
|
|
2120
|
-
var etagRoutes = /* @__PURE__ */ __name((fastify) => {
|
|
2121
|
-
fastify.get("/etag/:etag", {
|
|
2122
|
-
schema: etagSchema
|
|
2123
|
-
}, async (request, reply) => {
|
|
2124
|
-
const { etag } = request.params;
|
|
2125
|
-
const ifNoneMatch = request.headers["if-none-match"];
|
|
2126
|
-
const ifMatch = request.headers["if-match"];
|
|
2127
|
-
if (ifNoneMatch === etag) {
|
|
2128
|
-
await reply.status(304).send();
|
|
2129
|
-
return;
|
|
2130
|
-
}
|
|
2131
|
-
if (ifMatch && ifMatch !== etag) {
|
|
2132
|
-
await reply.status(412).send();
|
|
2133
|
-
return;
|
|
2134
|
-
}
|
|
2135
|
-
await reply.header("ETag", etag).send("Resource content for matching ETag.");
|
|
2136
|
-
});
|
|
2137
|
-
}, "etagRoutes");
|
|
2138
|
-
|
|
2139
|
-
// src/routes/response-inspection/response-headers.ts
|
|
2140
|
-
import { escape as escapeHtml } from "html-escaper";
|
|
2141
|
-
var responseHeadersSchema = {
|
|
2142
|
-
description: "Returns a set of response headers based on the query string.",
|
|
2143
|
-
tags: [
|
|
2144
|
-
"Response Inspection"
|
|
2145
|
-
],
|
|
2146
|
-
querystring: {
|
|
2147
|
-
type: "object",
|
|
2148
|
-
additionalProperties: true,
|
|
2149
|
-
description: "Freeform query string parameters to be added as response headers."
|
|
2150
|
-
},
|
|
2151
|
-
response: {
|
|
2152
|
-
200: {
|
|
2153
|
-
type: "object",
|
|
2154
|
-
additionalProperties: true,
|
|
2155
|
-
description: "Response headers as per query string parameters."
|
|
2156
|
-
}
|
|
2157
|
-
}
|
|
2158
|
-
};
|
|
2159
|
-
var responseHeadersRoutes = /* @__PURE__ */ __name((fastify) => {
|
|
2160
|
-
fastify.get("/response-headers", {
|
|
2161
|
-
schema: responseHeadersSchema
|
|
2162
|
-
}, async (request, reply) => {
|
|
2163
|
-
const queryParameters = request.query;
|
|
2164
|
-
for (const [key, value] of Object.entries(queryParameters)) {
|
|
2165
|
-
reply.header(key, escapeHtml(value));
|
|
2166
|
-
}
|
|
2167
|
-
const cleanedResponse = Object.fromEntries(Object.entries(queryParameters).map(([key, value]) => [
|
|
2168
|
-
key,
|
|
2169
|
-
escapeHtml(value)
|
|
2170
|
-
]));
|
|
2171
|
-
await reply.send(cleanedResponse);
|
|
2172
|
-
});
|
|
2173
|
-
fastify.post("/response-headers", {
|
|
2174
|
-
schema: responseHeadersSchema
|
|
2175
|
-
}, async (request, reply) => {
|
|
2176
|
-
const queryParameters = request.query ?? {};
|
|
2177
|
-
const body = request.body ?? {};
|
|
2178
|
-
for (const [key, value] of Object.entries(queryParameters)) {
|
|
2179
|
-
reply.header(key, escapeHtml(value));
|
|
2180
|
-
}
|
|
2181
|
-
for (const [key, value] of Object.entries(body)) {
|
|
2182
|
-
reply.header(key, escapeHtml(value));
|
|
2183
|
-
}
|
|
2184
|
-
const cleanedResponse = Object.fromEntries(Object.entries({
|
|
2185
|
-
...queryParameters,
|
|
2186
|
-
...body
|
|
2187
|
-
}).map(([key, value]) => [
|
|
2188
|
-
key,
|
|
2189
|
-
escapeHtml(value)
|
|
2190
|
-
]));
|
|
2191
|
-
await reply.send(cleanedResponse);
|
|
2192
|
-
});
|
|
2193
|
-
}, "responseHeadersRoutes");
|
|
2194
|
-
|
|
2195
|
-
// src/routes/sitemap.ts
|
|
2196
|
-
var sitemapRoute = /* @__PURE__ */ __name((fastify) => {
|
|
2197
|
-
fastify.get("/sitemap.xml", {
|
|
2198
|
-
schema: {
|
|
2199
|
-
hide: true
|
|
2200
|
-
}
|
|
2201
|
-
}, async (_request, reply) => {
|
|
2202
|
-
try {
|
|
2203
|
-
const host = _request.headers.host ?? "mockhttp.org";
|
|
2204
|
-
const isSecure = _request.headers["x-forwarded-proto"]?.includes("https");
|
|
2205
|
-
const protocol = isSecure ? "https" : "http";
|
|
2206
|
-
const xml = `<?xml version="1.0" encoding="UTF-8"?>
|
|
235
|
+
</html>`],u=Math.floor(Math.random()*p.length),m=p[u];return c.code(403),c.type("text/html"),m},"denyHandler");s.get("/deny",{schema:_},n),s.post("/deny",{schema:_},n),s.put("/deny",{schema:_},n),s.patch("/deny",{schema:_},n),s.delete("/deny",{schema:_},n);let d=r(async(l,c)=>{let p=[{message:"This is GZip compressed data from MockHTTP",timestamp:new Date().toISOString(),compression:"gzip",originalSize:0,compressedSize:0},{title:"Sample Document",content:"Lorem ipsum dolor sit amet, consectetur adipiscing elit. ".repeat(50),metadata:{author:"MockHTTP Server",format:"gzip",version:"1.0"}},{data:Array.from({length:100},(R,f)=>({id:f+1,value:`Item ${f+1}`,description:`This is item number ${f+1} in the dataset`})),info:"Large dataset compressed with GZip"}],u=Math.floor(Math.random()*p.length),m=p[u],y=JSON.stringify(m);m.originalSize!==void 0&&(m.originalSize=Buffer.byteLength(y));let b=be(y);if(m.compressedSize!==void 0){let R={...m,compressedSize:b.length},f=JSON.stringify(R),v=be(f);return c.header("Content-Encoding","gzip"),c.header("Content-Type","application/json"),c.type("application/octet-stream"),v}return c.header("Content-Encoding","gzip"),c.header("Content-Type","application/json"),c.type("application/octet-stream"),b},"gzipHandler");s.get("/gzip",{schema:H},d),s.post("/gzip",{schema:H},d),s.put("/gzip",{schema:H},d),s.patch("/gzip",{schema:H},d),s.delete("/gzip",{schema:H},d);let g=r(async(l,c)=>{let p=[{message:"This is Deflate compressed data from MockHTTP",timestamp:new Date().toISOString(),compression:"deflate",originalSize:0,compressedSize:0},{title:"Deflate Compressed Document",content:"The deflate algorithm is used for compression. ".repeat(40),details:{algorithm:"DEFLATE",method:"zlib deflate",server:"MockHTTP"}},{items:Array.from({length:50},(R,f)=>({index:f,name:`Entry ${f}`,data:`This is test data for entry number ${f}`,timestamp:new Date(Date.now()-f*1e6).toISOString()})),metadata:{compression:"deflate",count:50}}],u=Math.floor(Math.random()*p.length),m=p[u],y=JSON.stringify(m);m.originalSize!==void 0&&(m.originalSize=Buffer.byteLength(y));let b=ye(y);if(m.compressedSize!==void 0){let R={...m,compressedSize:b.length},f=JSON.stringify(R),v=ye(f);return c.header("Content-Encoding","deflate"),c.header("Content-Type","application/json"),c.type("application/octet-stream"),v}return c.header("Content-Encoding","deflate"),c.header("Content-Type","application/json"),c.type("application/octet-stream"),b},"deflateHandler");s.get("/deflate",{schema:$},g),s.post("/deflate",{schema:$},g),s.put("/deflate",{schema:$},g),s.patch("/deflate",{schema:$},g),s.delete("/deflate",{schema:$},g);let h=r(async(l,c)=>{let p=[{message:"This is Brotli compressed data from MockHTTP",timestamp:new Date().toISOString(),compression:"br",algorithm:"Brotli",originalSize:0,compressedSize:0},{title:"Brotli Compression Example",description:"Brotli is a generic-purpose lossless compression algorithm that compresses data using a combination of a modern variant of the LZ77 algorithm, Huffman coding and 2nd order context modeling.",content:"Brotli compression typically achieves 20-26% better compression ratios than gzip. ".repeat(10),metadata:{encoding:"br",quality:11,server:"MockHTTP"}},{benchmark:{title:"Compression Benchmark Data",results:Array.from({length:75},(R,f)=>({id:`test-${f}`,name:`Benchmark Test ${f}`,value:Math.random()*1e3,unit:"ms",timestamp:new Date(Date.now()-f*6e4).toISOString()})),summary:{compression:"brotli",totalTests:75,encoding:"br"}}}],u=Math.floor(Math.random()*p.length),m=p[u],y=JSON.stringify(m);m.originalSize!==void 0&&(m.originalSize=Buffer.byteLength(y));let b=fe(y);if(m.compressedSize!==void 0){let R={...m,compressedSize:b.length},f=JSON.stringify(R),v=fe(f);return c.header("Content-Encoding","br"),c.header("Content-Type","application/json"),c.type("application/octet-stream"),v}return c.header("Content-Encoding","br"),c.header("Content-Type","application/json"),c.type("application/octet-stream"),b},"brotliHandler");s.get("/brotli",{schema:D},h),s.post("/brotli",{schema:D},h),s.put("/brotli",{schema:D},h),s.patch("/brotli",{schema:D},h),s.delete("/brotli",{schema:D},h)},"responseFormatRoutes");var st={description:"Handles cache validation and retrieval.",tags:["Response Inspection"],response:{200:{type:"string",description:"The cached content or resource."},304:{type:"null",description:"Indicates the resource has not been modified."}}},ot={description:"Sets a Cache-Control header for n seconds.",tags:["Response Inspection"],params:{type:"object",properties:{value:{type:"integer",description:"Number of seconds to cache the resource."}},required:["value"]},response:{200:{type:"string",description:"Confirmation message for setting cache."}}},re=r(s=>{s.get("/cache",{schema:st},async(e,t)=>{let o=e.headers["if-modified-since"],a=e.headers["if-none-match"];if(o??a){await t.status(304).send();return}await t.send("Cache content or resource response here.")}),s.get("/cache/:value",{schema:ot},async(e,t)=>{let{value:o}=e.params;await t.header("Cache-Control",`max-age=${o}`).send(`Cache-Control set for ${o} seconds.`)})},"cacheRoutes");var rt={description:"Handles ETag-based conditional requests.",tags:["Response Inspection"],params:{type:"object",properties:{etag:{type:"string",description:"The ETag value for the resource."}},required:["etag"]},response:{200:{type:"string",description:"Resource content for matching ETag."},304:{type:"null",description:"Indicates the resource has not been modified."},412:{type:"null",description:"Indicates a precondition failed due to mismatched ETag."}}},ae=r(s=>{s.get("/etag/:etag",{schema:rt},async(e,t)=>{let{etag:o}=e.params,a=e.headers["if-none-match"],i=e.headers["if-match"];if(a===o){await t.status(304).send();return}if(i&&i!==o){await t.status(412).send();return}await t.header("ETag",o).send("Resource content for matching ETag.")})},"etagRoutes");import{escape as M}from"html-escaper";var ve={description:"Returns a set of response headers based on the query string.",tags:["Response Inspection"],querystring:{type:"object",additionalProperties:!0,description:"Freeform query string parameters to be added as response headers."},response:{200:{type:"object",additionalProperties:!0,description:"Response headers as per query string parameters."}}},ie=r(s=>{s.get("/response-headers",{schema:ve},async(e,t)=>{let o=e.query;for(let[i,n]of Object.entries(o))t.header(i,M(n));let a=Object.fromEntries(Object.entries(o).map(([i,n])=>[i,M(n)]));await t.send(a)}),s.post("/response-headers",{schema:ve},async(e,t)=>{let o=e.query??{},a=e.body??{};for(let[n,d]of Object.entries(o))t.header(n,M(d));for(let[n,d]of Object.entries(a))t.header(n,M(d));let i=Object.fromEntries(Object.entries({...o,...a}).map(([n,d])=>[n,M(d)]));await t.send(i)})},"responseHeadersRoutes");var Te=r(s=>{s.get("/sitemap.xml",{schema:{hide:!0}},async(e,t)=>{try{let o=e.headers.host??"mockhttp.org",n=`<?xml version="1.0" encoding="UTF-8"?>
|
|
2207
236
|
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
|
2208
237
|
<url>
|
|
2209
|
-
<loc>${
|
|
238
|
+
<loc>${e.headers["x-forwarded-proto"]?.includes("https")?"https":"http"}://${o}/</loc>
|
|
2210
239
|
<changefreq>monthly</changefreq>
|
|
2211
240
|
<priority>1.0</priority>
|
|
2212
241
|
</url>
|
|
2213
|
-
</urlset>`;
|
|
2214
|
-
await reply.type("text/xml").send(xml);
|
|
2215
|
-
} catch (error) {
|
|
2216
|
-
fastify.log.error(error);
|
|
2217
|
-
await reply.status(500).send({
|
|
2218
|
-
error: "Internal Server Error"
|
|
2219
|
-
});
|
|
2220
|
-
}
|
|
2221
|
-
});
|
|
2222
|
-
}, "sitemapRoute");
|
|
2223
|
-
|
|
2224
|
-
// src/routes/status-codes/index.ts
|
|
2225
|
-
import { randomInt } from "crypto";
|
|
2226
|
-
var statusCodes = [
|
|
2227
|
-
{
|
|
2228
|
-
code: 100,
|
|
2229
|
-
message: "Continue"
|
|
2230
|
-
},
|
|
2231
|
-
{
|
|
2232
|
-
code: 101,
|
|
2233
|
-
message: "Switching Protocols"
|
|
2234
|
-
},
|
|
2235
|
-
{
|
|
2236
|
-
code: 102,
|
|
2237
|
-
message: "Processing"
|
|
2238
|
-
},
|
|
2239
|
-
{
|
|
2240
|
-
code: 103,
|
|
2241
|
-
message: "Early Hints"
|
|
2242
|
-
},
|
|
2243
|
-
{
|
|
2244
|
-
code: 200,
|
|
2245
|
-
message: "OK"
|
|
2246
|
-
},
|
|
2247
|
-
{
|
|
2248
|
-
code: 201,
|
|
2249
|
-
message: "Created"
|
|
2250
|
-
},
|
|
2251
|
-
{
|
|
2252
|
-
code: 202,
|
|
2253
|
-
message: "Accepted"
|
|
2254
|
-
},
|
|
2255
|
-
{
|
|
2256
|
-
code: 203,
|
|
2257
|
-
message: "Non-Authoritative Information"
|
|
2258
|
-
},
|
|
2259
|
-
{
|
|
2260
|
-
code: 204,
|
|
2261
|
-
message: "No Content"
|
|
2262
|
-
},
|
|
2263
|
-
{
|
|
2264
|
-
code: 205,
|
|
2265
|
-
message: "Reset Content"
|
|
2266
|
-
},
|
|
2267
|
-
{
|
|
2268
|
-
code: 206,
|
|
2269
|
-
message: "Partial Content"
|
|
2270
|
-
},
|
|
2271
|
-
{
|
|
2272
|
-
code: 207,
|
|
2273
|
-
message: "Multi-Status"
|
|
2274
|
-
},
|
|
2275
|
-
{
|
|
2276
|
-
code: 208,
|
|
2277
|
-
message: "Already Reported"
|
|
2278
|
-
},
|
|
2279
|
-
{
|
|
2280
|
-
code: 226,
|
|
2281
|
-
message: "IM Used"
|
|
2282
|
-
},
|
|
2283
|
-
{
|
|
2284
|
-
code: 300,
|
|
2285
|
-
message: "Multiple Choices"
|
|
2286
|
-
},
|
|
2287
|
-
{
|
|
2288
|
-
code: 301,
|
|
2289
|
-
message: "Moved Permanently"
|
|
2290
|
-
},
|
|
2291
|
-
{
|
|
2292
|
-
code: 302,
|
|
2293
|
-
message: "Found"
|
|
2294
|
-
},
|
|
2295
|
-
{
|
|
2296
|
-
code: 303,
|
|
2297
|
-
message: "See Other"
|
|
2298
|
-
},
|
|
2299
|
-
{
|
|
2300
|
-
code: 304,
|
|
2301
|
-
message: "Not Modified"
|
|
2302
|
-
},
|
|
2303
|
-
{
|
|
2304
|
-
code: 305,
|
|
2305
|
-
message: "Use Proxy"
|
|
2306
|
-
},
|
|
2307
|
-
{
|
|
2308
|
-
code: 306,
|
|
2309
|
-
message: "Switch Proxy"
|
|
2310
|
-
},
|
|
2311
|
-
{
|
|
2312
|
-
code: 307,
|
|
2313
|
-
message: "Temporary Redirect"
|
|
2314
|
-
},
|
|
2315
|
-
{
|
|
2316
|
-
code: 308,
|
|
2317
|
-
message: "Permanent Redirect"
|
|
2318
|
-
},
|
|
2319
|
-
{
|
|
2320
|
-
code: 400,
|
|
2321
|
-
message: "Bad Request"
|
|
2322
|
-
},
|
|
2323
|
-
{
|
|
2324
|
-
code: 401,
|
|
2325
|
-
message: "Unauthorized"
|
|
2326
|
-
},
|
|
2327
|
-
{
|
|
2328
|
-
code: 402,
|
|
2329
|
-
message: "Payment Required"
|
|
2330
|
-
},
|
|
2331
|
-
{
|
|
2332
|
-
code: 403,
|
|
2333
|
-
message: "Forbidden"
|
|
2334
|
-
},
|
|
2335
|
-
{
|
|
2336
|
-
code: 404,
|
|
2337
|
-
message: "Not Found"
|
|
2338
|
-
},
|
|
2339
|
-
{
|
|
2340
|
-
code: 405,
|
|
2341
|
-
message: "Method Not Allowed"
|
|
2342
|
-
},
|
|
2343
|
-
{
|
|
2344
|
-
code: 406,
|
|
2345
|
-
message: "Not Acceptable"
|
|
2346
|
-
},
|
|
2347
|
-
{
|
|
2348
|
-
code: 407,
|
|
2349
|
-
message: "Proxy Authentication Required"
|
|
2350
|
-
},
|
|
2351
|
-
{
|
|
2352
|
-
code: 408,
|
|
2353
|
-
message: "Request Timeout"
|
|
2354
|
-
},
|
|
2355
|
-
{
|
|
2356
|
-
code: 409,
|
|
2357
|
-
message: "Conflict"
|
|
2358
|
-
},
|
|
2359
|
-
{
|
|
2360
|
-
code: 410,
|
|
2361
|
-
message: "Gone"
|
|
2362
|
-
},
|
|
2363
|
-
{
|
|
2364
|
-
code: 411,
|
|
2365
|
-
message: "Length Required"
|
|
2366
|
-
},
|
|
2367
|
-
{
|
|
2368
|
-
code: 412,
|
|
2369
|
-
message: "Precondition Failed"
|
|
2370
|
-
},
|
|
2371
|
-
{
|
|
2372
|
-
code: 413,
|
|
2373
|
-
message: "Payload Too Large"
|
|
2374
|
-
},
|
|
2375
|
-
{
|
|
2376
|
-
code: 414,
|
|
2377
|
-
message: "URI Too Long"
|
|
2378
|
-
},
|
|
2379
|
-
{
|
|
2380
|
-
code: 415,
|
|
2381
|
-
message: "Unsupported Media Type"
|
|
2382
|
-
},
|
|
2383
|
-
{
|
|
2384
|
-
code: 416,
|
|
2385
|
-
message: "Range Not Satisfiable"
|
|
2386
|
-
},
|
|
2387
|
-
{
|
|
2388
|
-
code: 417,
|
|
2389
|
-
message: "Expectation Failed"
|
|
2390
|
-
},
|
|
2391
|
-
{
|
|
2392
|
-
code: 418,
|
|
2393
|
-
message: "I'm a teapot"
|
|
2394
|
-
},
|
|
2395
|
-
{
|
|
2396
|
-
code: 421,
|
|
2397
|
-
message: "Misdirected Request"
|
|
2398
|
-
},
|
|
2399
|
-
{
|
|
2400
|
-
code: 422,
|
|
2401
|
-
message: "Unprocessable Entity"
|
|
2402
|
-
},
|
|
2403
|
-
{
|
|
2404
|
-
code: 423,
|
|
2405
|
-
message: "Locked"
|
|
2406
|
-
},
|
|
2407
|
-
{
|
|
2408
|
-
code: 424,
|
|
2409
|
-
message: "Failed Dependency"
|
|
2410
|
-
},
|
|
2411
|
-
{
|
|
2412
|
-
code: 425,
|
|
2413
|
-
message: "Too Early"
|
|
2414
|
-
},
|
|
2415
|
-
{
|
|
2416
|
-
code: 426,
|
|
2417
|
-
message: "Upgrade Required"
|
|
2418
|
-
},
|
|
2419
|
-
{
|
|
2420
|
-
code: 428,
|
|
2421
|
-
message: "Precondition Required"
|
|
2422
|
-
},
|
|
2423
|
-
{
|
|
2424
|
-
code: 429,
|
|
2425
|
-
message: "Too Many Requests"
|
|
2426
|
-
},
|
|
2427
|
-
{
|
|
2428
|
-
code: 431,
|
|
2429
|
-
message: "Request Header Fields Too Large"
|
|
2430
|
-
},
|
|
2431
|
-
{
|
|
2432
|
-
code: 451,
|
|
2433
|
-
message: "Unavailable For Legal Reasons"
|
|
2434
|
-
},
|
|
2435
|
-
{
|
|
2436
|
-
code: 500,
|
|
2437
|
-
message: "Internal Server Error"
|
|
2438
|
-
},
|
|
2439
|
-
{
|
|
2440
|
-
code: 501,
|
|
2441
|
-
message: "Not Implemented"
|
|
2442
|
-
},
|
|
2443
|
-
{
|
|
2444
|
-
code: 502,
|
|
2445
|
-
message: "Bad Gateway"
|
|
2446
|
-
},
|
|
2447
|
-
{
|
|
2448
|
-
code: 503,
|
|
2449
|
-
message: "Service Unavailable"
|
|
2450
|
-
},
|
|
2451
|
-
{
|
|
2452
|
-
code: 504,
|
|
2453
|
-
message: "Gateway Timeout"
|
|
2454
|
-
},
|
|
2455
|
-
{
|
|
2456
|
-
code: 505,
|
|
2457
|
-
message: "HTTP Version Not Supported"
|
|
2458
|
-
},
|
|
2459
|
-
{
|
|
2460
|
-
code: 506,
|
|
2461
|
-
message: "Variant Also Negotiates"
|
|
2462
|
-
},
|
|
2463
|
-
{
|
|
2464
|
-
code: 507,
|
|
2465
|
-
message: "Insufficient Storage"
|
|
2466
|
-
},
|
|
2467
|
-
{
|
|
2468
|
-
code: 508,
|
|
2469
|
-
message: "Loop Detected"
|
|
2470
|
-
},
|
|
2471
|
-
{
|
|
2472
|
-
code: 510,
|
|
2473
|
-
message: "Not Extended"
|
|
2474
|
-
},
|
|
2475
|
-
{
|
|
2476
|
-
code: 511,
|
|
2477
|
-
message: "Network Authentication Required"
|
|
2478
|
-
}
|
|
2479
|
-
];
|
|
2480
|
-
var statusSchema = {
|
|
2481
|
-
description: "Return status code or random status code if more than one are given ",
|
|
2482
|
-
tags: [
|
|
2483
|
-
"Status Codes"
|
|
2484
|
-
],
|
|
2485
|
-
params: {
|
|
2486
|
-
type: "object",
|
|
2487
|
-
properties: {
|
|
2488
|
-
code: {
|
|
2489
|
-
type: "string",
|
|
2490
|
-
description: "HTTP status code to be returned"
|
|
2491
|
-
}
|
|
2492
|
-
}
|
|
2493
|
-
},
|
|
2494
|
-
response: {
|
|
2495
|
-
200: {
|
|
2496
|
-
type: "object",
|
|
2497
|
-
properties: {
|
|
2498
|
-
status: {
|
|
2499
|
-
type: "string"
|
|
2500
|
-
}
|
|
2501
|
-
}
|
|
2502
|
-
}
|
|
2503
|
-
}
|
|
2504
|
-
};
|
|
2505
|
-
var statusCodeRoute = /* @__PURE__ */ __name((fastify) => {
|
|
2506
|
-
const handler = /* @__PURE__ */ __name(async (request, reply) => {
|
|
2507
|
-
let { code } = request.params;
|
|
2508
|
-
if (!code || !statusCodes.map((status) => status.code).includes(Number(code))) {
|
|
2509
|
-
code = statusCodes[randomInt(0, statusCodes.length)].code.toString();
|
|
2510
|
-
}
|
|
2511
|
-
await reply.code(Number(code)).send({
|
|
2512
|
-
status: `Response with status code ${code}`
|
|
2513
|
-
});
|
|
2514
|
-
}, "handler");
|
|
2515
|
-
fastify.get("/status/:code?", {
|
|
2516
|
-
schema: statusSchema
|
|
2517
|
-
}, handler);
|
|
2518
|
-
fastify.post("/status/:code?", {
|
|
2519
|
-
schema: statusSchema
|
|
2520
|
-
}, handler);
|
|
2521
|
-
fastify.put("/status/:code?", {
|
|
2522
|
-
schema: statusSchema
|
|
2523
|
-
}, handler);
|
|
2524
|
-
fastify.patch("/status/:code?", {
|
|
2525
|
-
schema: statusSchema
|
|
2526
|
-
}, handler);
|
|
2527
|
-
fastify.delete("/status/:code?", {
|
|
2528
|
-
schema: statusSchema
|
|
2529
|
-
}, handler);
|
|
2530
|
-
}, "statusCodeRoute");
|
|
2531
|
-
|
|
2532
|
-
// src/swagger.ts
|
|
2533
|
-
import { fastifySwaggerUi } from "@fastify/swagger-ui";
|
|
2534
|
-
|
|
2535
|
-
// package.json
|
|
2536
|
-
var package_default = {
|
|
2537
|
-
name: "@jaredwray/mockhttp",
|
|
2538
|
-
version: "0.9.0",
|
|
2539
|
-
description: "Mock Http - Easy way to mock http with httpbin replacement",
|
|
2540
|
-
type: "module",
|
|
2541
|
-
main: "dist/index.js",
|
|
2542
|
-
directories: {
|
|
2543
|
-
test: "test"
|
|
2544
|
-
},
|
|
2545
|
-
scripts: {
|
|
2546
|
-
lint: "biome check --write --error-on-warnings",
|
|
2547
|
-
test: "pnpm lint && vitest run --coverage",
|
|
2548
|
-
"test:ci": "biome check --error-on-warnings && vitest run --coverage",
|
|
2549
|
-
dev: "pnpm lint && tsx src/index.ts --watch",
|
|
2550
|
-
build: "rimraf ./dist && tsup src/index.ts --format esm --dts --clean",
|
|
2551
|
-
"docker:build": "docker build -t jaredwray/mockhttp .",
|
|
2552
|
-
"docker:run": "docker run -p 3000:3000 jaredwray/mockhttp",
|
|
2553
|
-
"docker:push": "docker push jaredwray/mockhttp",
|
|
2554
|
-
"docker:stop": 'docker stop $(docker ps -a -q --filter ancestor=jaredwray/mockhttp --format="{{.ID}}")',
|
|
2555
|
-
"docker:readme": "tsx ./scripts/create-docker-readme.ts",
|
|
2556
|
-
"docker:compose:up": "docker-compose up -d",
|
|
2557
|
-
"docker:compose:down": "docker-compose down",
|
|
2558
|
-
prepare: "pnpm run build",
|
|
2559
|
-
start: "node dist/index.js"
|
|
2560
|
-
},
|
|
2561
|
-
repository: {
|
|
2562
|
-
type: "git",
|
|
2563
|
-
url: "git+https://github.com/jaredwray/mockhttp.git"
|
|
2564
|
-
},
|
|
2565
|
-
keywords: [
|
|
2566
|
-
"mock",
|
|
2567
|
-
"http",
|
|
2568
|
-
"httpbin",
|
|
2569
|
-
"test"
|
|
2570
|
-
],
|
|
2571
|
-
author: "Jared Wray <me@jaredwray.com>",
|
|
2572
|
-
license: "MIT",
|
|
2573
|
-
bugs: {
|
|
2574
|
-
url: "https://github.com/jaredwray/mockhttp/issues"
|
|
2575
|
-
},
|
|
2576
|
-
homepage: "https://github.com/jaredwray/mockhttp#readme",
|
|
2577
|
-
devDependencies: {
|
|
2578
|
-
"@biomejs/biome": "^2.2.2",
|
|
2579
|
-
"@swc/core": "^1.13.5",
|
|
2580
|
-
"@types/html-escaper": "^3.0.4",
|
|
2581
|
-
"@vitest/coverage-v8": "^3.2.4",
|
|
2582
|
-
rimraf: "^6.0.1",
|
|
2583
|
-
tsup: "^8.5.0",
|
|
2584
|
-
tsx: "^4.20.5",
|
|
2585
|
-
typescript: "^5.9.2",
|
|
2586
|
-
vitest: "^3.2.4"
|
|
2587
|
-
},
|
|
2588
|
-
dependencies: {
|
|
2589
|
-
"@fastify/cookie": "^11.0.2",
|
|
2590
|
-
"@fastify/helmet": "^13.0.1",
|
|
2591
|
-
"@fastify/static": "^8.2.0",
|
|
2592
|
-
"@fastify/swagger": "^9.5.1",
|
|
2593
|
-
"@fastify/swagger-ui": "^5.2.3",
|
|
2594
|
-
"@scalar/api-reference": "^1.34.6",
|
|
2595
|
-
"detect-port": "^2.1.0",
|
|
2596
|
-
fastify: "^5.5.0",
|
|
2597
|
-
hookified: "^1.12.0",
|
|
2598
|
-
"html-escaper": "^3.0.3",
|
|
2599
|
-
pino: "^9.9.0",
|
|
2600
|
-
"pino-pretty": "^13.1.1"
|
|
2601
|
-
},
|
|
2602
|
-
files: [
|
|
2603
|
-
"dist",
|
|
2604
|
-
"public",
|
|
2605
|
-
"LICENCE"
|
|
2606
|
-
],
|
|
2607
|
-
pnpm: {
|
|
2608
|
-
onlyBuiltDependencies: [
|
|
2609
|
-
"@swc/core",
|
|
2610
|
-
"unrs-resolver",
|
|
2611
|
-
"vue-demi"
|
|
2612
|
-
]
|
|
2613
|
-
}
|
|
2614
|
-
};
|
|
2615
|
-
|
|
2616
|
-
// src/swagger.ts
|
|
2617
|
-
var description = `
|
|
242
|
+
</urlset>`;await t.type("text/xml").send(n)}catch(o){s.log.error(o),await t.status(500).send({error:"Internal Server Error"})}})},"sitemapRoute");import{randomInt as at}from"crypto";var ne=[{code:100,message:"Continue"},{code:101,message:"Switching Protocols"},{code:102,message:"Processing"},{code:103,message:"Early Hints"},{code:200,message:"OK"},{code:201,message:"Created"},{code:202,message:"Accepted"},{code:203,message:"Non-Authoritative Information"},{code:204,message:"No Content"},{code:205,message:"Reset Content"},{code:206,message:"Partial Content"},{code:207,message:"Multi-Status"},{code:208,message:"Already Reported"},{code:226,message:"IM Used"},{code:300,message:"Multiple Choices"},{code:301,message:"Moved Permanently"},{code:302,message:"Found"},{code:303,message:"See Other"},{code:304,message:"Not Modified"},{code:305,message:"Use Proxy"},{code:306,message:"Switch Proxy"},{code:307,message:"Temporary Redirect"},{code:308,message:"Permanent Redirect"},{code:400,message:"Bad Request"},{code:401,message:"Unauthorized"},{code:402,message:"Payment Required"},{code:403,message:"Forbidden"},{code:404,message:"Not Found"},{code:405,message:"Method Not Allowed"},{code:406,message:"Not Acceptable"},{code:407,message:"Proxy Authentication Required"},{code:408,message:"Request Timeout"},{code:409,message:"Conflict"},{code:410,message:"Gone"},{code:411,message:"Length Required"},{code:412,message:"Precondition Failed"},{code:413,message:"Payload Too Large"},{code:414,message:"URI Too Long"},{code:415,message:"Unsupported Media Type"},{code:416,message:"Range Not Satisfiable"},{code:417,message:"Expectation Failed"},{code:418,message:"I'm a teapot"},{code:421,message:"Misdirected Request"},{code:422,message:"Unprocessable Entity"},{code:423,message:"Locked"},{code:424,message:"Failed Dependency"},{code:425,message:"Too Early"},{code:426,message:"Upgrade Required"},{code:428,message:"Precondition Required"},{code:429,message:"Too Many Requests"},{code:431,message:"Request Header Fields Too Large"},{code:451,message:"Unavailable For Legal Reasons"},{code:500,message:"Internal Server Error"},{code:501,message:"Not Implemented"},{code:502,message:"Bad Gateway"},{code:503,message:"Service Unavailable"},{code:504,message:"Gateway Timeout"},{code:505,message:"HTTP Version Not Supported"},{code:506,message:"Variant Also Negotiates"},{code:507,message:"Insufficient Storage"},{code:508,message:"Loop Detected"},{code:510,message:"Not Extended"},{code:511,message:"Network Authentication Required"}],A={description:"Return status code or random status code if more than one are given ",tags:["Status Codes"],params:{type:"object",properties:{code:{type:"string",description:"HTTP status code to be returned"}}},response:{200:{type:"object",properties:{status:{type:"string"}}}}},Se=r(s=>{let e=r(async(t,o)=>{let{code:a}=t.params;(!a||!ne.map(i=>i.code).includes(Number(a)))&&(a=ne[at(0,ne.length)].code.toString()),await o.code(Number(a)).send({status:`Response with status code ${a}`})},"handler");s.get("/status/:code?",{schema:A},e),s.post("/status/:code?",{schema:A},e),s.put("/status/:code?",{schema:A},e),s.patch("/status/:code?",{schema:A},e),s.delete("/status/:code?",{schema:A},e)},"statusCodeRoute");import{fastifySwaggerUi as nt}from"@fastify/swagger-ui";var je={name:"@jaredwray/mockhttp",version:"1.0.1",description:"Mock Http - Easy way to mock http with httpbin replacement",type:"module",main:"dist/index.js",directories:{test:"test"},scripts:{lint:"biome check --write --error-on-warnings",test:"pnpm lint && vitest run --coverage","test:ci":"biome check --error-on-warnings && vitest run --coverage",dev:"pnpm lint && tsx src/index.ts --watch",build:"rimraf ./dist && tsup src/index.ts --format esm --dts --clean --minify","docker:build":"docker build -t jaredwray/mockhttp .","docker:run":"docker run -p 3000:3000 jaredwray/mockhttp","docker:push":"docker push jaredwray/mockhttp","docker:stop":'docker stop $(docker ps -a -q --filter ancestor=jaredwray/mockhttp --format="{{.ID}}")',"docker:readme":"tsx ./scripts/create-docker-readme.ts","docker:compose:up":"docker-compose up -d","docker:compose:down":"docker-compose down",prepare:"pnpm run build",start:"node dist/index.js"},repository:{type:"git",url:"git+https://github.com/jaredwray/mockhttp.git"},keywords:["mock","http","httpbin","test"],author:"Jared Wray <me@jaredwray.com>",license:"MIT",bugs:{url:"https://github.com/jaredwray/mockhttp/issues"},homepage:"https://github.com/jaredwray/mockhttp#readme",devDependencies:{"@biomejs/biome":"^2.2.5","@swc/core":"^1.13.5","@types/html-escaper":"^3.0.4","@vitest/coverage-v8":"^3.2.4",rimraf:"^6.0.1",tsup:"^8.5.0",tsx:"^4.20.6",typescript:"^5.9.3",vitest:"^3.2.4"},dependencies:{"@fastify/cookie":"^11.0.2","@fastify/helmet":"^13.0.2","@fastify/static":"^8.2.0","@fastify/swagger":"^9.5.2","@fastify/swagger-ui":"^5.2.3","@scalar/api-reference":"^1.37.0","detect-port":"^2.1.0",fastify:"^5.6.1",hookified:"^1.12.1","html-escaper":"^3.0.3",pino:"^10.0.0","pino-pretty":"^13.1.1"},files:["dist","public","LICENCE"],pnpm:{onlyBuiltDependencies:["@swc/core","unrs-resolver","vue-demi"]}};var ct=`
|
|
2618
243
|
A simple HTTP server that can be used to mock HTTP responses for testing purposes. Inspired by [httpbin](https://httpbin.org/) and built using \`nodejs\` and \`fastify\` with the idea of running it via https://mockhttp.org, via docker \`jaredwray/mockhttp\`, or nodejs \`npm install jaredwray/mockhttp\`.
|
|
2619
244
|
|
|
2620
245
|
* [GitHub Repository](https://github.com/jaredwray/mockhttp)
|
|
@@ -2624,372 +249,4 @@ A simple HTTP server that can be used to mock HTTP responses for testing purpose
|
|
|
2624
249
|
# About mockhttp.org
|
|
2625
250
|
|
|
2626
251
|
[mockhttp.org](https://mockhttp.org) is a free service that runs this codebase and allows you to use it for testing purposes. It is a simple way to mock HTTP responses for testing purposes. Ran via [Cloudflare](https://cloudflare.com) and [Google Cloud Run](https://cloud.google.com/run/) across 7 regions globally and can do millions of requests per second.
|
|
2627
|
-
|
|
2628
|
-
var fastifySwaggerConfig = {
|
|
2629
|
-
openapi: {
|
|
2630
|
-
info: {
|
|
2631
|
-
title: "Mock HTTP API",
|
|
2632
|
-
description,
|
|
2633
|
-
version: package_default.version
|
|
2634
|
-
},
|
|
2635
|
-
consumes: [
|
|
2636
|
-
"application/json"
|
|
2637
|
-
],
|
|
2638
|
-
produces: [
|
|
2639
|
-
"application/json"
|
|
2640
|
-
]
|
|
2641
|
-
}
|
|
2642
|
-
};
|
|
2643
|
-
var registerSwaggerUi = /* @__PURE__ */ __name(async (fastify) => {
|
|
2644
|
-
await fastify.register(fastifySwaggerUi, {
|
|
2645
|
-
routePrefix: "/docs",
|
|
2646
|
-
uiConfig: {
|
|
2647
|
-
docExpansion: "none",
|
|
2648
|
-
deepLinking: false
|
|
2649
|
-
},
|
|
2650
|
-
uiHooks: {
|
|
2651
|
-
/* c8 ignore next 6 */
|
|
2652
|
-
onRequest(_request, _reply, next) {
|
|
2653
|
-
next();
|
|
2654
|
-
},
|
|
2655
|
-
preHandler(_request, _reply, next) {
|
|
2656
|
-
next();
|
|
2657
|
-
}
|
|
2658
|
-
},
|
|
2659
|
-
staticCSP: true,
|
|
2660
|
-
transformSpecification: /* @__PURE__ */ __name((swaggerObject, _request, _reply) => swaggerObject, "transformSpecification"),
|
|
2661
|
-
transformSpecificationClone: true
|
|
2662
|
-
});
|
|
2663
|
-
}, "registerSwaggerUi");
|
|
2664
|
-
|
|
2665
|
-
// src/mock-http.ts
|
|
2666
|
-
var MockHttp = class extends Hookified {
|
|
2667
|
-
static {
|
|
2668
|
-
__name(this, "MockHttp");
|
|
2669
|
-
}
|
|
2670
|
-
_port = 3e3;
|
|
2671
|
-
_host = "0.0.0.0";
|
|
2672
|
-
_autoDetectPort = true;
|
|
2673
|
-
_helmet = true;
|
|
2674
|
-
_apiDocs = true;
|
|
2675
|
-
_httpBin = {
|
|
2676
|
-
httpMethods: true,
|
|
2677
|
-
redirects: true,
|
|
2678
|
-
requestInspection: true,
|
|
2679
|
-
responseInspection: true,
|
|
2680
|
-
responseFormats: true,
|
|
2681
|
-
statusCodes: true,
|
|
2682
|
-
cookies: true,
|
|
2683
|
-
anything: true,
|
|
2684
|
-
auth: true
|
|
2685
|
-
};
|
|
2686
|
-
_server = Fastify();
|
|
2687
|
-
constructor(options) {
|
|
2688
|
-
super(options?.hookOptions);
|
|
2689
|
-
if (options?.port) {
|
|
2690
|
-
this._port = options.port;
|
|
2691
|
-
}
|
|
2692
|
-
if (options?.host) {
|
|
2693
|
-
this._host = options.host;
|
|
2694
|
-
}
|
|
2695
|
-
if (options?.helmet !== void 0) {
|
|
2696
|
-
this._helmet = options.helmet;
|
|
2697
|
-
}
|
|
2698
|
-
if (options?.apiDocs !== void 0) {
|
|
2699
|
-
this._apiDocs = options.apiDocs;
|
|
2700
|
-
}
|
|
2701
|
-
if (options?.httpBin !== void 0) {
|
|
2702
|
-
this._httpBin = options.httpBin;
|
|
2703
|
-
}
|
|
2704
|
-
}
|
|
2705
|
-
/**
|
|
2706
|
-
* The port to listen on. If not provided, defaults to 3000 and detects the next available port if in use.
|
|
2707
|
-
* @default 3000
|
|
2708
|
-
*/
|
|
2709
|
-
get port() {
|
|
2710
|
-
return this._port;
|
|
2711
|
-
}
|
|
2712
|
-
/**
|
|
2713
|
-
* The port to listen on. If not provided, defaults to 3000 and detects the next available port if in use.
|
|
2714
|
-
* @default 3000
|
|
2715
|
-
*/
|
|
2716
|
-
set port(port) {
|
|
2717
|
-
this._port = port;
|
|
2718
|
-
}
|
|
2719
|
-
/**
|
|
2720
|
-
* The host to listen on. If not provided, defaults to 'localhost'.
|
|
2721
|
-
* @default 'localhost'
|
|
2722
|
-
*/
|
|
2723
|
-
get host() {
|
|
2724
|
-
return this._host;
|
|
2725
|
-
}
|
|
2726
|
-
/**
|
|
2727
|
-
* The host to listen on. If not provided, defaults to 'localhost'.
|
|
2728
|
-
* @default 'localhost'
|
|
2729
|
-
*/
|
|
2730
|
-
set host(host) {
|
|
2731
|
-
this._host = host;
|
|
2732
|
-
}
|
|
2733
|
-
/**
|
|
2734
|
-
* Whether to automatically detect the next available port if the provided port is in use. Defaults to true.
|
|
2735
|
-
* @default true
|
|
2736
|
-
*/
|
|
2737
|
-
get autoDetectPort() {
|
|
2738
|
-
return this._autoDetectPort;
|
|
2739
|
-
}
|
|
2740
|
-
/**
|
|
2741
|
-
* Whether to automatically detect the next available port if the provided port is in use. Defaults to true.
|
|
2742
|
-
* @default true
|
|
2743
|
-
*/
|
|
2744
|
-
set autoDetectPort(autoDetectPort) {
|
|
2745
|
-
this._autoDetectPort = autoDetectPort;
|
|
2746
|
-
}
|
|
2747
|
-
/**
|
|
2748
|
-
* Whether to use Helmet for security headers. Defaults to true.
|
|
2749
|
-
* @default true
|
|
2750
|
-
*/
|
|
2751
|
-
get helmet() {
|
|
2752
|
-
return this._helmet;
|
|
2753
|
-
}
|
|
2754
|
-
/**
|
|
2755
|
-
* Whether to use Helmet for security headers. Defaults to true.
|
|
2756
|
-
* @default true
|
|
2757
|
-
*/
|
|
2758
|
-
set helmet(helmet) {
|
|
2759
|
-
this._helmet = helmet;
|
|
2760
|
-
}
|
|
2761
|
-
/**
|
|
2762
|
-
* Whether to use Swagger for API documentation. Defaults to true.
|
|
2763
|
-
* @default true
|
|
2764
|
-
*/
|
|
2765
|
-
get apiDocs() {
|
|
2766
|
-
return this._apiDocs;
|
|
2767
|
-
}
|
|
2768
|
-
/**
|
|
2769
|
-
* Whether to use Swagger for API documentation. Defaults to true.
|
|
2770
|
-
* @default true
|
|
2771
|
-
*/
|
|
2772
|
-
set apiDocs(apiDocs) {
|
|
2773
|
-
this._apiDocs = apiDocs;
|
|
2774
|
-
}
|
|
2775
|
-
/**
|
|
2776
|
-
* HTTP Bin options. Defaults to all enabled.
|
|
2777
|
-
*/
|
|
2778
|
-
get httpBin() {
|
|
2779
|
-
return this._httpBin;
|
|
2780
|
-
}
|
|
2781
|
-
/**
|
|
2782
|
-
* HTTP Bin options. Defaults to all enabled.
|
|
2783
|
-
*/
|
|
2784
|
-
set httpBin(httpBinary) {
|
|
2785
|
-
this._httpBin = httpBinary;
|
|
2786
|
-
}
|
|
2787
|
-
/**
|
|
2788
|
-
* The Fastify server instance.
|
|
2789
|
-
*/
|
|
2790
|
-
get server() {
|
|
2791
|
-
return this._server;
|
|
2792
|
-
}
|
|
2793
|
-
/**
|
|
2794
|
-
* The Fastify server instance.
|
|
2795
|
-
*/
|
|
2796
|
-
set server(server) {
|
|
2797
|
-
this._server = server;
|
|
2798
|
-
}
|
|
2799
|
-
/**
|
|
2800
|
-
* Start the Fastify server. If the server is already running, it will be closed and restarted.
|
|
2801
|
-
*/
|
|
2802
|
-
async start() {
|
|
2803
|
-
try {
|
|
2804
|
-
if (this._server) {
|
|
2805
|
-
await this._server.close();
|
|
2806
|
-
}
|
|
2807
|
-
this._server = Fastify(fastifyConfig);
|
|
2808
|
-
await this._server.register(fastifyStatic, {
|
|
2809
|
-
root: path.resolve("./node_modules/@scalar/api-reference/dist"),
|
|
2810
|
-
prefix: "/scalar"
|
|
2811
|
-
});
|
|
2812
|
-
await this.server.register(fastifyStatic, {
|
|
2813
|
-
root: path.resolve("./public"),
|
|
2814
|
-
decorateReply: false
|
|
2815
|
-
});
|
|
2816
|
-
await this._server.register(sitemapRoute);
|
|
2817
|
-
if (this._helmet) {
|
|
2818
|
-
await this._server.register(fastifyHelmet);
|
|
2819
|
-
}
|
|
2820
|
-
if (this._apiDocs) {
|
|
2821
|
-
await this.registerApiDocs();
|
|
2822
|
-
}
|
|
2823
|
-
const { httpMethods, redirects, requestInspection, responseInspection, responseFormats, statusCodes: statusCodes2, cookies, anything, auth } = this._httpBin;
|
|
2824
|
-
if (httpMethods) {
|
|
2825
|
-
await this.registerHttpMethods();
|
|
2826
|
-
}
|
|
2827
|
-
if (statusCodes2) {
|
|
2828
|
-
await this.registerStatusCodeRoutes();
|
|
2829
|
-
}
|
|
2830
|
-
if (requestInspection) {
|
|
2831
|
-
await this.registerRequestInspectionRoutes();
|
|
2832
|
-
}
|
|
2833
|
-
if (responseInspection) {
|
|
2834
|
-
await this.registerResponseInspectionRoutes();
|
|
2835
|
-
}
|
|
2836
|
-
if (responseFormats) {
|
|
2837
|
-
await this.registerResponseFormatRoutes();
|
|
2838
|
-
}
|
|
2839
|
-
if (redirects) {
|
|
2840
|
-
await this.registerRedirectRoutes();
|
|
2841
|
-
}
|
|
2842
|
-
if (cookies) {
|
|
2843
|
-
await this.registerCookieRoutes();
|
|
2844
|
-
}
|
|
2845
|
-
if (anything) {
|
|
2846
|
-
await this.registerAnythingRoutes();
|
|
2847
|
-
}
|
|
2848
|
-
if (auth) {
|
|
2849
|
-
await this.registerAuthRoutes();
|
|
2850
|
-
}
|
|
2851
|
-
if (this._autoDetectPort) {
|
|
2852
|
-
const originalPort = this._port;
|
|
2853
|
-
this._port = await this.detectPort();
|
|
2854
|
-
if (originalPort !== this._port) {
|
|
2855
|
-
this._server.log.info(`Port ${originalPort} is in use, detected next available port: ${this._port}`);
|
|
2856
|
-
}
|
|
2857
|
-
}
|
|
2858
|
-
await this._server.listen({
|
|
2859
|
-
port: this._port,
|
|
2860
|
-
host: this._host
|
|
2861
|
-
});
|
|
2862
|
-
} catch (error) {
|
|
2863
|
-
this._server.log.error(error);
|
|
2864
|
-
}
|
|
2865
|
-
}
|
|
2866
|
-
/**
|
|
2867
|
-
* Close the Fastify server.
|
|
2868
|
-
*/
|
|
2869
|
-
async close() {
|
|
2870
|
-
await this._server.close();
|
|
2871
|
-
}
|
|
2872
|
-
/**
|
|
2873
|
-
* Detect the next available port to run on.
|
|
2874
|
-
* @returns The port that is available to run on
|
|
2875
|
-
*/
|
|
2876
|
-
async detectPort() {
|
|
2877
|
-
const { port } = this;
|
|
2878
|
-
return detect(port);
|
|
2879
|
-
}
|
|
2880
|
-
/**
|
|
2881
|
-
* This will register the API documentation routes including openapi and swagger ui.
|
|
2882
|
-
* @param fastifyInstance - the server instance to register the routes on.
|
|
2883
|
-
*/
|
|
2884
|
-
async registerApiDocs(fastifyInstance) {
|
|
2885
|
-
const fastify = fastifyInstance ?? this._server;
|
|
2886
|
-
await fastify.register(fastifySwagger, fastifySwaggerConfig);
|
|
2887
|
-
await registerSwaggerUi(fastify);
|
|
2888
|
-
await fastify.register(indexRoute);
|
|
2889
|
-
}
|
|
2890
|
-
/**
|
|
2891
|
-
* Register the HTTP methods routes.
|
|
2892
|
-
* @param fastifyInstance - the server instance to register the routes on.
|
|
2893
|
-
*/
|
|
2894
|
-
async registerHttpMethods(fastifyInstance) {
|
|
2895
|
-
const fastify = fastifyInstance ?? this._server;
|
|
2896
|
-
await fastify.register(getRoute);
|
|
2897
|
-
await fastify.register(postRoute);
|
|
2898
|
-
await fastify.register(deleteRoute);
|
|
2899
|
-
await fastify.register(putRoute);
|
|
2900
|
-
await fastify.register(patchRoute);
|
|
2901
|
-
}
|
|
2902
|
-
/**
|
|
2903
|
-
* Register the status code routes.
|
|
2904
|
-
* @param fastifyInstance - the server instance to register the routes on.
|
|
2905
|
-
*/
|
|
2906
|
-
async registerStatusCodeRoutes(fastifyInstance) {
|
|
2907
|
-
const fastify = fastifyInstance ?? this._server;
|
|
2908
|
-
await fastify.register(statusCodeRoute);
|
|
2909
|
-
}
|
|
2910
|
-
/**
|
|
2911
|
-
* Register the request inspection routes.
|
|
2912
|
-
* @param fastifyInstance - the server instance to register the routes on.
|
|
2913
|
-
*/
|
|
2914
|
-
async registerRequestInspectionRoutes(fastifyInstance) {
|
|
2915
|
-
const fastify = fastifyInstance ?? this._server;
|
|
2916
|
-
await fastify.register(ipRoute);
|
|
2917
|
-
await fastify.register(headersRoute);
|
|
2918
|
-
await fastify.register(userAgentRoute);
|
|
2919
|
-
}
|
|
2920
|
-
/**
|
|
2921
|
-
* Register the response inspection routes.
|
|
2922
|
-
* @param fastifyInstance - the server instance to register the routes on.
|
|
2923
|
-
*/
|
|
2924
|
-
async registerResponseInspectionRoutes(fastifyInstance) {
|
|
2925
|
-
const fastify = fastifyInstance ?? this._server;
|
|
2926
|
-
await fastify.register(cacheRoutes);
|
|
2927
|
-
await fastify.register(etagRoutes);
|
|
2928
|
-
await fastify.register(responseHeadersRoutes);
|
|
2929
|
-
}
|
|
2930
|
-
async registerResponseFormatRoutes(fastifyInstance) {
|
|
2931
|
-
const fastify = fastifyInstance ?? this._server;
|
|
2932
|
-
await fastify.register(responseFormatRoutes);
|
|
2933
|
-
}
|
|
2934
|
-
/**
|
|
2935
|
-
* Register the redirect routes.
|
|
2936
|
-
* @param fastifyInstance - the server instance to register the routes on.
|
|
2937
|
-
*/
|
|
2938
|
-
async registerRedirectRoutes(fastifyInstance) {
|
|
2939
|
-
const fastify = fastifyInstance ?? this._server;
|
|
2940
|
-
await fastify.register(absoluteRedirectRoute);
|
|
2941
|
-
await fastify.register(relativeRedirectRoute);
|
|
2942
|
-
await fastify.register(redirectToRoute);
|
|
2943
|
-
}
|
|
2944
|
-
/**
|
|
2945
|
-
* Register the cookie routes.
|
|
2946
|
-
* @param fastifyInstance - the server instance to register the routes on.
|
|
2947
|
-
*/
|
|
2948
|
-
async registerCookieRoutes(fastifyInstance) {
|
|
2949
|
-
const fastify = fastifyInstance ?? this._server;
|
|
2950
|
-
await fastify.register(fastifyCookie);
|
|
2951
|
-
await fastify.register(getCookiesRoute);
|
|
2952
|
-
await fastify.register(postCookieRoute);
|
|
2953
|
-
await fastify.register(deleteCookieRoute);
|
|
2954
|
-
}
|
|
2955
|
-
/**
|
|
2956
|
-
* Register the anything routes.
|
|
2957
|
-
* @param fastifyInstance - the server instance to register the routes on.
|
|
2958
|
-
*/
|
|
2959
|
-
async registerAnythingRoutes(fastifyInstance) {
|
|
2960
|
-
const fastify = fastifyInstance ?? this._server;
|
|
2961
|
-
await fastify.register(anythingRoute);
|
|
2962
|
-
}
|
|
2963
|
-
/**
|
|
2964
|
-
* Register the auth routes.
|
|
2965
|
-
* @param fastifyInstance - the server instance to register the routes on.
|
|
2966
|
-
*/
|
|
2967
|
-
async registerAuthRoutes(fastifyInstance) {
|
|
2968
|
-
const fastify = fastifyInstance ?? this._server;
|
|
2969
|
-
await fastify.register(basicAuthRoute);
|
|
2970
|
-
await fastify.register(hiddenBasicAuthRoute);
|
|
2971
|
-
await fastify.register(bearerAuthRoute);
|
|
2972
|
-
await fastify.register(digestAuthRoute);
|
|
2973
|
-
}
|
|
2974
|
-
};
|
|
2975
|
-
|
|
2976
|
-
// src/index.ts
|
|
2977
|
-
var start = /* @__PURE__ */ __name(async () => {
|
|
2978
|
-
const mockHttp = new MockHttp();
|
|
2979
|
-
if (process.env.PORT) {
|
|
2980
|
-
mockHttp.port = Number.parseInt(process.env.PORT, 10);
|
|
2981
|
-
}
|
|
2982
|
-
if (process.env.HOST) {
|
|
2983
|
-
mockHttp.host = process.env.HOST;
|
|
2984
|
-
}
|
|
2985
|
-
await mockHttp.start();
|
|
2986
|
-
return mockHttp;
|
|
2987
|
-
}, "start");
|
|
2988
|
-
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
2989
|
-
await start();
|
|
2990
|
-
}
|
|
2991
|
-
export {
|
|
2992
|
-
MockHttp as default,
|
|
2993
|
-
MockHttp as mockhttp,
|
|
2994
|
-
start
|
|
2995
|
-
};
|
|
252
|
+
`,Ce={openapi:{info:{title:"Mock HTTP API",description:ct,version:je.version},consumes:["application/json"],produces:["application/json"]}},Pe=r(async s=>{await s.register(nt,{routePrefix:"/docs",uiConfig:{docExpansion:"none",deepLinking:!1},uiHooks:{onRequest(e,t,o){o()},preHandler(e,t,o){o()}},staticCSP:!0,transformSpecification:r((e,t,o)=>e,"transformSpecification"),transformSpecificationClone:!0})},"registerSwaggerUi");import{randomUUID as dt}from"crypto";var L=class{static{r(this,"TapManager")}_injections;constructor(){this._injections=new Map}get injections(){return this._injections}get hasInjections(){return this._injections.size>0}inject(e,t){let o=dt(),a={id:o,response:e,matcher:t};return this._injections.set(o,a),a}removeInjection(e){let t=typeof e=="string"?e:e.id;return this._injections.delete(t)}matchRequest(e){for(let t of this._injections.values())if(this.requestMatches(e,t.matcher))return t}clear(){this._injections.clear()}requestMatches(e,t){if(!t)return!0;if(t.url&&!this.urlMatches(e.url,t.url)||t.hostname&&e.hostname!==t.hostname||t.method&&e.method.toUpperCase()!==t.method.toUpperCase())return!1;if(t.headers){for(let[o,a]of Object.entries(t.headers))if(e.headers[o.toLowerCase()]!==a)return!1}return!0}urlMatches(e,t){let o=e.split("?")[0];if(t===o)return!0;if(t.includes("*")){let a=t.split("*").map(n=>n.replaceAll(/[.*+?^${}()|[\]\\]/g,"\\$&")).join(".*");return new RegExp(`^${a}$`).test(o)}return!1}};var k=class extends lt{static{r(this,"MockHttp")}_port=3e3;_host="0.0.0.0";_autoDetectPort=!0;_helmet=!0;_apiDocs=!0;_httpBin={httpMethods:!0,redirects:!0,requestInspection:!0,responseInspection:!0,responseFormats:!0,statusCodes:!0,cookies:!0,anything:!0,auth:!0};_server=He();_taps=new L;constructor(e){super(e?.hookOptions),e?.port&&(this._port=e.port),e?.host&&(this._host=e.host),e?.helmet!==void 0&&(this._helmet=e.helmet),e?.apiDocs!==void 0&&(this._apiDocs=e.apiDocs),e?.httpBin!==void 0&&(this._httpBin=e.httpBin)}get port(){return this._port}set port(e){this._port=e}get host(){return this._host}set host(e){this._host=e}get autoDetectPort(){return this._autoDetectPort}set autoDetectPort(e){this._autoDetectPort=e}get helmet(){return this._helmet}set helmet(e){this._helmet=e}get apiDocs(){return this._apiDocs}set apiDocs(e){this._apiDocs=e}get httpBin(){return this._httpBin}set httpBin(e){this._httpBin=e}get server(){return this._server}set server(e){this._server=e}get taps(){return this._taps}set taps(e){this._taps=e}async start(){try{this._server&&await this._server.close(),this._server=He(ce),this._server.addHook("onRequest",async(l,c)=>{let p=this._taps.matchRequest(l);if(p){let u=typeof p.response=="function"?p.response(l):p.response,{response:m,statusCode:y=200,headers:b}=u;if(c.code(y),b)for(let[R,f]of Object.entries(b))c.header(R,f);return c.send(m)}}),await this._server.register(_e,{root:qe.resolve("./node_modules/@scalar/api-reference/dist"),prefix:"/scalar"}),await this.server.register(_e,{root:qe.resolve("./public"),decorateReply:!1}),await this._server.register(Te),this._helmet&&await this._server.register(ht),this._apiDocs&&await this.registerApiDocs();let{httpMethods:e,redirects:t,requestInspection:o,responseInspection:a,responseFormats:i,statusCodes:n,cookies:d,anything:g,auth:h}=this._httpBin;if(e&&await this.registerHttpMethods(),n&&await this.registerStatusCodeRoutes(),o&&await this.registerRequestInspectionRoutes(),a&&await this.registerResponseInspectionRoutes(),i&&await this.registerResponseFormatRoutes(),t&&await this.registerRedirectRoutes(),d&&await this.registerCookieRoutes(),g&&await this.registerAnythingRoutes(),h&&await this.registerAuthRoutes(),this._autoDetectPort){let l=this._port;this._port=await this.detectPort(),l!==this._port&&this._server.log.info(`Port ${l} is in use, detected next available port: ${this._port}`)}await this._server.listen({port:this._port,host:this._host})}catch(e){this._server.log.error(e)}}async close(){await this._server.close()}async detectPort(){let{port:e}=this;return ut(e)}async registerApiDocs(e){let t=e??this._server;await t.register(mt,Ce),await Pe(t),await t.register(ge)}async registerHttpMethods(e){let t=e??this._server;await t.register(Y),await t.register(V),await t.register(J),await t.register(X),await t.register(Z)}async registerStatusCodeRoutes(e){await(e??this._server).register(Se)}async registerRequestInspectionRoutes(e){let t=e??this._server;await t.register(se),await t.register(te),await t.register(oe)}async registerResponseInspectionRoutes(e){let t=e??this._server;await t.register(re),await t.register(ae),await t.register(ie)}async registerResponseFormatRoutes(e){await(e??this._server).register(ke)}async registerRedirectRoutes(e){let t=e??this._server;await t.register(K),await t.register(ee),await t.register(Q)}async registerCookieRoutes(e){let t=e??this._server;await t.register(pt),await t.register(N),await t.register(G),await t.register(W)}async registerAnythingRoutes(e){await(e??this._server).register(de)}async registerAuthRoutes(e){let t=e??this._server;await t.register(E),await t.register(F),await t.register(O),await t.register(B)}};var gt=r(async()=>{let s=new k;return I.env.PORT&&(s.port=Number.parseInt(I.env.PORT,10)),I.env.HOST&&(s.host=I.env.HOST),await s.start(),s},"start");import.meta.url===`file://${I.argv[1]}`&&await gt();export{k as default,k as mockhttp,gt as start};
|