bbyars / mountebank

Over the wire test doubles
http://www.mbtest.org
MIT License
2k stars 268 forks source link

mb replay not removing proxy as expected #675

Closed mmcmurray-lw closed 2 years ago

mmcmurray-lw commented 2 years ago

Expected behaviour

I start mb using the following command:

mb start --datadir /home/mountebank --configfile /app/proxy-imposters.json --loglevel debug

Where the data dir is empty and proxy-imposters.json is:

  "imposters": [
    {
      "port": 3001,
      "protocol": "http",
      "stubs": [
        {
          "responses": [
            {
              "proxy": {
                "to": "http://my-service:8080",
                "mode": "proxyAlways",
                "predicateGenerators": [
                  {
                    "matches": {
                      "method": true,
                      "path": true
                    },
                    "caseSensitive": true
                  }]}}]}]}]}

The imposter works as expected, allowing me to run some tests and record the traffic. I then use the following command: mb replay --port 3001

I get the following result back:

{
  port: 3001,
  log: { level: 'info', transports: { console: [Object], file: [Object] } },
  removeProxies: true
}

Given this result I would expect the proxy to have been removed and the imposter to be purely in replay mode.

Actual behaviour

Once the command is called I can see in debug logs GET /imposters?replayable=true&removeProxies=true is passed on through the proxying imposter to the service I am trying to mock, which as expected returns a 404 not found message which is then added to the predicate file.

Future requests sent to the imposter continue to be proxied to my-service:8080 as the proxyAlways stub is still present in the datadir. I can manually remove this stub to achieve my desired result but I would like to be able to do it in an automated fashion.

Steps to reproduce

Software versions used

OS         : macOS 12.0
mountebank : docker image - bbyars/mountebank:2.6.0
Installation method : Running docker container

Log contents in mb.log when running mb --loglevel debug

debug: [http:3001] ::ffff:192.168.176.7:48990 ESTABLISHED
info: [http:3001] ::ffff:192.168.176.7:48990 => GET /imposters?replayable=true&removeProxies=true
debug: [http:3001] ::ffff:192.168.176.7:48990 => {"requestFrom":"::ffff:192.168.176.7:48990","method":"GET","path":"/imposters","query":{"replayable":"true","removeProxies":"true"},"headers":{"Content-Type":"application/json","Connection":"close","Host":"mountebank:3001"},"body":"","ip":"::ffff:192.168.176.7"}
debug: [http:3001] using predicate match: {}
debug: [mb:2525] Acquired file lock on /home/mountebank/3001/stubs/1649083425990-1-1/meta.json for nextResponse-52 after 0.0066805s
debug: [mb:2525] Released file lock on /home/mountebank/3001/stubs/1649083425990-1-1/meta.json for nextResponse-52 after 0.0122398s
debug: [http:3001] generating response from {"proxy":{"to":"http://my-service:8080","mode":"proxyAlways","predicateGenerators":[{"matches":{"method":true,"path":true},"caseSensitive":true}]}}
debug: [http:3001] Proxy ::ffff:192.168.176.7:48990 => {"requestFrom":"::ffff:192.168.176.7:48990","method":"GET","path":"/imposters","query":{"replayable":"true","removeProxies":"true"},"headers":{"Content-Type":"application/json","Connection":"close","Host":"mountebank:3001"},"body":"","ip":"::ffff:192.168.176.7"} => http://my-service:8080
debug: [http:3001] Proxy ::ffff:192.168.176.7:48990 <= {"statusCode":404,"headers":{"Connection":"close","Date":"Mon, 04 Apr 2022 15:13:14 GMT","Content-Type":"application/json","Content-Length":"43"},"body":"{\"code\":404,\"message\":\"HTTP 404 Not Found\"}","_mode":"text"} <= http://my-service:8080
debug: [mb:2525] Acquired file lock on /home/mountebank/3001/stubs/1649084163700-1-35/meta.json for addResponse-53 after 0.006353s
debug: [mb:2525] Released file lock on /home/mountebank/3001/stubs/1649084163700-1-35/meta.json for addResponse-53 after 0.0378336s
debug: [http:3001] ::ffff:192.168.176.7:48990 <= {"statusCode":404,"headers":{"Connection":"close","Date":"Mon, 04 Apr 2022 15:13:14 GMT","Content-Type":"application/json","Content-Length":43},"body":"{\"code\":404,\"message\":\"HTTP 404 Not Found\"}","_mode":"text"}
debug: [http:3001] ::ffff:192.168.176.7:48990 CLOSED
mmcmurray-lw commented 2 years ago

On re-read of the docs I can see this is my mistake and I can see why I made it.

From the mbtest docs: http://www.mbtest.org/docs/commandLine#replay

The replay command is a convenience that removes all proxies, effectively switching from record mode to replay mode. Assuming mountebank is running on port 3000, you would run the following command:

mb replay --port 3000

In the book 3000 is used as the assumed imposter port.

So cross referencing between mbtest and the book I confused 3000 on mbtest as the imposter port rather than mountebank port.