@jaredwray/mockhttp 1.2.0 → 1.4.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 +127 -3
- package/dist/index.d.ts +103 -1
- package/dist/index.js +1248 -21
- package/package.json +18 -13
package/README.md
CHANGED
|
@@ -25,6 +25,7 @@ A simple HTTP server that can be used to mock HTTP responses for testing purpose
|
|
|
25
25
|
- [Deploy via Docker](#deploy-via-docker)
|
|
26
26
|
- [Deploy via Docker Compose](#deploy-via-docker-compose)
|
|
27
27
|
- [Deploy via NodeJS](#deploy-via-nodejs)
|
|
28
|
+
- [HTTPS Support](#https-support)
|
|
28
29
|
- [Response Injection (Tap Feature)](#response-injection-tap-feature)
|
|
29
30
|
- [Rate Limiting](#rate-limiting)
|
|
30
31
|
- [Logging](#logging)
|
|
@@ -69,13 +70,133 @@ npm install @jaredwray/mockhttp --save
|
|
|
69
70
|
then run `mockhttp` in your code.
|
|
70
71
|
|
|
71
72
|
```javascript
|
|
72
|
-
import {
|
|
73
|
-
|
|
73
|
+
import { MockHttp } from '@jaredwray/mockhttp';
|
|
74
|
+
const mock = new MockHttp();
|
|
75
|
+
await mock.start(); // start the server
|
|
74
76
|
const response = await fetch('http://localhost:3000/get');
|
|
75
77
|
console.log(response);
|
|
76
|
-
await
|
|
78
|
+
await mock.close(); // stop the server
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
# HTTPS Support
|
|
82
|
+
|
|
83
|
+
MockHttp supports HTTPS with auto-generated self-signed certificates or your own custom certificates. No external dependencies are required — certificate generation uses only Node.js built-in `crypto`.
|
|
84
|
+
|
|
85
|
+
## Auto-Generated Certificate
|
|
86
|
+
|
|
87
|
+
The simplest way to enable HTTPS is to pass `https: true`. A self-signed certificate for `localhost` is generated automatically:
|
|
88
|
+
|
|
89
|
+
```javascript
|
|
90
|
+
import { MockHttp } from '@jaredwray/mockhttp';
|
|
91
|
+
|
|
92
|
+
const mock = new MockHttp({ https: true });
|
|
93
|
+
await mock.start();
|
|
94
|
+
|
|
95
|
+
console.log(mock.isHttps); // true
|
|
96
|
+
|
|
97
|
+
// Use Fastify's built-in inject() for testing (no TLS setup needed)
|
|
98
|
+
const response = await mock.server.inject({ method: 'GET', url: '/get' });
|
|
99
|
+
console.log(response.statusCode); // 200
|
|
100
|
+
|
|
101
|
+
await mock.close();
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
> **Note:** Self-signed certificates are not trusted by default. When making real HTTPS requests (e.g. with `fetch`), set `NODE_TLS_REJECT_UNAUTHORIZED=0` in your test environment or use a custom HTTPS agent.
|
|
105
|
+
|
|
106
|
+
## Custom Certificate Options
|
|
107
|
+
|
|
108
|
+
You can customize the auto-generated certificate by passing `certificateOptions`:
|
|
109
|
+
|
|
110
|
+
```javascript
|
|
111
|
+
const mock = new MockHttp({
|
|
112
|
+
https: {
|
|
113
|
+
certificateOptions: {
|
|
114
|
+
commonName: 'my-test-server',
|
|
115
|
+
validityDays: 30,
|
|
116
|
+
keySize: 4096,
|
|
117
|
+
altNames: [
|
|
118
|
+
{ type: 'dns', value: 'example.local' },
|
|
119
|
+
{ type: 'dns', value: '*.example.local' },
|
|
120
|
+
{ type: 'ip', value: '192.168.1.100' },
|
|
121
|
+
],
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
await mock.start();
|
|
127
|
+
// Make requests...
|
|
128
|
+
await mock.close();
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Provide Your Own Certificate
|
|
132
|
+
|
|
133
|
+
You can supply your own PEM-encoded certificate and key, either as strings or file paths:
|
|
134
|
+
|
|
135
|
+
```javascript
|
|
136
|
+
// Using PEM strings
|
|
137
|
+
const mock = new MockHttp({
|
|
138
|
+
https: {
|
|
139
|
+
cert: '-----BEGIN CERTIFICATE-----\n...',
|
|
140
|
+
key: '-----BEGIN PRIVATE KEY-----\n...',
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
await mock.start();
|
|
144
|
+
// Make requests...
|
|
145
|
+
await mock.close();
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
```javascript
|
|
149
|
+
// Using file paths
|
|
150
|
+
const mock = new MockHttp({
|
|
151
|
+
https: {
|
|
152
|
+
cert: '/path/to/cert.pem',
|
|
153
|
+
key: '/path/to/key.pem',
|
|
154
|
+
},
|
|
155
|
+
});
|
|
156
|
+
await mock.start();
|
|
157
|
+
// Make requests...
|
|
158
|
+
await mock.close();
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## Standalone Certificate Generation
|
|
162
|
+
|
|
163
|
+
You can also generate certificates independently using the exported utility functions:
|
|
164
|
+
|
|
165
|
+
```javascript
|
|
166
|
+
import { generateCertificate, generateCertificateFiles } from '@jaredwray/mockhttp';
|
|
167
|
+
|
|
168
|
+
// Generate in-memory PEM strings
|
|
169
|
+
const { cert, key } = generateCertificate({
|
|
170
|
+
commonName: 'my-app',
|
|
171
|
+
validityDays: 90,
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
// Generate and write to disk
|
|
175
|
+
const result = await generateCertificateFiles({
|
|
176
|
+
certPath: './certs/cert.pem',
|
|
177
|
+
keyPath: './certs/key.pem',
|
|
178
|
+
commonName: 'my-app',
|
|
179
|
+
});
|
|
77
180
|
```
|
|
78
181
|
|
|
182
|
+
## HTTPS Options Reference
|
|
183
|
+
|
|
184
|
+
| Option | Type | Default | Description |
|
|
185
|
+
|--------|------|---------|-------------|
|
|
186
|
+
| `cert` | string | — | PEM-encoded certificate string or file path |
|
|
187
|
+
| `key` | string | — | PEM-encoded private key string or file path |
|
|
188
|
+
| `autoGenerate` | boolean | `true` | Auto-generate a self-signed certificate when cert/key are not provided |
|
|
189
|
+
| `certificateOptions` | CertificateOptions | — | Options for the auto-generated certificate |
|
|
190
|
+
|
|
191
|
+
### Certificate Options
|
|
192
|
+
|
|
193
|
+
| Option | Type | Default | Description |
|
|
194
|
+
|--------|------|---------|-------------|
|
|
195
|
+
| `commonName` | string | `'localhost'` | Certificate subject Common Name (CN) |
|
|
196
|
+
| `altNames` | Array\<{ type, value }\> | `[dns:localhost, ip:127.0.0.1, ip:::1]` | Subject Alternative Names with type `'dns'` or `'ip'` |
|
|
197
|
+
| `validityDays` | number | `365` | Certificate validity period in days |
|
|
198
|
+
| `keySize` | number | `2048` | RSA key size in bits |
|
|
199
|
+
|
|
79
200
|
# Response Injection (Tap Feature)
|
|
80
201
|
|
|
81
202
|
The injection/tap feature allows you to "tap into" the request flow and inject custom responses for specific requests. This is particularly useful for:
|
|
@@ -467,6 +588,7 @@ new MockHttp(options?)
|
|
|
467
588
|
- `anything?`: boolean - Enable anything routes (default: true)
|
|
468
589
|
- `auth?`: boolean - Enable authentication routes (default: true)
|
|
469
590
|
- `images?`: boolean - Enable image routes (default: true)
|
|
591
|
+
- `https?`: boolean | HttpsOptions - Enable HTTPS with auto-generated or custom certificates (default: undefined/disabled)
|
|
470
592
|
- `hookOptions?`: HookifiedOptions - Hookified options
|
|
471
593
|
|
|
472
594
|
### Properties
|
|
@@ -479,6 +601,8 @@ new MockHttp(options?)
|
|
|
479
601
|
- `logging`: boolean - Get/set logging enabled state
|
|
480
602
|
- `rateLimit`: RateLimitPluginOptions | undefined - Get/set rate limiting options
|
|
481
603
|
- `httpBin`: HttpBinOptions - Get/set httpbin route options
|
|
604
|
+
- `https`: HttpsOptions | undefined - Get/set HTTPS configuration
|
|
605
|
+
- `isHttps`: boolean - Whether the server is running with HTTPS
|
|
482
606
|
- `server`: FastifyInstance - Get/set the Fastify server instance
|
|
483
607
|
- `taps`: TapManager - Get/set the TapManager instance
|
|
484
608
|
|
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,61 @@ import { RateLimitPluginOptions } from '@fastify/rate-limit';
|
|
|
2
2
|
import { FastifyRequest, FastifyInstance } from 'fastify';
|
|
3
3
|
import { Hookified, HookifiedOptions } from 'hookified';
|
|
4
4
|
|
|
5
|
+
type CertificateOptions = {
|
|
6
|
+
/**
|
|
7
|
+
* Common Name for the certificate subject. Defaults to 'localhost'.
|
|
8
|
+
*/
|
|
9
|
+
commonName?: string;
|
|
10
|
+
/**
|
|
11
|
+
* Subject Alternative Names. Defaults to [dns:localhost, ip:127.0.0.1, ip:::1].
|
|
12
|
+
*/
|
|
13
|
+
altNames?: Array<{
|
|
14
|
+
type: "dns";
|
|
15
|
+
value: string;
|
|
16
|
+
} | {
|
|
17
|
+
type: "ip";
|
|
18
|
+
value: string;
|
|
19
|
+
}>;
|
|
20
|
+
/**
|
|
21
|
+
* Number of days the certificate is valid. Defaults to 365.
|
|
22
|
+
*/
|
|
23
|
+
validityDays?: number;
|
|
24
|
+
/**
|
|
25
|
+
* RSA key size in bits. Defaults to 2048.
|
|
26
|
+
*/
|
|
27
|
+
keySize?: number;
|
|
28
|
+
};
|
|
29
|
+
type CertificateResult = {
|
|
30
|
+
/**
|
|
31
|
+
* PEM-encoded X.509 certificate.
|
|
32
|
+
*/
|
|
33
|
+
cert: string;
|
|
34
|
+
/**
|
|
35
|
+
* PEM-encoded PKCS#8 private key.
|
|
36
|
+
*/
|
|
37
|
+
key: string;
|
|
38
|
+
};
|
|
39
|
+
type CertificateFileOptions = CertificateOptions & {
|
|
40
|
+
/**
|
|
41
|
+
* File path to write the certificate PEM.
|
|
42
|
+
*/
|
|
43
|
+
certPath: string;
|
|
44
|
+
/**
|
|
45
|
+
* File path to write the private key PEM.
|
|
46
|
+
*/
|
|
47
|
+
keyPath: string;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Generate a self-signed certificate using only Node.js built-in crypto.
|
|
51
|
+
* Returns PEM-encoded certificate and private key strings.
|
|
52
|
+
*/
|
|
53
|
+
declare function generateCertificate(options?: CertificateOptions): CertificateResult;
|
|
54
|
+
/**
|
|
55
|
+
* Generate a self-signed certificate and write PEM files to disk.
|
|
56
|
+
* Returns the same CertificateResult as generateCertificate().
|
|
57
|
+
*/
|
|
58
|
+
declare function generateCertificateFiles(options: CertificateFileOptions): Promise<CertificateResult>;
|
|
59
|
+
|
|
5
60
|
/**
|
|
6
61
|
* Configuration for the injected response
|
|
7
62
|
*/
|
|
@@ -132,6 +187,25 @@ type HttpBinOptions = {
|
|
|
132
187
|
anything?: boolean;
|
|
133
188
|
auth?: boolean;
|
|
134
189
|
images?: boolean;
|
|
190
|
+
dynamicData?: boolean;
|
|
191
|
+
};
|
|
192
|
+
type HttpsOptions = {
|
|
193
|
+
/**
|
|
194
|
+
* PEM-encoded certificate string, or file path to a certificate PEM file.
|
|
195
|
+
*/
|
|
196
|
+
cert?: string;
|
|
197
|
+
/**
|
|
198
|
+
* PEM-encoded private key string, or file path to a private key PEM file.
|
|
199
|
+
*/
|
|
200
|
+
key?: string;
|
|
201
|
+
/**
|
|
202
|
+
* If true, auto-generate a self-signed certificate. Defaults to true when no cert/key provided.
|
|
203
|
+
*/
|
|
204
|
+
autoGenerate?: boolean;
|
|
205
|
+
/**
|
|
206
|
+
* Options for auto-generating a self-signed certificate. Only used when auto-generating.
|
|
207
|
+
*/
|
|
208
|
+
certificateOptions?: CertificateOptions;
|
|
135
209
|
};
|
|
136
210
|
type MockHttpOptions = {
|
|
137
211
|
/**
|
|
@@ -172,6 +246,11 @@ type MockHttpOptions = {
|
|
|
172
246
|
* Whether to enable logging. Defaults to true.
|
|
173
247
|
*/
|
|
174
248
|
logging?: boolean;
|
|
249
|
+
/**
|
|
250
|
+
* HTTPS configuration. Set to true to auto-generate a self-signed certificate,
|
|
251
|
+
* or provide an object with cert/key strings/paths or auto-generation options.
|
|
252
|
+
*/
|
|
253
|
+
https?: boolean | HttpsOptions;
|
|
175
254
|
};
|
|
176
255
|
declare class MockHttp extends Hookified {
|
|
177
256
|
private _port;
|
|
@@ -182,6 +261,8 @@ declare class MockHttp extends Hookified {
|
|
|
182
261
|
private _logging;
|
|
183
262
|
private _httpBin;
|
|
184
263
|
private _rateLimit?;
|
|
264
|
+
private _https;
|
|
265
|
+
private _httpsCredentials;
|
|
185
266
|
private _server;
|
|
186
267
|
private _taps;
|
|
187
268
|
constructor(options?: MockHttpOptions);
|
|
@@ -245,6 +326,20 @@ declare class MockHttp extends Hookified {
|
|
|
245
326
|
* @default true
|
|
246
327
|
*/
|
|
247
328
|
set logging(logging: boolean);
|
|
329
|
+
/**
|
|
330
|
+
* HTTPS configuration. Set to true to auto-generate a self-signed certificate,
|
|
331
|
+
* or provide an HttpsOptions object with cert/key or auto-generation options.
|
|
332
|
+
*/
|
|
333
|
+
get https(): HttpsOptions | undefined;
|
|
334
|
+
/**
|
|
335
|
+
* HTTPS configuration. Set to true to auto-generate a self-signed certificate,
|
|
336
|
+
* or provide an HttpsOptions object with cert/key or auto-generation options.
|
|
337
|
+
*/
|
|
338
|
+
set https(value: boolean | HttpsOptions | undefined);
|
|
339
|
+
/**
|
|
340
|
+
* Whether the server is running in HTTPS mode.
|
|
341
|
+
*/
|
|
342
|
+
get isHttps(): boolean;
|
|
248
343
|
/**
|
|
249
344
|
* HTTP Bin options. Defaults to all enabled.
|
|
250
345
|
*/
|
|
@@ -347,8 +442,15 @@ declare class MockHttp extends Hookified {
|
|
|
347
442
|
* @param fastifyInstance - the server instance to register the routes on.
|
|
348
443
|
*/
|
|
349
444
|
registerImageRoutes(fastifyInstance?: FastifyInstance): Promise<void>;
|
|
445
|
+
/**
|
|
446
|
+
* Register the dynamic data routes.
|
|
447
|
+
* @param fastifyInstance - the server instance to register the routes on.
|
|
448
|
+
*/
|
|
449
|
+
registerDynamicDataRoutes(fastifyInstance?: FastifyInstance): Promise<void>;
|
|
450
|
+
private resolveHttpsCredentials;
|
|
451
|
+
private loadPemValue;
|
|
350
452
|
}
|
|
351
453
|
|
|
352
454
|
declare const start: () => Promise<MockHttp>;
|
|
353
455
|
|
|
354
|
-
export { type InjectionMatcher, type InjectionResponse, type InjectionTap, MockHttp as default, MockHttp as mockhttp, start };
|
|
456
|
+
export { type CertificateFileOptions, type CertificateOptions, type CertificateResult, type HttpsOptions, type InjectionMatcher, type InjectionResponse, type InjectionTap, MockHttp as default, generateCertificate, generateCertificateFiles, MockHttp as mockhttp, start };
|