@jaredwray/mockhttp 0.2.3 → 0.3.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.
Files changed (3) hide show
  1. package/README.md +12 -4
  2. package/dist/index.js +197 -13
  3. package/package.json +12 -12
package/README.md CHANGED
@@ -7,9 +7,17 @@
7
7
  [![npm](https://img.shields.io/npm/v/@jaredwray/mockhttp)](https://npmjs.com/package/@jaredwray/mockhttp)
8
8
  [![Docker Pulls](https://img.shields.io/docker/pulls/jaredwray/mockhttp)](https://hub.docker.com/r/jaredwray/mockhttp)
9
9
 
10
-
11
10
  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`.
12
11
 
12
+ # Features
13
+ * All the features of [httpbin](https://httpbin.org/)
14
+ * CORS and Helmet support
15
+ * Built with `nodejs` and `fastify`
16
+ * Deploy via `docker` or `nodejs`
17
+ * Global deployment via [mockhttp.org](https://mockhttp.org) (free service)
18
+ * Better API documentation and examples
19
+ * Maintained and updated regularly!
20
+
13
21
  # Deploy via Docker
14
22
  ```bash
15
23
  docker run -d -p 3000:3000 jaredwray/mockhttp
@@ -24,14 +32,14 @@ then run `mockhttp` in your code.
24
32
 
25
33
  ```javascript
26
34
  import { mockhttp } from '@jaredwray/mockhttp';
27
- mockhttp.start(); // start the server
35
+ await mockhttp.start(); // start the server
28
36
  const response = await fetch('http://localhost:8080/ip');
29
- mockhttp.stop(); // stop the server
37
+ await mockhttp.stop(); // stop the server
30
38
  ```
31
39
 
32
40
  # About mockhttp.org
33
41
 
34
- [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. It is globally available has some limitations on it to prevent abuse but many people will not see it. It is ran via [Cloudflare](https://cloudflare.com) and [Google Cloud Run](https://cloud.google.com/run/) across 7 regions globally.
42
+ [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. It is globally available has some limitations on it to prevent abuse but many people will not see it. It is 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.
35
43
 
36
44
  # Contributing
37
45
 
package/dist/index.js CHANGED
@@ -132,7 +132,7 @@ import { fastifySwaggerUi } from "@fastify/swagger-ui";
132
132
  // package.json
133
133
  var package_default = {
134
134
  name: "@jaredwray/mockhttp",
135
- version: "0.2.3",
135
+ version: "0.3.0",
136
136
  description: "Mock Http - Easy way to mock http with httpbin replacement",
137
137
  type: "module",
138
138
  main: "dist/index.js",
@@ -168,23 +168,23 @@ var package_default = {
168
168
  },
169
169
  homepage: "https://github.com/jaredwray/mockhttp#readme",
170
170
  devDependencies: {
171
- "@swc/core": "^1.10.4",
171
+ "@swc/core": "^1.10.9",
172
172
  "@types/html-escaper": "^3.0.2",
173
- "@vitest/coverage-v8": "^2.1.8",
173
+ "@vitest/coverage-v8": "^3.0.4",
174
174
  rimraf: "^6.0.1",
175
- tsup: "^8.3.5",
175
+ tsup: "^8.3.6",
176
176
  tsx: "^4.19.2",
177
- typescript: "^5.7.2",
178
- vitest: "^2.1.8",
177
+ typescript: "^5.7.3",
178
+ vitest: "^3.0.4",
179
179
  xo: "^0.60.0"
180
180
  },
181
181
  dependencies: {
182
- "@fastify/helmet": "^13.0.0",
183
- "@fastify/static": "^8.0.3",
184
- "@fastify/swagger": "^9.4.0",
185
- "@fastify/swagger-ui": "^5.2.0",
186
- "@scalar/api-reference": "^1.25.99",
187
- fastify: "^5.2.0",
182
+ "@fastify/helmet": "^13.0.1",
183
+ "@fastify/static": "^8.0.4",
184
+ "@fastify/swagger": "^9.4.2",
185
+ "@fastify/swagger-ui": "^5.2.1",
186
+ "@scalar/api-reference": "^1.25.108",
187
+ fastify: "^5.2.1",
188
188
  "html-escaper": "^3.0.3",
189
189
  pino: "^9.6.0",
190
190
  "pino-pretty": "^13.0.0"
@@ -197,11 +197,22 @@ var package_default = {
197
197
  };
198
198
 
199
199
  // src/swagger.ts
200
+ var description = `
201
+ 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\`.
202
+
203
+ * [GitHub Repository](https://github.com/jaredwray/mockhttp)
204
+ * [Docker Image](https://hub.docker.com/r/jaredwray/mockhttp)
205
+ * [NPM Package](https://www.npmjs.com/package/jaredwray/mockhttp)
206
+
207
+ # About mockhttp.org
208
+
209
+ [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. It is globally available has some limitations on it to prevent abuse but many people will not see it. It is 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.
210
+ `;
200
211
  var fastifySwaggerConfig = {
201
212
  openapi: {
202
213
  info: {
203
214
  title: "Mock HTTP API",
204
- description: "A replacement for httpbin built using Fastify and Typescript",
215
+ description,
205
216
  version: package_default.version
206
217
  },
207
218
  consumes: [
@@ -1074,6 +1085,176 @@ var responseHeadersRoutes = /* @__PURE__ */ __name((fastify2) => {
1074
1085
  });
1075
1086
  }, "responseHeadersRoutes");
1076
1087
 
1088
+ // src/routes/redirects/absolute-redirect.ts
1089
+ var absoluteRedirectSchema = {
1090
+ tags: [
1091
+ "Redirects"
1092
+ ],
1093
+ description: "Redirects the request to the target URL using an absolute URL",
1094
+ params: {
1095
+ type: "object",
1096
+ properties: {
1097
+ value: {
1098
+ type: "integer",
1099
+ minimum: 1
1100
+ }
1101
+ },
1102
+ required: [
1103
+ "value"
1104
+ ]
1105
+ },
1106
+ headers: {
1107
+ type: "object",
1108
+ properties: {
1109
+ host: {
1110
+ type: "string"
1111
+ }
1112
+ },
1113
+ required: [
1114
+ "host"
1115
+ ]
1116
+ },
1117
+ response: {
1118
+ // eslint-disable-next-line @typescript-eslint/naming-convention
1119
+ 302: {
1120
+ type: "string"
1121
+ }
1122
+ }
1123
+ };
1124
+ var absoluteRedirectRoute = /* @__PURE__ */ __name((fastify2) => {
1125
+ fastify2.get("/absolute-redirect/:value", {
1126
+ schema: absoluteRedirectSchema
1127
+ }, async (request, reply) => {
1128
+ const value = request.params.value;
1129
+ const { host } = request.headers;
1130
+ const protocol = request.protocol;
1131
+ let url = `${protocol}://${host}/get`;
1132
+ let html = `
1133
+ <title>Redirecting...</title>
1134
+ <h1>Redirecting...</h1>
1135
+ <p>You should be redirected automatically to target URL: <a href="${url}">${url}</a>. If not click the link.</p>
1136
+ `;
1137
+ if (value > 1) {
1138
+ url = `${protocol}://${host}/absolute-redirect/${value - 1}`;
1139
+ html = `
1140
+ <title>Redirecting...</title>
1141
+ <h1>Redirecting...</h1>
1142
+ <p>You should be redirected automatically to target URL: <a href="${url}">${url}</a>. If not click the link.</p>
1143
+ `;
1144
+ }
1145
+ await reply.header("Location", url).status(302).type("text/html").send(html);
1146
+ });
1147
+ }, "absoluteRedirectRoute");
1148
+
1149
+ // src/routes/redirects/relative-redirect.ts
1150
+ var relativeRedirectSchema = {
1151
+ tags: [
1152
+ "Redirects"
1153
+ ],
1154
+ description: "Redirects the request to the target URL using an relative URL",
1155
+ params: {
1156
+ type: "object",
1157
+ properties: {
1158
+ value: {
1159
+ type: "integer",
1160
+ minimum: 1
1161
+ }
1162
+ },
1163
+ required: [
1164
+ "value"
1165
+ ]
1166
+ },
1167
+ headers: {
1168
+ type: "object",
1169
+ properties: {
1170
+ host: {
1171
+ type: "string"
1172
+ }
1173
+ },
1174
+ required: [
1175
+ "host"
1176
+ ]
1177
+ },
1178
+ response: {
1179
+ // eslint-disable-next-line @typescript-eslint/naming-convention
1180
+ 302: {
1181
+ type: "string"
1182
+ }
1183
+ }
1184
+ };
1185
+ var relativeRedirectRoute = /* @__PURE__ */ __name((fastify2) => {
1186
+ fastify2.get("/relative-redirect/:value", {
1187
+ schema: relativeRedirectSchema
1188
+ }, async (request, reply) => {
1189
+ const value = request.params.value;
1190
+ let url = "/get";
1191
+ let html = `
1192
+ <title>Redirecting...</title>
1193
+ <h1>Redirecting...</h1>
1194
+ <p>You should be redirected automatically to target URL: <a href="${url}">${url}</a>. If not click the link.</p>
1195
+ `;
1196
+ if (value > 1) {
1197
+ url = `/relative-redirect/${value - 1}`;
1198
+ html = `
1199
+ <title>Redirecting...</title>
1200
+ <h1>Redirecting...</h1>
1201
+ <p>You should be redirected automatically to target URL: <a href="${url}">${url}</a>. If not click the link.</p>
1202
+ `;
1203
+ }
1204
+ await reply.header("Location", url).status(302).type("text/html").send(html);
1205
+ });
1206
+ }, "relativeRedirectRoute");
1207
+
1208
+ // src/routes/redirects/redirect-to.ts
1209
+ var redirectToSchema = {
1210
+ tags: [
1211
+ "Redirects"
1212
+ ],
1213
+ description: "Redirects the request to the target URL",
1214
+ querystring: {
1215
+ type: "object",
1216
+ properties: {
1217
+ url: {
1218
+ type: "string",
1219
+ format: "uri",
1220
+ description: "The URL to redirect to"
1221
+ },
1222
+ // eslint-disable-next-line @typescript-eslint/naming-convention
1223
+ status_code: {
1224
+ type: "string",
1225
+ pattern: "^[1-5][0-9][0-9]$",
1226
+ description: "The HTTP status code to use for redirection"
1227
+ }
1228
+ },
1229
+ required: [
1230
+ "url"
1231
+ ],
1232
+ additionalProperties: false
1233
+ }
1234
+ };
1235
+ var redirectToRoute = /* @__PURE__ */ __name((fastify2) => {
1236
+ const handler = /* @__PURE__ */ __name(async (request, reply) => {
1237
+ const { url, status_code } = request.query;
1238
+ const statusCode = status_code ? Number(status_code) : 302;
1239
+ await reply.redirect(url, statusCode);
1240
+ }, "handler");
1241
+ fastify2.get("/redirect-to", {
1242
+ schema: redirectToSchema
1243
+ }, handler);
1244
+ fastify2.post("/redirect-to", {
1245
+ schema: redirectToSchema
1246
+ }, handler);
1247
+ fastify2.put("/redirect-to", {
1248
+ schema: redirectToSchema
1249
+ }, handler);
1250
+ fastify2.patch("/redirect-to", {
1251
+ schema: redirectToSchema
1252
+ }, handler);
1253
+ fastify2.delete("/redirect-to", {
1254
+ schema: redirectToSchema
1255
+ }, handler);
1256
+ }, "redirectToRoute");
1257
+
1077
1258
  // src/index.ts
1078
1259
  var fastify = Fastify(fastifyConfig);
1079
1260
  await fastify.register(fastifyStatic, {
@@ -1101,6 +1282,9 @@ await fastify.register(userAgentRoute);
1101
1282
  await fastify.register(cacheRoutes);
1102
1283
  await fastify.register(etagRoutes);
1103
1284
  await fastify.register(responseHeadersRoutes);
1285
+ await fastify.register(absoluteRedirectRoute);
1286
+ await fastify.register(relativeRedirectRoute);
1287
+ await fastify.register(redirectToRoute);
1104
1288
  var start = /* @__PURE__ */ __name(async () => {
1105
1289
  try {
1106
1290
  let port = 3e3;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jaredwray/mockhttp",
3
- "version": "0.2.3",
3
+ "version": "0.3.0",
4
4
  "description": "Mock Http - Easy way to mock http with httpbin replacement",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -36,23 +36,23 @@
36
36
  },
37
37
  "homepage": "https://github.com/jaredwray/mockhttp#readme",
38
38
  "devDependencies": {
39
- "@swc/core": "^1.10.4",
39
+ "@swc/core": "^1.10.9",
40
40
  "@types/html-escaper": "^3.0.2",
41
- "@vitest/coverage-v8": "^2.1.8",
41
+ "@vitest/coverage-v8": "^3.0.4",
42
42
  "rimraf": "^6.0.1",
43
- "tsup": "^8.3.5",
43
+ "tsup": "^8.3.6",
44
44
  "tsx": "^4.19.2",
45
- "typescript": "^5.7.2",
46
- "vitest": "^2.1.8",
45
+ "typescript": "^5.7.3",
46
+ "vitest": "^3.0.4",
47
47
  "xo": "^0.60.0"
48
48
  },
49
49
  "dependencies": {
50
- "@fastify/helmet": "^13.0.0",
51
- "@fastify/static": "^8.0.3",
52
- "@fastify/swagger": "^9.4.0",
53
- "@fastify/swagger-ui": "^5.2.0",
54
- "@scalar/api-reference": "^1.25.99",
55
- "fastify": "^5.2.0",
50
+ "@fastify/helmet": "^13.0.1",
51
+ "@fastify/static": "^8.0.4",
52
+ "@fastify/swagger": "^9.4.2",
53
+ "@fastify/swagger-ui": "^5.2.1",
54
+ "@scalar/api-reference": "^1.25.108",
55
+ "fastify": "^5.2.1",
56
56
  "html-escaper": "^3.0.3",
57
57
  "pino": "^9.6.0",
58
58
  "pino-pretty": "^13.0.0"