Skip to main content

9C2.Request Record/Replay

trydoforOriginalPracticeTestingAbout 1 min

9C2.Request Record/Replay

In the product environment, there is a need to log and replay certain http requests. Simple and easy way is Nginx's mirror replication to GoReplay or wiremock.

The reference is as follows, this manual uses mirror WeChat requests to mocklab service and Gor local server as an example,

9C2.1.Mocklab Service

Use github to apply for a free account and get the URLs and Tokens of the Mock APIs, such as The curl in Settings/Usage examples has the appropriate fields, with the following features

Finally, you can get all the requests in the Request Log and Convert to stub for testing purposes.

9C2.2.Gor Local Server

wget https://github.com/buger/goreplay/releases/download/1.3.3/gor_1.3.3_x64.tar.gz
tar -xzf gor_1.3.3_x64.tar.gz
# Open and listen on 18081, if attaching to another service port, don't need http-pprof
tee gor.sh << "EOF"
#!/bin/bash
gor_file=weixin-%Y%m%d.gor
cur_file=$(date +$gor_file)
test -f "$cur_file" && mv $cur_file $cur_file.$(date +%H%M%S)
./gor --http-pprof :18081 \
--input-raw :18081 \
--output-file $gor_file \
--output-file-append
EOF
chmod +x gor.sh
sudo nohup ./gor.sh &
# test
curl http://127.0.0.1:18081/debug/pprof/

Note that gor 2.0 is still in the rc stage, and there are some issues in the 1.3 live test, such as when saving files https://github.com/buger/goreplay/blob/master/output_file.goopen in new window

  • output-file-append is invalid, restarting gor will overwrite gor file, should backup if needed
  • output-file does not flush automatically, only buff full or close gor
  • output-stdout is recommended if you need to watch in real time

9C2.3.Nginx Mirror

The ngx_http_mirror_module module (1.13.4) implements mirroring of an original request by creating background mirror subrequests. Responses to mirror subrequests are ignored.

location /weixin/geteway.api {
    mirror /__mirror_mock;
    mirror /__mirror_gor;
    proxy_pass http://xxxx;
}

location = /__mirror_mock {
    internal;
    resolver 8.8.8.8;
    proxy_pass https://xxx.mocklab.io$request_uri;
    proxy_set_header Authorization "Token 14exxxxxxxba5";
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-Host $host;
}

location = /__mirror_gor {
    internal;
    proxy_pass http://127.0.0.1:18081$request_uri;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-Host $host;
}

9C2.4.Usage Suggestion

One of gor or mocklab is enough, depending on the situation, mocklab is recommended for dev environment, it is easy to generate test.

If mirror does not forward, you can check the nginx error log, and the resolver 8.8.8.8 in the above example solves the following problem.

no resolver defined to resolve xxx.mocklab.io, request: "POST ..."