From 4f97216501c2d55038aefc4a50b94798ac8bae42 Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Tue, 7 Jun 2016 12:07:46 +0530 Subject: [PATCH 1/8] Py3: Properly encode() access to user_agents --- pathod/language/http2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pathod/language/http2.py b/pathod/language/http2.py index 85d9047fc..45b2a1048 100644 --- a/pathod/language/http2.py +++ b/pathod/language/http2.py @@ -125,7 +125,7 @@ class ShortcutUserAgent(_HeaderMixin, base.OptionsOrValue): def values(self, settings): value = self.value.val if self.option_used: - value = user_agents.get_by_shortcut(value.lower())[2] + value = user_agents.get_by_shortcut(value.lower().decode())[2].encode() return ( self.key.get_generator(settings), From 0280af9522aadd0f6ca15e8d55f4991cfa884c59 Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Tue, 7 Jun 2016 12:08:20 +0530 Subject: [PATCH 2/8] Py3: Use BytesIO instead of StringIO --- test/pathod/test_language_http2.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/pathod/test_language_http2.py b/test/pathod/test_language_http2.py index de256626f..4b4d1ff96 100644 --- a/test/pathod/test_language_http2.py +++ b/test/pathod/test_language_http2.py @@ -1,4 +1,4 @@ -from six.moves import cStringIO as StringIO +from six import BytesIO import netlib from netlib import tcp @@ -25,7 +25,7 @@ def default_settings(): def test_make_error_response(): - d = StringIO() + d = BytesIO() s = http2.make_error_response("foo", "bar") language.serve(s, d, default_settings()) @@ -84,7 +84,7 @@ class TestRequest: assert r[1].method.string() == "GET" def test_render_simple(self): - s = StringIO() + s = BytesIO() r = parse_request("GET:'/foo'") assert language.serve( r, @@ -126,7 +126,7 @@ class TestRequest: assert r.headers[0].values(default_settings()) == ("user-agent", user_agents.get_by_shortcut('a')[2]) def test_render_with_headers(self): - s = StringIO() + s = BytesIO() r = parse_request('GET:/foo:h"foo"="bar"') assert language.serve( r, @@ -142,7 +142,7 @@ class TestRequest: assert r.values(default_settings()) def test_render_with_body(self): - s = StringIO() + s = BytesIO() r = parse_request("GET:'/foo':bfoobar") assert language.serve( r, @@ -199,7 +199,7 @@ class TestResponse: assert r.body.string() == "foobar" def test_render_simple(self): - s = StringIO() + s = BytesIO() r = parse_response('200') assert language.serve( r, @@ -208,7 +208,7 @@ class TestResponse: ) def test_render_with_headers(self): - s = StringIO() + s = BytesIO() r = parse_response('200:h"foo"="bar"') assert language.serve( r, @@ -217,7 +217,7 @@ class TestResponse: ) def test_render_with_body(self): - s = StringIO() + s = BytesIO() r = parse_response('200:bfoobar') assert language.serve( r, From 6b03df2633c4e82dac378cb2d0d5506067c6f40c Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Tue, 7 Jun 2016 12:08:46 +0530 Subject: [PATCH 3/8] Py3: Use global next() instead of iterator method --- test/pathod/test_language_http2.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/pathod/test_language_http2.py b/test/pathod/test_language_http2.py index 4b4d1ff96..40243f03d 100644 --- a/test/pathod/test_language_http2.py +++ b/test/pathod/test_language_http2.py @@ -10,11 +10,11 @@ import tutils def parse_request(s): - return language.parse_pathoc(s, True).next() + return next(language.parse_pathoc(s, True)) def parse_response(s): - return language.parse_pathod(s, True).next() + return next(language.parse_pathod(s, True)) def default_settings(): From b3b4156c2f50ae81f1c1e4c750e47572127baecd Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Tue, 7 Jun 2016 12:09:30 +0530 Subject: [PATCH 4/8] Py3: Fix test_language_http2 tests by using byte literals --- test/pathod/test_language_http2.py | 46 +++++++++++++++--------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/test/pathod/test_language_http2.py b/test/pathod/test_language_http2.py index 40243f03d..a2bffe630 100644 --- a/test/pathod/test_language_http2.py +++ b/test/pathod/test_language_http2.py @@ -46,15 +46,15 @@ class TestRequest: def test_simple(self): r = parse_request('GET:"/foo"') - assert r.method.string() == "GET" - assert r.path.string() == "/foo" + assert r.method.string() == b"GET" + assert r.path.string() == b"/foo" r = parse_request('GET:/foo') - assert r.path.string() == "/foo" + assert r.path.string() == b"/foo" def test_multiple(self): r = list(language.parse_pathoc("GET:/ PUT:/")) - assert r[0].method.string() == "GET" - assert r[1].method.string() == "PUT" + assert r[0].method.string() == b"GET" + assert r[1].method.string() == b"PUT" assert len(r) == 2 l = """ @@ -71,8 +71,8 @@ class TestRequest: """ r = list(language.parse_pathoc(l, True)) assert len(r) == 2 - assert r[0].method.string() == "GET" - assert r[1].method.string() == "PUT" + assert r[0].method.string() == b"GET" + assert r[1].method.string() == b"PUT" l = """ get:"http://localhost:9999/p/200" @@ -80,8 +80,8 @@ class TestRequest: """ r = list(language.parse_pathoc(l, True)) assert len(r) == 2 - assert r[0].method.string() == "GET" - assert r[1].method.string() == "GET" + assert r[0].method.string() == b"GET" + assert r[1].method.string() == b"GET" def test_render_simple(self): s = BytesIO() @@ -101,29 +101,29 @@ class TestRequest: r = parse_request('GET:/') assert len(r.headers) == 1 - assert r.headers[0].values(default_settings()) == ("content-length", "0") + assert r.headers[0].values(default_settings()) == (b"content-length", b"0") r = parse_request('GET:/:b"foobar"') assert len(r.headers) == 1 - assert r.headers[0].values(default_settings()) == ("content-length", "6") + assert r.headers[0].values(default_settings()) == (b"content-length", b"6") r = parse_request('GET:/:b"foobar":h"content-length"="42"') assert len(r.headers) == 1 - assert r.headers[0].values(default_settings()) == ("content-length", "42") + assert r.headers[0].values(default_settings()) == (b"content-length", b"42") r = parse_request('GET:/:r:b"foobar":h"content-length"="42"') assert len(r.headers) == 1 - assert r.headers[0].values(default_settings()) == ("content-length", "42") + assert r.headers[0].values(default_settings()) == (b"content-length", b"42") def test_content_type(self): r = parse_request('GET:/:r:c"foobar"') assert len(r.headers) == 1 - assert r.headers[0].values(default_settings()) == ("content-type", "foobar") + assert r.headers[0].values(default_settings()) == (b"content-type", b"foobar") def test_user_agent(self): r = parse_request('GET:/:r:ua') assert len(r.headers) == 1 - assert r.headers[0].values(default_settings()) == ("user-agent", user_agents.get_by_shortcut('a')[2]) + assert r.headers[0].values(default_settings()) == (b"user-agent", user_agents.get_by_shortcut('a')[2].encode()) def test_render_with_headers(self): s = BytesIO() @@ -177,26 +177,26 @@ class TestResponse: r = parse_response('200') assert len(r.headers) == 1 - assert r.headers[0].values(default_settings()) == ("content-length", "0") + assert r.headers[0].values(default_settings()) == (b"content-length", b"0") def test_content_type(self): r = parse_response('200:r:c"foobar"') assert len(r.headers) == 1 - assert r.headers[0].values(default_settings()) == ("content-type", "foobar") + assert r.headers[0].values(default_settings()) == (b"content-type", b"foobar") def test_simple(self): r = parse_response('200:r:h"foo"="bar"') - assert r.status_code.string() == "200" + assert r.status_code.string() == b"200" assert len(r.headers) == 1 - assert r.headers[0].values(default_settings()) == ("foo", "bar") + assert r.headers[0].values(default_settings()) == (b"foo", b"bar") assert r.body is None r = parse_response('200:r:h"foo"="bar":bfoobar:h"bla"="fasel"') - assert r.status_code.string() == "200" + assert r.status_code.string() == b"200" assert len(r.headers) == 2 - assert r.headers[0].values(default_settings()) == ("foo", "bar") - assert r.headers[1].values(default_settings()) == ("bla", "fasel") - assert r.body.string() == "foobar" + assert r.headers[0].values(default_settings()) == (b"foo", b"bar") + assert r.headers[1].values(default_settings()) == (b"bla", b"fasel") + assert r.body.string() == b"foobar" def test_render_simple(self): s = BytesIO() From 2ff5c789df09e8d101ded5312cd8071cd0cb0537 Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Tue, 7 Jun 2016 12:18:14 +0530 Subject: [PATCH 5/8] Enable Travis for test_language_http2 --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index b75f536ff..feb680246 100644 --- a/tox.ini +++ b/tox.ini @@ -16,7 +16,7 @@ commands = # remove bash & pipe & grep hack after cryptography ships with openssl 1.1.0 whitelist_externals = bash commands = - bash -c 'py.test --cov netlib --cov mitmproxy --cov pathod --color=yes --timeout 60 test/netlib test/mitmproxy/script test/pathod/test_utils.py test/pathod/test_log.py test/pathod/test_language_generators.py test/pathod/test_language_writer.py test/pathod/test_language_base.py test/pathod/test_language_http.py test/pathod/test_language_websocket.py 2>&1 | grep -v Cryptography_locking_cb' + bash -c 'py.test --cov netlib --cov mitmproxy --cov pathod --color=yes --timeout 60 test/netlib test/mitmproxy/script test/pathod/test_utils.py test/pathod/test_log.py test/pathod/test_language_generators.py test/pathod/test_language_writer.py test/pathod/test_language_base.py test/pathod/test_language_http.py test/pathod/test_language_websocket.py test/pathod/test_language_http2.py 2>&1 | grep -v Cryptography_locking_cb' codecov -e TOXENV [testenv:docs] From e187358e97f93ca8a1b3df4761a1edca5c4f5e59 Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Tue, 7 Jun 2016 13:40:39 +0530 Subject: [PATCH 6/8] Py3: Pass bytes to http Request and Response --- pathod/language/http2.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pathod/language/http2.py b/pathod/language/http2.py index 45b2a1048..e83fe9ad8 100644 --- a/pathod/language/http2.py +++ b/pathod/language/http2.py @@ -190,7 +190,7 @@ class Response(_HTTP2Message): resp = http.Response( (2, 0), self.status_code.string(), - '', + b'', headers, body, ) @@ -271,11 +271,11 @@ class Request(_HTTP2Message): body = body.string() req = http.Request( - '', + b'', self.method.string(), - '', - '', - '', + b'', + b'', + b'', path, (2, 0), headers, From 70216673a1411a17b29899ed1807008ef5867948 Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Wed, 8 Jun 2016 16:28:39 +0530 Subject: [PATCH 7/8] Py3: Find content-length header by bytes --- pathod/language/http2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pathod/language/http2.py b/pathod/language/http2.py index e83fe9ad8..4d2ae1984 100644 --- a/pathod/language/http2.py +++ b/pathod/language/http2.py @@ -60,7 +60,7 @@ class _HTTP2Message(message.Message): headers = self.toks(_HeaderMixin) if not self.raw: - if not get_header("content-length", headers): + if not get_header(b"content-length", headers): if not self.body: length = 0 else: From 7d62121b858040935555098ec4c7323662b76e3b Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Wed, 8 Jun 2016 16:29:21 +0530 Subject: [PATCH 8/8] Py3: encode() the spec before appending to path --- pathod/language/http2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pathod/language/http2.py b/pathod/language/http2.py index 4d2ae1984..ea4fcd27b 100644 --- a/pathod/language/http2.py +++ b/pathod/language/http2.py @@ -262,7 +262,7 @@ class Request(_HTTP2Message): else: path = self.path.string() if self.nested_response: - path += self.nested_response.parsed.spec() + path += self.nested_response.parsed.spec().encode() headers = Headers([header.values(settings) for header in self.headers])