1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
import cStringIO, textwrap
from cStringIO import StringIO
import libpry
from libmproxy import proxy, flow
class u_read_chunked(libpry.AutoTree):
def test_all(self):
s = cStringIO.StringIO("1\r\na\r\n0\r\n")
libpry.raises(IOError, proxy.read_chunked, s, None)
s = cStringIO.StringIO("1\r\na\r\n0\r\n\r\n")
assert proxy.read_chunked(s, None) == "a"
s = cStringIO.StringIO("\r\n")
libpry.raises(IOError, proxy.read_chunked, s, None)
s = cStringIO.StringIO("1\r\nfoo")
libpry.raises(IOError, proxy.read_chunked, s, None)
s = cStringIO.StringIO("foo\r\nfoo")
libpry.raises(proxy.ProxyError, proxy.read_chunked, s, None)
class Dummy: pass
class u_read_http_body(libpry.AutoTree):
def test_all(self):
d = Dummy()
h = flow.ODict()
s = cStringIO.StringIO("testing")
assert proxy.read_http_body(s, d, h, False, None) == ""
h["content-length"] = ["foo"]
s = cStringIO.StringIO("testing")
libpry.raises(proxy.ProxyError, proxy.read_http_body, s, d, h, False, None)
h["content-length"] = [5]
s = cStringIO.StringIO("testing")
assert len(proxy.read_http_body(s, d, h, False, None)) == 5
s = cStringIO.StringIO("testing")
libpry.raises(proxy.ProxyError, proxy.read_http_body, s, d, h, False, 4)
h = flow.ODict()
s = cStringIO.StringIO("testing")
assert len(proxy.read_http_body(s, d, h, True, 4)) == 4
s = cStringIO.StringIO("testing")
assert len(proxy.read_http_body(s, d, h, True, 100)) == 7
class u_parse_request_line(libpry.AutoTree):
def test_simple(self):
libpry.raises(proxy.ProxyError, proxy.parse_request_line, "")
u = "GET ... HTTP/1.1"
libpry.raises("invalid url", proxy.parse_request_line, u)
u = "GET http://foo.com:8888/test HTTP/1.1"
m, s, h, po, pa, minor = proxy.parse_request_line(u)
assert m == "GET"
assert s == "http"
assert h == "foo.com"
assert po == 8888
assert pa == "/test"
assert minor == 1
def test_connect(self):
u = "CONNECT host.com:443 HTTP/1.0"
expected = ('CONNECT', None, 'host.com', 443, None, 0)
ret = proxy.parse_request_line(u)
assert expected == ret
def test_inner(self):
u = "GET / HTTP/1.1"
assert proxy.parse_request_line(u) == ('GET', None, None, None, '/', 1)
class uFileLike(libpry.AutoTree):
def test_wrap(self):
s = cStringIO.StringIO("foobar\nfoobar")
s = proxy.FileLike(s)
s.flush()
assert s.readline() == "foobar\n"
assert s.readline() == "foobar"
# Test __getattr__
assert s.isatty
class uProxyError(libpry.AutoTree):
def test_simple(self):
p = proxy.ProxyError(111, "msg")
assert repr(p)
class u_read_headers(libpry.AutoTree):
def test_read_simple(self):
data = """
Header: one
Header2: two
\r\n
"""
data = textwrap.dedent(data)
data = data.strip()
s = StringIO(data)
headers = proxy.read_headers(s)
assert headers["header"] == ["one"]
assert headers["header2"] == ["two"]
def test_read_multi(self):
data = """
Header: one
Header: two
\r\n
"""
data = textwrap.dedent(data)
data = data.strip()
s = StringIO(data)
headers = proxy.read_headers(s)
assert headers["header"] == ["one", "two"]
def test_read_continued(self):
data = """
Header: one
\ttwo
Header2: three
\r\n
"""
data = textwrap.dedent(data)
data = data.strip()
s = StringIO(data)
headers = proxy.read_headers(s)
assert headers["header"] == ['one\r\n two']
class u_parse_http_protocol(libpry.AutoTree):
def test_simple(self):
assert proxy.parse_http_protocol("HTTP/1.1") == (1, 1)
assert proxy.parse_http_protocol("HTTP/0.0") == (0, 0)
assert not proxy.parse_http_protocol("foo/0.0")
class u_parse_init_connect(libpry.AutoTree):
def test_simple(self):
assert proxy.parse_init_connect("CONNECT host.com:443 HTTP/1.0")
assert not proxy.parse_init_connect("bogus")
assert not proxy.parse_init_connect("GET host.com:443 HTTP/1.0")
assert not proxy.parse_init_connect("CONNECT host.com443 HTTP/1.0")
assert not proxy.parse_init_connect("CONNECT host.com:443 foo/1.0")
class u_parse_init_proxy(libpry.AutoTree):
def test_simple(self):
u = "GET http://foo.com:8888/test HTTP/1.1"
m, s, h, po, pa, major, minor = proxy.parse_init_proxy(u)
assert m == "GET"
assert s == "http"
assert h == "foo.com"
assert po == 8888
assert pa == "/test"
assert major == 1
assert minor == 1
assert not proxy.parse_init_proxy("invalid")
assert not proxy.parse_init_proxy("GET invalid HTTP/1.1")
assert not proxy.parse_init_proxy("GET http://foo.com:8888/test foo/1.1")
class u_parse_init_http(libpry.AutoTree):
def test_simple(self):
u = "GET /test HTTP/1.1"
m, u, major, minor = proxy.parse_init_http(u)
assert m == "GET"
assert u == "/test"
assert major == 1
assert minor == 1
assert not proxy.parse_init_http("invalid")
assert not proxy.parse_init_http("GET invalid HTTP/1.1")
assert not proxy.parse_init_http("GET /test foo/1.1")
tests = [
u_parse_http_protocol(),
u_parse_init_connect(),
u_parse_init_proxy(),
u_parse_init_http(),
uProxyError(),
uFileLike(),
u_parse_request_line(),
u_read_chunked(),
u_read_http_body(),
u_read_headers()
]
|