The Goal
- Run integration tests but stub sub-requests to a specific domain that occur within the API code itself
- Ideally, do this via docker-compose because I want to be able to run this in Circle CI or Jenkins
The attempt
I'm using the mockttp
library as outlined here to create a proxy server that allows all traffic to pass through untouched with the exception of one specific domain where I will have a set response.
The proxy server, API, and K6 service are spun up using docker-compose, and the HTTP(S)_PROXY
variables are set in the K6 service.
The problem
Direct requests are proxied but requests instigated by those requests are not. For example, if I make a request for the specific domain I am attempting to a stub, the proxy does its job. But that domain isn't called directly, it is part of the API code and the proxy has no effect at this level.
If I spin up the proxy server and configure my laptop to use the proxy server, everything works correctly. However, if I configure Postman or K6 to use the proxy server it doesn't.
docker-compose.yml
version: '3.7'
services:
service:
build:
context: ./service
container_name: service
ports:
- '3000:3000'
environment:
- NODE_TLS_REJECT_UNAUTHORIZED=0
proxy:
build:
context: ./
container_name: proxy
ports:
- '8002:8002'
environment:
- BASE_URL=service:3000
k6:
image: loadimpact/k6:latest
container_name: k6
depends_on:
- service
- proxy
volumes:
- ./service/test/:/perf
environment:
- HTTPS_PROXY=https://proxy:8002
- HTTP_PROXY=http://proxy:8002
- BASE_URL=http://service:3000/foo
- NODE_TLS_REJECT_UNAUTHORIZED=0
- K6_HTTP_DEBUG=true
entrypoint: ['k6', 'run', '--insecure-skip-tls-verify', '/perf/test.js']
I'm sure I'm missing something obvious but networking is not my strong point. My understanding is that if I tell something to use a proxy, all traffic should go through the proxy. I am finding that if I set the proxy in my computer's settings, this is indeed the case. But if I try this by configuring K6 or even Postman, the behavior is different.
What am I doing wrong?
Proxy server code for reference
(async () => {
const server = await require('mockttp').getLocal();
server.enableDebug();
await server.anyRequest().forHost('hostiwanttostub').always().thenReply(201);
await server.anyRequest().always().thenForwardTo(process.env.BASE_URL);
await server.start(8002);
// Print out the server details:
console.log(`Server running on port ${server.port}`);
})(); // (Run in an async wrapper so we can use top-level await everywhere)