diff --git a/internal/deploy/deploy.go b/internal/deploy/deploy.go index 5acf223..eedeb46 100644 --- a/internal/deploy/deploy.go +++ b/internal/deploy/deploy.go @@ -32,7 +32,7 @@ type SlidingSyncDeployment struct { slidingSync testcontainers.Container reverseProxy testcontainers.Container slidingSyncURL string - controllerURL string + mitmClient *http.Client proxyURLToHS map[string]string mu sync.RWMutex tcpdump *exec.Cmd @@ -47,26 +47,18 @@ func (d *SlidingSyncDeployment) SlidingSyncURL(t *testing.T) string { return d.slidingSyncURL } -func (d *SlidingSyncDeployment) SetMITMOptions(t *testing.T, options map[string]string) { +func (d *SlidingSyncDeployment) SetMITMOptions(t *testing.T, options map[string]interface{}) { t.Helper() - proxyURL, err := url.Parse(d.controllerURL) - must.NotError(t, "failed to parse controller URL", err) - cli := &http.Client{ - Timeout: 5 * time.Second, - Transport: &http.Transport{ - Proxy: http.ProxyURL(proxyURL), - }, - } jsonBody, err := json.Marshal(map[string]interface{}{ "options": options, }) must.NotError(t, "failed to marshal options", err) - req, err := http.NewRequest("POST", magicMITMURL, bytes.NewBuffer(jsonBody)) + req, err := http.NewRequest("POST", magicMITMURL+"/options", bytes.NewBuffer(jsonBody)) must.NotError(t, "failed to prepare request", err) req.Header.Set("Content-Type", "application/json") - res, err := cli.Do(req) + res, err := d.mitmClient.Do(req) must.NotError(t, "failed to do request", err) - must.Equal(t, 200, res.StatusCode, "controller returned wrong HTTP status") + must.Equal(t, res.StatusCode, 200, "controller returned wrong HTTP status") } func (d *SlidingSyncDeployment) ReverseProxyURLForHS(hsName string) string { @@ -231,6 +223,8 @@ func RunNewDeployment(t *testing.T, shouldTCPDump bool) *SlidingSyncDeployment { // TODO needs sudo t.Logf("Started tcpdumping: PID %d", cmd.Process.Pid) } + proxyURL, err := url.Parse(controllerURL) + must.NotError(t, "failed to parse controller URL", err) return &SlidingSyncDeployment{ Deployment: deployment, slidingSync: ssContainer, @@ -238,7 +232,12 @@ func RunNewDeployment(t *testing.T, shouldTCPDump bool) *SlidingSyncDeployment { reverseProxy: mitmproxyContainer, slidingSyncURL: ssURL, tcpdump: cmd, - controllerURL: controllerURL, + mitmClient: &http.Client{ + Timeout: 5 * time.Second, + Transport: &http.Transport{ + Proxy: http.ProxyURL(proxyURL), + }, + }, proxyURLToHS: map[string]string{ "hs1": rpHS1URL, "hs2": rpHS2URL, diff --git a/tests/addons/__init__.py b/tests/addons/__init__.py index b4c14c6..43b54d9 100644 --- a/tests/addons/__init__.py +++ b/tests/addons/__init__.py @@ -7,13 +7,12 @@ app = Flask("mitmoptset") -@app.route("/", methods=["POST"]) +@app.route("/options", methods=["POST"]) def set_options() -> str: body = request.json options = body.get("options", {}) print(f"setting options {options}") - for k, v in options: - ctx.options[k] = v + ctx.options.update(**options) return {} addons = [ diff --git a/tests/addons/status_code.py b/tests/addons/status_code.py index 955cc5b..7f8c138 100644 --- a/tests/addons/status_code.py +++ b/tests/addons/status_code.py @@ -26,6 +26,8 @@ def configure(self, updates): logging.info(f"statuscode will return HTTP {self.return_status_code}") def response(self, flow): + if not flow.request.pretty_host.startswith("hs"): + return # ignore responses sent by the controller if self.return_status_code == 0: - return + return # ignore responses if we aren't told a code flow.response = Response.make(self.return_status_code)