MCPcopy
hub / github.com/mitmproxy/mitmproxy / test_upgrade

Function test_upgrade

test/mitmproxy/proxy/layers/test_websocket.py:62–129  ·  view source on GitHub ↗

Test a HTTP -> WebSocket upgrade

(tctx)

Source from the content-addressed store, hash-verified

60
61
62def test_upgrade(tctx):
63 """Test a HTTP -> WebSocket upgrade"""
64 tctx.server.address = ("example.com", 80)
65 tctx.server.state = ConnectionState.OPEN
66 flow = Placeholder(HTTPFlow)
67 assert (
68 Playbook(http.HttpLayer(tctx, HTTPMode.transparent))
69 >> DataReceived(
70 tctx.client,
71 b"GET / HTTP/1.1\r\n"
72 b"Connection: upgrade\r\n"
73 b"Upgrade: websocket\r\n"
74 b"Sec-WebSocket-Version: 13\r\n"
75 b"\r\n",
76 )
77 << http.HttpRequestHeadersHook(flow)
78 >> reply()
79 << http.HttpRequestHook(flow)
80 >> reply()
81 << SendData(
82 tctx.server,
83 b"GET / HTTP/1.1\r\n"
84 b"Connection: upgrade\r\n"
85 b"Upgrade: websocket\r\n"
86 b"Sec-WebSocket-Version: 13\r\n"
87 b"\r\n",
88 )
89 >> DataReceived(
90 tctx.server,
91 b"HTTP/1.1 101 Switching Protocols\r\n"
92 b"Upgrade: websocket\r\n"
93 b"Connection: Upgrade\r\n"
94 b"\r\n",
95 )
96 << http.HttpResponseHeadersHook(flow)
97 >> reply()
98 << http.HttpResponseHook(flow)
99 >> reply()
100 << SendData(
101 tctx.client,
102 b"HTTP/1.1 101 Switching Protocols\r\n"
103 b"Upgrade: websocket\r\n"
104 b"Connection: Upgrade\r\n"
105 b"\r\n",
106 )
107 << websocket.WebsocketStartHook(flow)
108 >> reply()
109 >> DataReceived(tctx.client, masked_bytes(b"\x81\x0bhello world"))
110 << websocket.WebsocketMessageHook(flow)
111 >> reply()
112 << SendData(tctx.server, masked(b"\x81\x0bhello world"))
113 >> DataReceived(tctx.server, b"\x82\nhello back")
114 << websocket.WebsocketMessageHook(flow)
115 >> reply()
116 << SendData(tctx.client, b"\x82\nhello back")
117 >> DataReceived(tctx.client, masked_bytes(b"\x81\x0bhello again"))
118 << websocket.WebsocketMessageHook(flow)
119 >> reply()

Callers

nothing calls this directly

Calls 7

PlaceholderFunction · 0.90
PlaybookClass · 0.90
DataReceivedClass · 0.90
replyClass · 0.90
SendDataClass · 0.90
masked_bytesFunction · 0.85
maskedFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…