Why are only top-level requests proxied when configuring http clients such as postman or K6

  docker, integration-testing, k6, postman, proxy

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

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 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 affect 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 behaviour 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)

Source: Docker Questions

LEAVE A COMMENT