@jaredwray/mockhttp 1.5.1 → 1.6.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 +18 -2
  2. package/dist/index.mjs +43 -12
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -8,11 +8,11 @@
8
8
  [![Docker Pulls](https://img.shields.io/docker/pulls/jaredwray/mockhttp)](https://hub.docker.com/r/jaredwray/mockhttp)
9
9
  [![mockhttp.org](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fjaredwray.com%2Fapi%2Fmockhttp-traffic&query=%24.message&label=mockhttp.org)](https://mockhttp.org)
10
10
 
11
- 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`.
11
+ 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
12
 
13
13
  # Features
14
14
  * All the features of [httpbin](https://httpbin.org/)
15
- * Taps - Inject custom responses for testing and develepment
15
+ * Taps - Inject custom responses for testing and development
16
16
  * `@fastify/helmet` built in by default
17
17
  * Built with `nodejs`, `typescript`, and `fastify`
18
18
  * Deploy via `docker` or `nodejs`
@@ -30,6 +30,7 @@ A simple HTTP server that can be used to mock HTTP responses for testing purpose
30
30
  - [Response Injection (Tap Feature)](#response-injection-tap-feature)
31
31
  - [Rate Limiting](#rate-limiting)
32
32
  - [Logging](#logging)
33
+ - [Flexible URL Matching](#flexible-url-matching)
33
34
  - [API Reference](#api-reference)
34
35
  - [About mockhttp.org](#about-mockhttporg)
35
36
  - [Contributing](#contributing)
@@ -608,6 +609,21 @@ You can also disable logging via the `LOGGING` environment variable:
608
609
  LOGGING=false node your-app.js
609
610
  ```
610
611
 
612
+ # Flexible URL Matching
613
+
614
+ MockHttp ignores trailing path segments that come after the parsable portion of a URL. This is useful when a client appends extra data to a known endpoint — instead of returning 404, MockHttp serves the closest matching route.
615
+
616
+ For example, all of these are served by `/status/:code`:
617
+
618
+ ```
619
+ GET /status/429
620
+ GET /status/429/
621
+ GET /status/429/foo
622
+ GET /status/429/foo/bar
623
+ ```
624
+
625
+ The rewrite preserves the query string and only triggers when a more specific route exists; URLs whose first path segment doesn't correspond to a registered route still return 404.
626
+
611
627
  # API Reference
612
628
 
613
629
  ## MockHttp Class
package/dist/index.mjs CHANGED
@@ -246,16 +246,47 @@ async function generateCertificateFiles(options) {
246
246
  }
247
247
  //#endregion
248
248
  //#region src/fastify-config.ts
249
+ function isSpecificMatch(route) {
250
+ if (!route) return false;
251
+ const params = route.params;
252
+ return params === void 0 || params["*"] === void 0;
253
+ }
254
+ function rewriteUrl(req) {
255
+ const originalUrl = req.url ?? "/";
256
+ const method = req.method;
257
+ if (!method) return originalUrl;
258
+ const queryIdx = originalUrl.indexOf("?");
259
+ const path = queryIdx >= 0 ? originalUrl.slice(0, queryIdx) : originalUrl;
260
+ const query = queryIdx >= 0 ? originalUrl.slice(queryIdx) : "";
261
+ if (isSpecificMatch(this.findRoute({
262
+ method,
263
+ url: path
264
+ }))) return originalUrl;
265
+ const segments = path.split("/").filter(Boolean);
266
+ while (segments.length >= 1) {
267
+ const candidatePath = `/${segments.join("/")}`;
268
+ if (candidatePath !== path && isSpecificMatch(this.findRoute({
269
+ method,
270
+ url: candidatePath
271
+ }))) return candidatePath + query;
272
+ if (segments.length === 1) break;
273
+ segments.pop();
274
+ }
275
+ return originalUrl;
276
+ }
249
277
  function getFastifyConfig(logging = true) {
250
- return { logger: logging ? { transport: {
251
- target: "pino-pretty",
252
- options: {
253
- colorize: true,
254
- translateTime: true,
255
- ignore: "pid,hostname",
256
- singleLine: true
257
- }
258
- } } : false };
278
+ return {
279
+ logger: logging ? { transport: {
280
+ target: "pino-pretty",
281
+ options: {
282
+ colorize: true,
283
+ translateTime: true,
284
+ ignore: "pid,hostname",
285
+ singleLine: true
286
+ }
287
+ } } : false,
288
+ rewriteUrl
289
+ };
259
290
  }
260
291
  //#endregion
261
292
  //#region src/routes/anything/index.ts
@@ -2867,17 +2898,17 @@ const fastifySwaggerConfig = { openapi: {
2867
2898
  info: {
2868
2899
  title: "Mock HTTP API",
2869
2900
  description: `
2870
- 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\`.
2901
+ 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\`.
2871
2902
 
2872
2903
  * [GitHub Repository](https://github.com/jaredwray/mockhttp)
2873
2904
  * [Docker Image](https://hub.docker.com/r/jaredwray/mockhttp)
2874
- * [NPM Package](https://www.npmjs.com/package/jaredwray/mockhttp)
2905
+ * [NPM Package](https://www.npmjs.com/package/@jaredwray/mockhttp)
2875
2906
 
2876
2907
  # About mockhttp.org
2877
2908
 
2878
2909
  [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.
2879
2910
  `,
2880
- version: "1.5.1"
2911
+ version: "1.6.0"
2881
2912
  },
2882
2913
  consumes: ["application/json"],
2883
2914
  produces: ["application/json"]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jaredwray/mockhttp",
3
- "version": "1.5.1",
3
+ "version": "1.6.0",
4
4
  "description": "Mock Http - Easy way to mock http with httpbin replacement",
5
5
  "type": "module",
6
6
  "main": "dist/index.mjs",