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
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)
> **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';
| `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:
0 commit comments