diff --git a/.gitignore b/.gitignore index 2116329..c950518 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ js-sdk/node_modules js-sdk/dist internal/api/dist +__pycache__ \ No newline at end of file diff --git a/internal/deploy/deploy.go b/internal/deploy/deploy.go index 773ccfc..e62ae9f 100644 --- a/internal/deploy/deploy.go +++ b/internal/deploy/deploy.go @@ -8,6 +8,7 @@ import ( "net/url" "os" "os/exec" + "path/filepath" "sync" "testing" "time" @@ -88,7 +89,15 @@ func RunNewDeployment(t *testing.T, shouldTCPDump bool) *SlidingSyncDeployment { deployment := complement.Deploy(t, 2) networkName := deployment.Network() - // make a reverse proxy. + workingDir, err := os.Getwd() + if err != nil { + t.Fatalf("failed to find working directory: %s", err) + } + + // Make the mitmproxy and hardcode CONTAINER PORTS for hs1/hs2. HOST PORTS are still dynamically allocated. + // By running this container on the same network as the homeservers, we can leverage DNS hence hs1/hs2 URLs. + // We also need to preload addons into the proxy, so we bind mount the addons directory. This also allows + // test authors to easily add custom addons. hs1ExposedPort := "3000/tcp" hs2ExposedPort := "3001/tcp" mitmproxyContainer, err := testcontainers.GenericContainer(context.Background(), testcontainers.GenericContainerRequest{ @@ -97,13 +106,16 @@ func RunNewDeployment(t *testing.T, shouldTCPDump bool) *SlidingSyncDeployment { ExposedPorts: []string{hs1ExposedPort, hs2ExposedPort}, Env: map[string]string{}, Cmd: []string{ - "mitmdump", "--mode", "reverse:http://hs1:8008@3000", "--mode", "reverse:http://hs2:8008@3001", + "mitmdump", "--mode", "reverse:http://hs1:8008@3000", "--mode", "reverse:http://hs2:8008@3001", "-s", "/addons/__init__.py", }, // WaitingFor: wait.ForLog("listening"), Networks: []string{networkName}, NetworkAliases: map[string][]string{ networkName: {"mitmproxy"}, }, + Mounts: testcontainers.Mounts( + testcontainers.BindMount(filepath.Join(workingDir, "addons"), "/addons"), + ), }, Started: true, }) diff --git a/tests/addons/__init__.py b/tests/addons/__init__.py new file mode 100644 index 0000000..feb1d2e --- /dev/null +++ b/tests/addons/__init__.py @@ -0,0 +1,4 @@ +from add_header import AddHeader + + +addons = [AddHeader()] diff --git a/tests/addons/add_header.py b/tests/addons/add_header.py new file mode 100644 index 0000000..13c792f --- /dev/null +++ b/tests/addons/add_header.py @@ -0,0 +1,9 @@ +class AddHeader: + def __init__(self): + self.num = 0 + + def response(self, flow): + self.num = self.num + 1 + print("got response num", self.num) + flow.response.headers["count"] = str(self.num) + \ No newline at end of file