2015-07-27 09:46:49 +00:00
|
|
|
import cStringIO
|
2015-04-13 23:58:10 +00:00
|
|
|
from cStringIO import StringIO
|
|
|
|
|
2015-03-16 09:23:50 +00:00
|
|
|
from mock import MagicMock
|
2015-04-13 23:58:10 +00:00
|
|
|
|
2014-02-07 02:56:57 +00:00
|
|
|
from libmproxy.protocol.http import *
|
2015-04-13 23:58:10 +00:00
|
|
|
from netlib import odict
|
2015-07-27 09:46:49 +00:00
|
|
|
from netlib.http import http1
|
2015-07-29 09:39:53 +00:00
|
|
|
from netlib.http.semantics import CONTENT_MISSING
|
2015-04-13 23:58:10 +00:00
|
|
|
|
2015-04-14 21:14:20 +00:00
|
|
|
import tutils
|
|
|
|
import tservers
|
2014-02-07 02:56:57 +00:00
|
|
|
|
2015-08-09 20:15:58 +00:00
|
|
|
def mock_protocol(data=''):
|
2015-07-27 09:46:49 +00:00
|
|
|
rfile = cStringIO.StringIO(data)
|
|
|
|
wfile = cStringIO.StringIO()
|
|
|
|
return http1.HTTP1Protocol(rfile=rfile, wfile=wfile)
|
|
|
|
|
|
|
|
|
2014-02-07 02:56:57 +00:00
|
|
|
class TestHTTPResponse:
|
|
|
|
def test_read_from_stringio(self):
|
2015-07-27 09:46:49 +00:00
|
|
|
s = "HTTP/1.1 200 OK\r\n" \
|
2014-02-07 02:56:57 +00:00
|
|
|
"Content-Length: 7\r\n" \
|
|
|
|
"\r\n"\
|
|
|
|
"content\r\n" \
|
|
|
|
"HTTP/1.1 204 OK\r\n" \
|
|
|
|
"\r\n"
|
2015-07-27 09:46:49 +00:00
|
|
|
|
|
|
|
protocol = mock_protocol(s)
|
|
|
|
r = HTTPResponse.from_protocol(protocol, "GET")
|
|
|
|
assert r.status_code == 200
|
2014-02-07 02:56:57 +00:00
|
|
|
assert r.content == "content"
|
2015-07-27 09:46:49 +00:00
|
|
|
assert HTTPResponse.from_protocol(protocol, "GET").status_code == 204
|
2014-02-07 02:56:57 +00:00
|
|
|
|
2015-07-27 09:46:49 +00:00
|
|
|
protocol = mock_protocol(s)
|
2015-04-14 21:14:20 +00:00
|
|
|
# HEAD must not have content by spec. We should leave it on the pipe.
|
2015-07-27 09:46:49 +00:00
|
|
|
r = HTTPResponse.from_protocol(protocol, "HEAD")
|
|
|
|
assert r.status_code == 200
|
2014-02-07 02:56:57 +00:00
|
|
|
assert r.content == ""
|
2015-04-14 21:14:20 +00:00
|
|
|
tutils.raises(
|
|
|
|
"Invalid server response: 'content",
|
2015-07-27 09:46:49 +00:00
|
|
|
HTTPResponse.from_protocol, protocol, "GET"
|
2015-04-14 21:14:20 +00:00
|
|
|
)
|
2014-02-07 06:08:59 +00:00
|
|
|
|
2015-04-14 04:23:51 +00:00
|
|
|
|
2014-09-04 12:46:25 +00:00
|
|
|
class TestHTTPFlow(object):
|
|
|
|
def test_repr(self):
|
|
|
|
f = tutils.tflow(resp=True, err=True)
|
|
|
|
assert repr(f)
|
|
|
|
|
2014-02-07 06:08:59 +00:00
|
|
|
|
2014-02-07 17:14:15 +00:00
|
|
|
class TestInvalidRequests(tservers.HTTPProxTest):
|
|
|
|
ssl = True
|
2015-04-14 21:14:20 +00:00
|
|
|
|
2014-02-07 17:14:15 +00:00
|
|
|
def test_double_connect(self):
|
|
|
|
p = self.pathoc()
|
|
|
|
r = p.request("connect:'%s:%s'" % ("127.0.0.1", self.server2.port))
|
2014-03-10 01:32:27 +00:00
|
|
|
assert r.status_code == 400
|
2015-07-22 11:04:45 +00:00
|
|
|
assert "Must not CONNECT on already encrypted connection" in r.body
|
2014-02-07 17:14:15 +00:00
|
|
|
|
2014-03-08 14:47:09 +00:00
|
|
|
def test_relative_request(self):
|
2014-02-07 17:14:15 +00:00
|
|
|
p = self.pathoc_raw()
|
|
|
|
p.connect()
|
|
|
|
r = p.request("get:/p/200")
|
|
|
|
assert r.status_code == 400
|
2015-07-22 11:04:45 +00:00
|
|
|
assert "Invalid HTTP request form" in r.body
|