Skip to content

TLS/HTTPS Configuration

mockd supports HTTPS for both the mock server and proxy modes. This guide covers certificate generation, configuration, and common use cases.

Enable HTTPS with auto-generated certificates:

Terminal window
mockd start --config mocks.json --https

mockd generates a self-signed certificate and starts on port 8443.

Terminal window
mockd start --config mocks.json --https --port 443
Terminal window
mockd start --config mocks.json \
--cert ./certs/server.crt \
--key ./certs/server.key
{
"server": {
"port": 8443,
"tls": {
"enabled": true,
"certFile": "./certs/server.crt",
"keyFile": "./certs/server.key"
}
},
"mocks": [...]
}

Generate a self-signed certificate:

Terminal window
mockd cert generate --name localhost --days 365

Output:

Generated certificate: ./certs/localhost.crt
Generated private key: ./certs/localhost.key

With Subject Alternative Names:

Terminal window
mockd cert generate \
--name localhost \
--san "127.0.0.1" \
--san "::1" \
--san "myapp.local"

Generate a CA for MITM proxying:

Terminal window
mockd cert generate-ca --name "mockd CA" --days 3650

Output:

Generated CA certificate: ./certs/mockd-ca.crt
Generated CA private key: ./certs/mockd-ca.key

For the proxy to intercept HTTPS traffic, clients must trust the mockd CA.

  1. Generate CA Certificate:
Terminal window
mockd cert generate-ca
  1. Start Proxy:
Terminal window
mockd proxy --target https://api.example.com \
--ca-cert ./certs/mockd-ca.crt \
--ca-key ./certs/mockd-ca.key
  1. Install CA on Client:

See Installing CA Certificates below.

{
"proxy": {
"target": "https://api.example.com",
"tls": {
"caCertFile": "./certs/mockd-ca.crt",
"caKeyFile": "./certs/mockd-ca.key",
"certCacheDir": "./certs/generated"
}
}
}
FieldDescription
caCertFileCA certificate for signing
caKeyFileCA private key
certCacheDirCache for generated certs
Terminal window
# Add to system keychain
sudo security add-trusted-cert -d -r trustRoot \
-k /Library/Keychains/System.keychain \
./certs/mockd-ca.crt
# Or for current user only
security add-trusted-cert -r trustRoot \
-k ~/Library/Keychains/login.keychain \
./certs/mockd-ca.crt
Terminal window
# Copy certificate
sudo cp ./certs/mockd-ca.crt /usr/local/share/ca-certificates/mockd-ca.crt
# Update certificate store
sudo update-ca-certificates
Terminal window
# Import to Trusted Root Certification Authorities
Import-Certificate -FilePath .\certs\mockd-ca.crt `
-CertStoreLocation Cert:\LocalMachine\Root
Terminal window
export NODE_EXTRA_CA_CERTS=./certs/mockd-ca.crt
node app.js
import requests
requests.get('https://localhost:8443', verify='./certs/mockd-ca.crt')
Terminal window
curl --cacert ./certs/mockd-ca.crt https://localhost:8443/api/users

Mount the CA certificate:

Terminal window
docker run -v $(pwd)/certs/mockd-ca.crt:/etc/ssl/certs/mockd-ca.crt \
myapp
{
"server": {
"tls": {
"enabled": true,
"minVersion": "1.2"
}
}
}

Supported: 1.0, 1.1, 1.2, 1.3

{
"server": {
"tls": {
"enabled": true,
"cipherSuites": [
"TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256"
]
}
}
}

Require client certificates:

{
"server": {
"tls": {
"enabled": true,
"certFile": "./certs/server.crt",
"keyFile": "./certs/server.key",
"clientAuth": "require",
"clientCAs": ["./certs/client-ca.crt"]
}
}
}

Client auth modes:

  • none - No client cert required
  • request - Request but don’t require
  • require - Require valid client cert

Serve both protocols:

{
"server": {
"port": 4280,
"tls": {
"enabled": true,
"port": 8443,
"certFile": "./certs/server.crt",
"keyFile": "./certs/server.key"
}
}
}

Both endpoints serve the same mocks:

  • http://localhost:4280
  • https://localhost:8443

Redirect HTTP to HTTPS:

{
"server": {
"port": 4280,
"httpsRedirect": true,
"tls": {
"enabled": true,
"port": 8443
}
}
}

Symptom: CERT_AUTHORITY_INVALID or similar errors

Solution: Install the CA certificate as described above, or use --insecure flags for testing:

Terminal window
curl -k https://localhost:8443/api/users

Symptom: HOSTNAME_MISMATCH error

Solution: Generate certificate with correct SANs:

Terminal window
mockd cert generate --name localhost --san "myapp.local"

Symptom: CERT_HAS_EXPIRED error

Solution: Regenerate with longer validity:

Terminal window
mockd cert generate --name localhost --days 3650

Symptom: Cannot bind to port 443

Solution: Use a high port or grant capability:

Terminal window
# Use high port
mockd start --https --port 8443
# Or grant capability (Linux)
sudo setcap 'cap_net_bind_service=+ep' $(which mockd)
  1. Never use self-signed certs in production - They’re for development only

  2. Protect private keys - Restrict file permissions:

    Terminal window
    chmod 600 ./certs/*.key
  3. Short-lived certificates - Use shorter validity for development certs

  4. Don’t commit certs - Add to .gitignore:

    certs/
    *.crt
    *.key
    *.pem