From 0af060897854e0e0fc8207af02d22bec9eacab12 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Wed, 16 Sep 2015 18:45:22 +0200 Subject: adjust to netlib changes --- test/test_dump.py | 10 ++++----- test/test_filt.py | 5 ++--- test/test_flow.py | 8 +++---- test/test_fuzzing.py | 8 ++----- test/test_protocol_http.py | 52 +++++++++++++++++++--------------------------- test/test_proxy.py | 8 +++---- test/test_server.py | 52 +++++++++++++++++++++++----------------------- test/tutils.py | 2 +- 8 files changed, 65 insertions(+), 80 deletions(-) (limited to 'test') diff --git a/test/test_dump.py b/test/test_dump.py index 29931759..9d177ea0 100644 --- a/test/test_dump.py +++ b/test/test_dump.py @@ -4,7 +4,7 @@ from libmproxy.exceptions import ContentViewException from libmproxy.models import HTTPResponse import netlib.tutils -from netlib.http.semantics import CONTENT_MISSING +from netlib.http import CONTENT_MISSING from libmproxy import dump, flow from libmproxy.proxy import Log @@ -38,13 +38,13 @@ def test_strfuncs(): flow.request.stickycookie = True flow.client_conn = mock.MagicMock() flow.client_conn.address.host = "foo" - flow.response = netlib.tutils.tresp(content=CONTENT_MISSING) + flow.response = netlib.tutils.tresp(body=CONTENT_MISSING) flow.response.is_replay = True flow.response.code = 300 m.echo_flow(flow) - flow = tutils.tflow(resp=netlib.tutils.tresp("{")) + flow = tutils.tflow(resp=netlib.tutils.tresp(body="{")) flow.response.headers["content-type"] = "application/json" flow.response.code = 400 m.echo_flow(flow) @@ -62,14 +62,14 @@ def test_contentview(get_content_view): class TestDumpMaster: def _cycle(self, m, content): - f = tutils.tflow(req=netlib.tutils.treq(content)) + f = tutils.tflow(req=netlib.tutils.treq(body=content)) l = Log("connect") l.reply = mock.MagicMock() m.handle_log(l) m.handle_clientconnect(f.client_conn) m.handle_serverconnect(f.server_conn) m.handle_request(f) - f.response = HTTPResponse.wrap(netlib.tutils.tresp(content)) + f.response = HTTPResponse.wrap(netlib.tutils.tresp(body=content)) f = m.handle_response(f) m.handle_clientdisconnect(f.client_conn) return f diff --git a/test/test_filt.py b/test/test_filt.py index 76e10710..104849be 100644 --- a/test/test_filt.py +++ b/test/test_filt.py @@ -84,7 +84,7 @@ class TestMatching: "host", 80, "/path", - (1, 1), + b"HTTP/1.1", headers, "content_request", None, @@ -99,8 +99,7 @@ class TestMatching: headers = Headers([["header_response", "svalue"]]) f.response = http.HTTPResponse( - (1, - 1), + b"HTTP/1.1", 200, "OK", headers, diff --git a/test/test_flow.py b/test/test_flow.py index c93beca4..f73187c1 100644 --- a/test/test_flow.py +++ b/test/test_flow.py @@ -8,7 +8,7 @@ import mock import netlib.utils from netlib import odict -from netlib.http.semantics import CONTENT_MISSING, HDR_FORM_URLENCODED, Headers +from netlib.http import CONTENT_MISSING, HDR_FORM_URLENCODED, Headers from libmproxy import filt, protocol, controller, tnetstring, flow from libmproxy.models import Error, Flow, HTTPRequest, HTTPResponse, HTTPFlow, decoded from libmproxy.proxy.config import HostMatcher @@ -849,7 +849,7 @@ class TestFlowMaster: s = flow.State() f = tutils.tflow() - f.response = HTTPResponse.wrap(netlib.tutils.tresp(f.request)) + f.response = HTTPResponse.wrap(netlib.tutils.tresp(body=f.request)) pb = [f] fm = flow.FlowMaster(None, s) @@ -903,7 +903,7 @@ class TestFlowMaster: def test_server_playback_kill(self): s = flow.State() f = tutils.tflow() - f.response = HTTPResponse.wrap(netlib.tutils.tresp(f.request)) + f.response = HTTPResponse.wrap(netlib.tutils.tresp(body=f.request)) pb = [f] fm = flow.FlowMaster(None, s) fm.refresh_server_playback = True @@ -1043,7 +1043,7 @@ class TestRequest: def test_getset_form_urlencoded(self): d = odict.ODict([("one", "two"), ("three", "four")]) - r = HTTPRequest.wrap(netlib.tutils.treq(content=netlib.utils.urlencode(d.lst))) + r = HTTPRequest.wrap(netlib.tutils.treq(body=netlib.utils.urlencode(d.lst))) r.headers["content-type"] = HDR_FORM_URLENCODED assert r.get_form_urlencoded() == d diff --git a/test/test_fuzzing.py b/test/test_fuzzing.py index 482495f3..eff8c573 100644 --- a/test/test_fuzzing.py +++ b/test/test_fuzzing.py @@ -17,15 +17,11 @@ class TestFuzzy(tservers.HTTPProxTest): p = self.pathoc() assert p.request(req % self.server.port).status_code == 400 - def test_invalid_ports(self): - req = 'get:"http://localhost:999999"' - p = self.pathoc() - assert p.request(req).status_code == 400 - def test_invalid_ipv6_url(self): req = 'get:"http://localhost:%s":i13,"["' p = self.pathoc() - assert p.request(req % self.server.port).status_code == 400 + resp = p.request(req % self.server.port) + assert resp.status_code == 400 # def test_invalid_upstream(self): # req = r"get:'http://localhost:%s/p/200:i10,\x27+\x27'" diff --git a/test/test_protocol_http.py b/test/test_protocol_http.py index f53d43cf..5ddb5b5b 100644 --- a/test/test_protocol_http.py +++ b/test/test_protocol_http.py @@ -1,46 +1,36 @@ -import cStringIO -from cStringIO import StringIO +from io import BytesIO +from netlib.exceptions import HttpSyntaxException -from mock import MagicMock - -from libmproxy.protocol.http import * -import netlib.http from netlib.http import http1 -from netlib.http.semantics import CONTENT_MISSING - +from netlib.tutils import treq, raises import tutils import tservers -def mock_protocol(data=''): - rfile = cStringIO.StringIO(data) - wfile = cStringIO.StringIO() - return http1.HTTP1Protocol(rfile=rfile, wfile=wfile) - class TestHTTPResponse: def test_read_from_stringio(self): - s = "HTTP/1.1 200 OK\r\n" \ - "Content-Length: 7\r\n" \ - "\r\n"\ - "content\r\n" \ - "HTTP/1.1 204 OK\r\n" \ - "\r\n" - - protocol = mock_protocol(s) - r = HTTPResponse.from_protocol(protocol, "GET") + s = ( + b"HTTP/1.1 200 OK\r\n" + b"Content-Length: 7\r\n" + b"\r\n" + b"content\r\n" + b"HTTP/1.1 204 OK\r\n" + b"\r\n" + ) + rfile = BytesIO(s) + r = http1.read_response(rfile, treq()) assert r.status_code == 200 - assert r.content == "content" - assert HTTPResponse.from_protocol(protocol, "GET").status_code == 204 + assert r.content == b"content" + assert http1.read_response(rfile, treq()).status_code == 204 - protocol = mock_protocol(s) + rfile = BytesIO(s) # HEAD must not have content by spec. We should leave it on the pipe. - r = HTTPResponse.from_protocol(protocol, "HEAD") + r = http1.read_response(rfile, treq(method=b"HEAD")) assert r.status_code == 200 - assert r.content == "" - tutils.raises( - "Invalid server response: 'content", - HTTPResponse.from_protocol, protocol, "GET" - ) + assert r.content == b"" + + with raises(HttpSyntaxException): + http1.read_response(rfile, treq()) class TestHTTPFlow(object): diff --git a/test/test_proxy.py b/test/test_proxy.py index 3707fabe..76d8758c 100644 --- a/test/test_proxy.py +++ b/test/test_proxy.py @@ -9,6 +9,7 @@ from libmproxy.proxy.server import DummyServer, ProxyServer, ConnectionHandler import tutils from libpathod import test from netlib import http, tcp +from netlib.http import http1 class TestServerConnection: @@ -26,11 +27,10 @@ class TestServerConnection: f.request.path = "/p/200:da" # use this protocol just to assemble - not for actual sending - protocol = http.http1.HTTP1Protocol(rfile=sc.rfile) - sc.send(protocol.assemble(f.request)) + sc.wfile.write(http1.assemble_request(f.request)) + sc.wfile.flush() - protocol = http.http1.HTTP1Protocol(rfile=sc.rfile) - assert protocol.read_response(f.request.method, 1000) + assert http1.read_response(sc.rfile, f.request, 1000) assert self.d.last_log() sc.finish() diff --git a/test/test_server.py b/test/test_server.py index 4a5dd7c2..0e338368 100644 --- a/test/test_server.py +++ b/test/test_server.py @@ -1,13 +1,14 @@ import socket import time from OpenSSL import SSL +from netlib.exceptions import HttpReadDisconnect, HttpException from netlib.tcp import Address import netlib.tutils from netlib import tcp, http, socks from netlib.certutils import SSLCert -from netlib.http import authentication -from netlib.http.semantics import CONTENT_MISSING +from netlib.http import authentication, CONTENT_MISSING, http1 +from netlib.tutils import raises from libpathod import pathoc, pathod from libmproxy.proxy.config import HostMatcher @@ -143,10 +144,9 @@ class TcpMixin: # mitmproxy responds with bad gateway assert self.pathod(spec).status_code == 502 self._ignore_on() - tutils.raises( - "invalid server response", - self.pathod, - spec) # pathoc tries to parse answer as HTTP + with raises(HttpException): + self.pathod(spec) # pathoc tries to parse answer as HTTP + self._ignore_off() def _tcpproxy_on(self): @@ -250,11 +250,6 @@ class TestHTTP(tservers.HTTPProxTest, CommonMixin, AppMixin): assert p.request(req % self.server2.urlbase) assert switched(self.proxy.log) - def test_get_connection_err(self): - p = self.pathoc() - ret = p.request("get:'http://localhost:0'") - assert ret.status_code == 502 - def test_blank_leading_line(self): p = self.pathoc() req = "get:'%s/p/201':i0,'\r\n'" @@ -262,8 +257,8 @@ class TestHTTP(tservers.HTTPProxTest, CommonMixin, AppMixin): def test_invalid_headers(self): p = self.pathoc() - req = p.request("get:'http://foo':h':foo'='bar'") - assert req.status_code == 400 + resp = p.request("get:'http://foo':h':foo'='bar'") + assert resp.status_code == 400 def test_empty_chunked_content(self): """ @@ -570,17 +565,23 @@ class TestProxy(tservers.HTTPProxTest): connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) connection.connect(("localhost", self.proxy.port)) connection.send( - "GET http://localhost:%d/p/304:b@1k HTTP/1.1\r\n" % + "GET http://localhost:%d/p/200:b@1k HTTP/1.1\r\n" % self.server.port) connection.send("\r\n") - connection.recv(5000) + # a bit hacky: make sure that we don't just read the headers only. + recvd = 0 + while recvd < 1024: + recvd += len(connection.recv(5000)) connection.send( - "GET http://localhost:%d/p/304:b@1k HTTP/1.1\r\n" % + "GET http://localhost:%d/p/200:b@1k HTTP/1.1\r\n" % self.server.port) connection.send("\r\n") - connection.recv(5000) + recvd = 0 + while recvd < 1024: + recvd += len(connection.recv(5000)) connection.close() + print(self.master.state.view._list) first_flow = self.master.state.view[0] second_flow = self.master.state.view[1] assert first_flow.server_conn.timestamp_tcp_setup @@ -718,15 +719,12 @@ class TestStreamRequest(tservers.HTTPProxTest): (self.server.urlbase, spec)) connection.send("\r\n") - protocol = http.http1.HTTP1Protocol(rfile=fconn) - resp = protocol.read_response("GET", None, include_body=False) + resp = http1.read_response_head(fconn) assert resp.headers["Transfer-Encoding"] == 'chunked' assert resp.status_code == 200 - chunks = list(protocol.read_http_body_chunked( - resp.headers, None, "GET", 200, False - )) + chunks = list(http1.read_body(fconn, None)) assert chunks == ["this", "isatest__reachhex"] connection.close() @@ -743,7 +741,7 @@ class TestFakeResponse(tservers.HTTPProxTest): def test_fake(self): f = self.pathod("200") - assert "header_response" in f.headers + assert "header-response" in f.headers class TestServerConnect(tservers.HTTPProxTest): @@ -766,7 +764,8 @@ class TestKillRequest(tservers.HTTPProxTest): masterclass = MasterKillRequest def test_kill(self): - tutils.raises("server disconnect", self.pathod, "200") + with raises(HttpReadDisconnect): + self.pathod("200") # Nothing should have hit the server assert not self.server.last_log() @@ -780,7 +779,8 @@ class TestKillResponse(tservers.HTTPProxTest): masterclass = MasterKillResponse def test_kill(self): - tutils.raises("server disconnect", self.pathod, "200") + with raises(HttpReadDisconnect): + self.pathod("200") # The server should have seen a request assert self.server.last_log() @@ -907,7 +907,7 @@ class TestUpstreamProxySSL( """ def handle_request(f): - f.request.httpversion = (1, 0) + f.request.httpversion = b"HTTP/1.1" del f.request.headers["Content-Length"] f.reply() diff --git a/test/tutils.py b/test/tutils.py index d64388f3..229b51a8 100644 --- a/test/tutils.py +++ b/test/tutils.py @@ -102,7 +102,7 @@ def tflowview(request_contents=None): if request_contents is None: flow = tflow() else: - flow = tflow(req=netlib.tutils.treq(request_contents)) + flow = tflow(req=netlib.tutils.treq(body=request_contents)) fv = FlowView(m, cs, flow) return fv -- cgit v1.2.3