Skip to content

Commit

Permalink
various bugfixes to get status code options working
Browse files Browse the repository at this point in the history
  • Loading branch information
kegsay committed Nov 30, 2023
1 parent b3d070d commit b74e978
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
27 changes: 13 additions & 14 deletions internal/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -231,14 +223,21 @@ 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,
postgres: postgresContainer,
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,
Expand Down
5 changes: 2 additions & 3 deletions tests/addons/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
4 changes: 3 additions & 1 deletion tests/addons/status_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit b74e978

Please sign in to comment.