I have set up mockttp to intercept HTTPS requests and do some logging, with a rule like this:
import * as mockttp from 'mockttp'
const https = await mockttp.generateCACertificate()
const caFingerprint = mockttp.generateSPKIFingerprint(https.cert)
const server = mockttp.getLocal({ https })
await server.forAnyRequest().matching(request => doesMatchCriteria(request)).thenPassThrough({
beforeRequest: request => doSomethingWithRequest(request),
beforeResponse: response => doSomethingWithResponse(response)
})
This is all working as expected to point my browser at this proxy server with the trusted caFingerprint
value. Requests are getting passed through and beforeRequest
and beforeResponse
are getting called.
I would also like to be able to make HTTP requests to the proxy server itself while it is running to dynamically configure the behavior of the logging, but I am struggling to define a rule to catch all such requests.
I have tried defining individual rules like this:
await server.forGet(`/some/path`).forPort(server.port).always().thenCallback(callbackFn)
With this rule, any GET
request to /some/path
on the port mockttp is running on does get handled by this. But I would like to be able to capture all requests, not just particular paths.
I could do something like this:
await server.forAnyRequest().forPort(server.port).always().thenCallback(callback)
But this rule would handle all requests on the same port, even if they were intended for a different server. For example, if it is port 8000, then requests to example.com:8000 would also be handled by this, even though I only want to handle requests to the proxy server itself.
I have noticed when the server is running on port 8000, and I make a request to e.g. localhost:8000/some/path/not/defined/by/a/rule, I get this error:
Failed to handle request: Passthrough loop detected. This probably means you're sending a request directly to a passthrough endpoint, which is forwarding it to the target URL, which is a passthrough endpoint, which is forwarding it to the target URL, which is a passthrough endpoint...
I'd love to define a rule that would handle all requests where a passthrough loop is detected so I can handle all requests made to e.g. localhost:8000 or 127.0.0.1:8000 or my-mockttp-proxy-server:8000. Is there a way to do this?
Alternatively, I'm considering starting a separate http server on a different port to handle these additional endpoints. Would that be a better solution?