You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+259Lines changed: 259 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,6 +12,7 @@ A simple HTTP server that can be used to mock HTTP responses for testing purpose
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
16
*`@fastify/helmet` built in by default
16
17
* Built with `nodejs`, `typescript`, and `fastify`
17
18
* Deploy via `docker` or `nodejs`
@@ -63,6 +64,264 @@ console.log(reaponse);
63
64
await mockhttp.stop(); // stop the server
64
65
```
65
66
67
+
# Response Injection (Tap Feature)
68
+
69
+
The injection/tap feature allows you to "tap into" the request flow and inject custom responses for specific requests. This is particularly useful for:
70
+
- **Offline testing** - Mock external API responses without network access
71
+
- **Testing edge cases** - Simulate errors, timeouts, or specific response scenarios
72
+
- **Development** - Work on your application without depending on external services
73
+
74
+
## What is a "Tap"?
75
+
76
+
A "tap" is a reference to an injected response, similar to "wiretapping" - you're intercepting requests and returning predefined responses. Each tap can be removed when you're done with it, restoring normal server behavior.
77
+
78
+
## Basic Usage
79
+
80
+
```javascript
81
+
import { mockhttp } from '@jaredwray/mockhttp';
82
+
83
+
const mock = new mockhttp();
84
+
await mock.start();
85
+
86
+
// Inject a simple response
87
+
const tap = mock.taps.inject(
88
+
{
89
+
response: "Hello, World!",
90
+
statusCode: 200,
91
+
headers: { "Content-Type": "text/plain" }
92
+
},
93
+
{
94
+
url: "/api/greeting",
95
+
method: "GET"
96
+
}
97
+
);
98
+
99
+
// Make requests - they will get the injected response
- `url?`: string - URL path (supports wildcards with `*`)
294
+
- `method?`: string - HTTP method (GET, POST, etc.)
295
+
- `hostname?`: string - Hostname to match
296
+
- `headers?`: object - Headers that must be present
297
+
298
+
**Returns:** `InjectionTap` - A tap object with a unique `id` that can be used to remove the injection
299
+
300
+
## `taps.removeInjection(tapOrId)`
301
+
302
+
Removes an injection.
303
+
304
+
**Parameters:**
305
+
- `tapOrId`: InjectionTap | string - The tap object or tap ID to remove
306
+
307
+
**Returns:** boolean - `true` if removed, `false` if not found
308
+
309
+
### `taps.injections`
310
+
311
+
A getter that returns a Map of all active injection taps.
312
+
313
+
**Returns:** `Map<string, InjectionTap>` - Map of all active injections with tap IDs as keys
314
+
315
+
## `taps.clear()`
316
+
317
+
Removes all injections.
318
+
319
+
## `taps.hasInjections`
320
+
321
+
A getter that returns whether there are any active injections.
322
+
323
+
**Returns:** boolean - `true` if there are active injections, `false` otherwise
324
+
66
325
# About mockhttp.org
67
326
68
327
[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 such as requests per second. 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.
0 commit comments