Skip to content

Commit 720ec52

Browse files
authored
Merge pull request #125 from jaredwray/feat-adding-in-https-support-in-docs
feat: adding in https support in docs
2 parents 05dc3e3 + 5fec886 commit 720ec52

1 file changed

Lines changed: 127 additions & 3 deletions

File tree

README.md

Lines changed: 127 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ A simple HTTP server that can be used to mock HTTP responses for testing purpose
2525
- [Deploy via Docker](#deploy-via-docker)
2626
- [Deploy via Docker Compose](#deploy-via-docker-compose)
2727
- [Deploy via NodeJS](#deploy-via-nodejs)
28+
- [HTTPS Support](#https-support)
2829
- [Response Injection (Tap Feature)](#response-injection-tap-feature)
2930
- [Rate Limiting](#rate-limiting)
3031
- [Logging](#logging)
@@ -69,13 +70,133 @@ npm install @jaredwray/mockhttp --save
6970
then run `mockhttp` in your code.
7071

7172
```javascript
72-
import { mockhttp } from '@jaredwray/mockhttp';
73-
await mockhttp.start(); // start the server
73+
import { MockHttp } from '@jaredwray/mockhttp';
74+
const mock = new MockHttp();
75+
await mock.start(); // start the server
7476
const response = await fetch('http://localhost:3000/get');
7577
console.log(response);
76-
await mockhttp.stop(); // stop the server
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+
});
77180
```
78181

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+
79200
# Response Injection (Tap Feature)
80201

81202
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?)
467588
- `anything?`: boolean - Enable anything routes (default: true)
468589
- `auth?`: boolean - Enable authentication routes (default: true)
469590
- `images?`: boolean - Enable image routes (default: true)
591+
- `https?`: boolean | HttpsOptions - Enable HTTPS with auto-generated or custom certificates (default: undefined/disabled)
470592
- `hookOptions?`: HookifiedOptions - Hookified options
471593
472594
### Properties
@@ -479,6 +601,8 @@ new MockHttp(options?)
479601
- `logging`: boolean - Get/set logging enabled state
480602
- `rateLimit`: RateLimitPluginOptions | undefined - Get/set rate limiting options
481603
- `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
482606
- `server`: FastifyInstance - Get/set the Fastify server instance
483607
- `taps`: TapManager - Get/set the TapManager instance
484608

0 commit comments

Comments
 (0)