MCPcopy Create free account
hub / github.com/mitmproxy/mitmproxy / test_redirect

Function test_redirect

test/mitmproxy/proxy/layers/http/test_http.py:128–177  ·  view source on GitHub ↗

Test redirects between http:// and https:// in regular proxy mode.

(strategy, https_server, https_client, tctx, monkeypatch)

Source from the content-addressed store, hash-verified

126@pytest.mark.parametrize("https_server", [False, True])
127@pytest.mark.parametrize("strategy", ["lazy", "eager"])
128def test_redirect(strategy, https_server, https_client, tctx, monkeypatch):
129 """Test redirects between http:// and https:// in regular proxy mode."""
130 server = Placeholder(Server)
131 flow = Placeholder(HTTPFlow)
132 tctx.options.connection_strategy = strategy
133 p = Playbook(http.HttpLayer(tctx, HTTPMode.regular), hooks=False)
134
135 if https_server:
136 monkeypatch.setattr(tls, "ServerTLSLayer", tls.MockTLSLayer)
137
138 def redirect(flow: HTTPFlow):
139 if https_server:
140 flow.request.url = "https://redirected.site/"
141 else:
142 flow.request.url = "http://redirected.site/"
143
144 if https_client:
145 p >> DataReceived(
146 tctx.client,
147 b"CONNECT example.com:80 HTTP/1.1\r\nHost: example.com:80\r\n\r\n",
148 )
149 if strategy == "eager":
150 p << OpenConnection(Placeholder())
151 p >> reply(None)
152 p << SendData(tctx.client, b"HTTP/1.1 200 Connection established\r\n\r\n")
153 p >> DataReceived(tctx.client, b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")
154 p << layer.NextLayerHook(Placeholder())
155 p >> reply_next_layer(lambda ctx: http.HttpLayer(ctx, HTTPMode.transparent))
156 else:
157 p >> DataReceived(
158 tctx.client,
159 b"GET http://example.com/ HTTP/1.1\r\nHost: example.com\r\n\r\n",
160 )
161 p << http.HttpRequestHook(flow)
162 p >> reply(side_effect=redirect)
163 p << OpenConnection(server)
164 p >> reply(None)
165 p << SendData(server, b"GET / HTTP/1.1\r\nHost: redirected.site\r\n\r\n")
166 p >> DataReceived(
167 server, b"HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello World!"
168 )
169 p << SendData(
170 tctx.client, b"HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello World!"
171 )
172
173 assert p
174 if https_server:
175 assert server().address == ("redirected.site", 443)
176 else:
177 assert server().address == ("redirected.site", 80)
178
179
180def test_multiple_server_connections(tctx):

Callers

nothing calls this directly

Calls 7

PlaceholderFunction · 0.90
PlaybookClass · 0.90
DataReceivedClass · 0.90
OpenConnectionClass · 0.90
replyClass · 0.90
SendDataClass · 0.90
reply_next_layerFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…