How the proxy works
When you configure your application to route traffic through MockServer, MockServer forwards each request to the real upstream service and returns the response to the caller. At the same time it stores a copy of the request and response internally as a recorded expectation. You can then:- Inspect the recorded traffic to understand what your application actually sends.
- Verify that specific requests were made — or were not made — during a test run.
- Replay the recorded expectations as mock expectations so your tests no longer depend on the real service.
Proxy modes
MockServer supports four proxy modes. Choose the one that fits your client and network setup.Web proxy (HTTP)
Each request is forwarded dynamically using its
Host header. Configure your HTTP client to use MockServer as an HTTP proxy.Secure web proxy (HTTPS tunneling)
Requests are forwarded using an HTTP
CONNECT request that establishes an HTTP tunnel. MockServer auto-generates an SSL certificate so encrypted HTTPS traffic is recorded transparently.SOCKS proxy
Operates at the socket level using a SOCKS
CONNECT command. You can configure a single client, an entire JVM, or the whole OS to route traffic through it. SSL traffic is also recorded transparently.Port forwarding
All requests arriving on a specific local port are forwarded to a fixed remote host and port. No code changes required — just redirect traffic at the network or configuration level.
Port forwarding and SOCKS proxying are the most transparent modes and typically require no code changes. Web proxy and secure web proxy modes give you more control: only the clients you explicitly configure are proxied, and you can proxy multiple services simultaneously.
Configure your application to use the proxy
JVM system properties
The simplest way to route all JVM HTTP traffic through MockServer is to set the standard Java proxy system properties at startup:HttpURLConnection, Apache HttpClient, and Spring RestTemplate.
Java HTTP clients
- HttpURLConnection
- Apache HttpClient
- Spring RestTemplate
- Spring WebClient
- Jersey Client
SOCKS proxy — JVM-wide configuration
To route all outbound connections from an entire JVM through the SOCKS proxy, override the defaultProxySelector:
Port forwarding
Port forwarding requires no code changes in your application. Instead, configure MockServer with the remote host and port to forward to:serverPort— the local port MockServer listens on.proxyRemotePort— the port on the remote service to forward requests to.proxyRemoteHost— the hostname or IP of the remote service (defaults tolocalhostif not set).
localhost:<serverPort> — or add an /etc/hosts entry mapping the real service hostname to localhost — and MockServer forwards all traffic to the configured remote endpoint.
SSL traffic inspection
For HTTPS traffic, MockServer auto-generates a trusted SSL certificate per connection. This lets it decrypt, record, and re-encrypt traffic transparently — no changes to your application needed. Make sure your client trusts the MockServer CA certificate, or disable SSL verification in your test environment.Recorded traffic and verification
Every request proxied through MockServer is stored internally. After your application runs, you can:- Retrieve recorded expectations to review or convert them into mock expectations.
- Verify that specific requests were received, including proxy-forwarded requests.