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