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

Function test_upstream_proxy

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

Test that an upstream HTTP proxy is used.

(tctx, redirect, domain, scheme)

Source from the content-addressed store, hash-verified

812@pytest.mark.parametrize("domain", [b"example.com", b"xn--eckwd4c7c.xn--zckzah"])
813@pytest.mark.parametrize("scheme", ["http", "https"])
814def test_upstream_proxy(tctx, redirect, domain, scheme):
815 """Test that an upstream HTTP proxy is used."""
816 server = Placeholder(Server)
817 server2 = Placeholder(Server)
818 flow = Placeholder(HTTPFlow)
819 tctx.client.proxy_mode = ProxyMode.parse("upstream:http://proxy:8080")
820 playbook = Playbook(http.HttpLayer(tctx, HTTPMode.upstream), hooks=False)
821
822 if scheme == "http":
823 assert (
824 playbook
825 >> DataReceived(
826 tctx.client,
827 b"GET http://%s/ HTTP/1.1\r\nHost: %s\r\n\r\n" % (domain, domain),
828 )
829 << OpenConnection(server)
830 >> reply(None)
831 << SendData(
832 server,
833 b"GET http://%s/ HTTP/1.1\r\nHost: %s\r\n\r\n" % (domain, domain),
834 )
835 )
836
837 else:
838 assert (
839 playbook
840 >> DataReceived(
841 tctx.client,
842 b"CONNECT %s:443 HTTP/1.1\r\nHost: %s:443\r\n\r\n" % (domain, domain),
843 )
844 << SendData(tctx.client, b"HTTP/1.1 200 Connection established\r\n\r\n")
845 >> DataReceived(tctx.client, b"GET / HTTP/1.1\r\nHost: %s\r\n\r\n" % domain)
846 << layer.NextLayerHook(Placeholder())
847 >> reply_next_layer(lambda ctx: http.HttpLayer(ctx, HTTPMode.transparent))
848 << OpenConnection(server)
849 >> reply(None)
850 << SendData(
851 server,
852 b"CONNECT %s:443 HTTP/1.1\r\nHost: %s:443\r\n\r\n" % (domain, domain),
853 )
854 >> DataReceived(server, b"HTTP/1.1 200 Connection established\r\n\r\n")
855 << SendData(server, b"GET / HTTP/1.1\r\nHost: %s\r\n\r\n" % domain)
856 )
857
858 playbook >> DataReceived(server, b"HTTP/1.1 418 OK\r\nContent-Length: 0\r\n\r\n")
859 playbook << SendData(tctx.client, b"HTTP/1.1 418 OK\r\nContent-Length: 0\r\n\r\n")
860
861 assert playbook
862 assert server().address == ("proxy", 8080)
863
864 if scheme == "http":
865 playbook >> DataReceived(
866 tctx.client,
867 b"GET http://%s/two HTTP/1.1\r\nHost: %s\r\n\r\n" % (domain, domain),
868 )
869 else:
870 playbook >> DataReceived(
871 tctx.client, b"GET /two HTTP/1.1\r\nHost: %s\r\n\r\n" % domain

Callers

nothing calls this directly

Calls 11

PlaceholderFunction · 0.90
PlaybookClass · 0.90
DataReceivedClass · 0.90
OpenConnectionClass · 0.90
replyClass · 0.90
SendDataClass · 0.90
reply_next_layerFunction · 0.90
ConnectionClosedClass · 0.90
CloseConnectionClass · 0.90
parseMethod · 0.45
decodeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…