def test_equal(self): a = tutils.tresp() b = tutils.tresp() assert a == b assert not a == 'foo' assert not b == 'foo' assert not 'foo' == a assert not 'foo' == b
def test_equal(self): a = tutils.tresp(timestamp_start=42, timestamp_end=43) b = tutils.tresp(timestamp_start=42, timestamp_end=43) assert a == b assert not a == 'foo' assert not b == 'foo' assert not 'foo' == a assert not 'foo' == b
def test_eq_ne(self): data = tresp(timestamp_start=42, timestamp_end=42).data same = tresp(timestamp_start=42, timestamp_end=42).data assert data == same assert not data != same other = tresp(content=b"foo").data assert not data == other assert data != other assert data != 0
def test_eq_ne(self): resp = tresp(timestamp_start=42, timestamp_end=42) same = tresp(timestamp_start=42, timestamp_end=42) assert resp == same assert not resp != same other = tresp(timestamp_start=0, timestamp_end=0) assert not resp == other assert resp != other assert resp != 0
def test_assemble_response(): c = assemble_response(tresp()) == ( b"HTTP/1.1 200 OK\r\n" b"header-response: svalue\r\n" b"Content-Length: 7\r\n" b"\r\n" b"message" ) with raises(HttpException): assemble_response(tresp(content=CONTENT_MISSING))
def test_assemble_response(): assert assemble_response(tresp()) == ( b"HTTP/1.1 200 OK\r\n" b"content-length: 7\r\n" b"header-response: svalue\r\n" b"\r\n" b"message" ) with raises(HttpException): assemble_response(tresp(content=None))
def test_cannot_encode(self): r = tresp() r.content = None assert "content-type" not in r.headers assert r.raw_content is None r.headers["content-type"] = "text/html; charset=latin1; foo=bar" r.text = u"☃" assert r.headers["content-type"] == "text/html; charset=utf-8; foo=bar" assert r.raw_content == b'\xe2\x98\x83' r.headers["content-type"] = "gibberish" r.text = u"☃" assert r.headers["content-type"] == "text/plain; charset=utf-8" assert r.raw_content == b'\xe2\x98\x83' del r.headers["content-type"] r.text = u"☃" assert r.headers["content-type"] == "text/plain; charset=utf-8" assert r.raw_content == b'\xe2\x98\x83' r.headers["content-type"] = "text/html; charset=latin1" r.text = u'\udcff' assert r.headers["content-type"] == "text/html; charset=utf-8" assert r.raw_content == b'\xed\xb3\xbf' if six.PY2 else b"\xFF"
def test_har_extractor(log): if sys.version_info >= (3, 0): with tutils.raises("does not work on Python 3"): with example("har_extractor.py -"): pass return with tutils.raises(script.ScriptException): with example("har_extractor.py"): pass times = dict( timestamp_start=746203272, timestamp_end=746203272, ) flow = tutils.tflow( req=netutils.treq(**times), resp=netutils.tresp(**times) ) with example("har_extractor.py -") as ex: ex.run("response", flow) with open(tutils.test_data.path("data/har_extractor.har")) as fp: test_data = json.load(fp) assert json.loads(ex.ns["context"].HARLog.json()) == test_data["test_response"]
def test_get_cookies_simple(self): resp = tresp() resp.headers = Headers(set_cookie="cookiename=cookievalue") result = resp.cookies assert len(result) == 1 assert "cookiename" in result assert result["cookiename"][0] == ["cookievalue", ODict()]
def test_none(self): r = tresp(content=None) assert r.text is None r.text = u"foo" assert r.text is not None r.text = None assert r.text is None
def test_unknown_ce(self): r = tresp() r.headers["content-type"] = "text/html; charset=wtf" r.raw_content = b"foo" with tutils.raises(ValueError): assert r.text == u"foo" assert r.get_text(strict=False) == u"foo"
def test_get_cookies_simple(self): resp = tutils.tresp() resp.headers = http.Headers(set_cookie="cookiename=cookievalue") result = resp.get_cookies() assert len(result) == 1 assert "cookiename" in result assert result["cookiename"][0] == ["cookievalue", odict.ODict()]
def test_simple(self): r = tresp(content=b'\xfc') assert r.raw_content == b"\xfc" assert r.content == b"\xfc" assert r.text == u"ü" r.encode("gzip") assert r.text == u"ü" r.decode() assert r.text == u"ü" r.headers["content-type"] = "text/html; charset=latin1" r.content = b"\xc3\xbc" assert r.text == u"ü" r.headers["content-type"] = "text/html; charset=utf8" assert r.text == u"ü" r.encode("identity") r.raw_content = b"foo" with mock.patch("netlib.encoding.decode") as e: assert r.text assert e.call_count == 2 e.reset_mock() assert r.text assert e.call_count == 0
def test_har_extractor(self): if sys.version_info >= (3, 0): with tutils.raises("does not work on Python 3"): tscript("har_extractor.py") return with tutils.raises(ScriptError): tscript("har_extractor.py") with tutils.tmpdir() as tdir: times = dict( timestamp_start=746203272, timestamp_end=746203272, ) path = os.path.join(tdir, "file") m, sc = tscript("har_extractor.py", six.moves.shlex_quote(path)) f = tutils.tflow( req=netutils.treq(**times), resp=netutils.tresp(**times) ) self.invoke(m, "response", f) m.addons.remove(sc) with open(path, "rb") as f: test_data = json.load(f) assert len(test_data["log"]["pages"]) == 1
def test_modify_response_body(self): with tutils.raises(ScriptError): tscript("modify_response_body.py") m, sc = tscript("modify_response_body.py", "mitmproxy rocks") f = tutils.tflow(resp=netutils.tresp(content=b"I <3 mitmproxy")) self.invoke(m, "response", f) assert f.response.content == b"I <3 rocks"
def test_content_length_update(self): resp = tresp() resp.content = b"foo" assert resp.data.content == b"foo" assert resp.headers["content-length"] == "3" resp.content = b"" assert resp.data.content == b"" assert resp.headers["content-length"] == "0"
def test_get_cookies_no_value(self): resp = tresp() resp.headers = Headers(set_cookie="cookiename=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/") result = resp.cookies assert len(result) == 1 assert "cookiename" in result assert result["cookiename"][0][0] == "" assert len(result["cookiename"][0][1]) == 2
def test_cannot_decode(self): r = tresp() r.headers["content-type"] = "text/html; charset=utf8" r.raw_content = b"\xFF" with tutils.raises(ValueError): assert r.text assert r.get_text(strict=False) == u'\ufffd' if six.PY2 else '\udcff'
def test_unknown_ce(self): r = tresp() r.headers["content-encoding"] = "zopfli" r.raw_content = b"foo" with tutils.raises(ValueError): assert r.content assert r.headers["content-encoding"] assert r.get_content(strict=False) == b"foo"
def test_cannot_encode(self): r = tresp() assert r.encode("gzip") with decoded(r): r.content = None assert "content-encoding" not in r.headers assert r.content is None
def test_unknown_ce(self): r = tresp() r.headers["content-encoding"] = "zopfli" r.content = b"foo" with decoded(r): assert r.headers["content-encoding"] assert r.content == b"foo" assert r.headers["content-encoding"] assert r.content == b"foo"
def test_iframe_injector(self): with tutils.raises(ScriptError): tscript("iframe_injector.py") m, sc = tscript("iframe_injector.py", "http://example.org/evil_iframe") flow = tutils.tflow(resp=netutils.tresp(content=b"<html>mitmproxy</html>")) self.invoke(m, "response", flow) content = flow.response.content assert b'iframe' in content and b'evil_iframe' in content
def test_set_cookies(self): resp = tutils.tresp() v = resp.get_cookies() v.add("foo", ["bar", odict.ODictCaseless()]) resp.set_cookies(v) v = resp.get_cookies() assert len(v) == 1 assert v["foo"] == [["bar", odict.ODictCaseless()]]
def test_get_cookies_simple(self): h = odict.ODictCaseless() h["Set-Cookie"] = ["cookiename=cookievalue"] resp = tutils.tresp() resp.headers = h result = resp.get_cookies() assert len(result) == 1 assert "cookiename" in result assert result["cookiename"][0] == ["cookievalue", odict.ODict()]
def test_set_cookies(self): resp = tresp() v = resp.cookies v.add("foo", ["bar", ODictCaseless()]) resp.set_cookies(v) v = resp.cookies assert len(v) == 1 assert v["foo"] == [["bar", ODictCaseless()]]
def test_replace(self): r = tresp() r.content = b"foofootoo" r.replace(b"foo", "gg") assert r.content == b"ggggtoo" r.content = b"foofootoo" r.replace(b"foo", "gg", count=1) assert r.content == b"ggfootoo"
def test_get_cookies_twocookies(self): resp = tresp() resp.headers = Headers([[b"Set-Cookie", b"cookiename=cookievalue"], [b"Set-Cookie", b"othercookie=othervalue"]]) result = resp.cookies assert len(result) == 2 assert "cookiename" in result assert result["cookiename"][0] == ["cookievalue", ODict()] assert "othercookie" in result assert result["othercookie"][0] == ["othervalue", ODict()]
def test_simple(self): resp = tutils.tresp() b = HTTP1Protocol().assemble_response(resp) assert b == match_http_string(""" HTTP/1.1 200 OK header_response: svalue Content-Length: 7 message""")
def test_iframe_injector(): with tutils.raises(script.ScriptException): with example("iframe_injector.py") as ex: pass flow = tutils.tflow(resp=netutils.tresp(content="<html>mitmproxy</html>")) with example("iframe_injector.py http://example.org/evil_iframe") as ex: ex.run("response", flow) content = flow.response.content assert 'iframe' in content and 'evil_iframe' in content
def test_modify_response_body(): with tutils.raises(script.ScriptException): with example("modify_response_body.py"): assert True flow = tutils.tflow(resp=netutils.tresp(content="I <3 mitmproxy")) with example("modify_response_body.py mitmproxy rocks") as ex: assert ex.ctx.old == "mitmproxy" and ex.ctx.new == "rocks" ex.run("response", flow) assert flow.response.content == "I <3 rocks"
def test_get_cookies_no_value(self): resp = tutils.tresp() resp.headers = http.Headers( set_cookie= "cookiename=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/") result = resp.get_cookies() assert len(result) == 1 assert "cookiename" in result assert result["cookiename"][0][0] == "" assert len(result["cookiename"][0][1]) == 2
def test_simple(self): r = tresp() assert r.raw_content == b"message" assert "content-encoding" not in r.headers r.encode("gzip") assert r.headers["content-encoding"] assert r.raw_content != b"message" assert r.content == b"message" assert r.raw_content != b"message"
def test_iframe_injector(self): with tutils.raises(ScriptError): tscript("iframe_injector.py") m, sc = tscript("iframe_injector.py", "http://example.org/evil_iframe") f = tutils.tflow(resp=netutils.tresp( content=b"<html>mitmproxy</html>")) m.response(f) content = f.response.content assert b'iframe' in content and b'evil_iframe' in content
def test_expected_http_body_size(): # Expect: 100-continue assert expected_http_body_size( treq(headers=Headers(expect="100-continue", content_length="42")) ) == 0 # http://tools.ietf.org/html/rfc7230#section-3.3 assert expected_http_body_size( treq(method=b"HEAD"), tresp(headers=Headers(content_length="42")) ) == 0 assert expected_http_body_size( treq(method=b"CONNECT"), tresp() ) == 0 for code in (100, 204, 304): assert expected_http_body_size( treq(), tresp(status_code=code) ) == 0 # chunked assert expected_http_body_size( treq(headers=Headers(transfer_encoding="chunked")), ) is None # explicit length for val in (b"foo", b"-7"): with raises(HttpSyntaxException): expected_http_body_size( treq(headers=Headers(content_length=val)) ) assert expected_http_body_size( treq(headers=Headers(content_length="42")) ) == 42 # no length assert expected_http_body_size( treq(headers=Headers()) ) == 0 assert expected_http_body_size( treq(headers=Headers()), tresp(headers=Headers()) ) == -1
def test_modify(self): r = tresp() r.text = u"ü" assert r.raw_content == b"\xfc" r.headers["content-type"] = "text/html; charset=utf8" r.text = u"ü" assert r.raw_content == b"\xc3\xbc" assert r.headers["content-length"] == "2"
def test_get_cookies_twocookies(self): resp = tutils.tresp() resp.headers = http.Headers([["Set-Cookie", "cookiename=cookievalue"], ["Set-Cookie", "othercookie=othervalue"]]) result = resp.get_cookies() assert len(result) == 2 assert "cookiename" in result assert result["cookiename"][0] == ["cookievalue", odict.ODict()] assert "othercookie" in result assert result["othercookie"][0] == ["othervalue", odict.ODict()]
def test_get_cookies_twocookies(self): resp = tresp() resp.headers = Headers([[b"Set-Cookie", b"cookiename=cookievalue"], [b"Set-Cookie", b"othercookie=othervalue"]]) result = resp.cookies assert len(result) == 2 assert "cookiename" in result assert result["cookiename"] == ("cookievalue", CookieAttrs()) assert "othercookie" in result assert result["othercookie"] == ("othervalue", CookieAttrs())
def flow(self, resp_content=b'message'): times = dict( timestamp_start=746203272, timestamp_end=746203272, ) # Create a dummy flow for testing return tutils.tflow( req=netutils.treq(method=b'GET', **times), resp=netutils.tresp(content=resp_content, **times) )
def test_get_cookies_twocookies(self): h = odict.ODictCaseless() h["Set-Cookie"] = ["cookiename=cookievalue", "othercookie=othervalue"] resp = tutils.tresp() resp.headers = h result = resp.get_cookies() assert len(result) == 2 assert "cookiename" in result assert result["cookiename"][0] == ["cookievalue", odict.ODict()] assert "othercookie" in result assert result["othercookie"][0] == ["othervalue", odict.ODict()]
def test_cannot_decode(self): r = tresp() assert r.encode("gzip") r.content = b"foo" with decoded(r): assert r.headers["content-encoding"] assert r.content == b"foo" assert r.headers["content-encoding"] assert r.content != b"foo" r.decode() assert r.content == b"foo"
def test_modify(self): r = tresp() assert "content-encoding" not in r.headers assert r.encode("gzip") with decoded(r): r.content = b"foo" assert r.content != b"foo" r.decode() assert r.content == b"foo"
def test_content_length_update(self): resp = tresp() resp.content = b"foo" assert resp.data.content == b"foo" assert resp.headers["content-length"] == "3" resp.content = b"" assert resp.data.content == b"" assert resp.headers["content-length"] == "0" resp.raw_content = b"bar" assert resp.data.content == b"bar" assert resp.headers["content-length"] == "0"
def test_get_cookies_no_value(self): h = odict.ODictCaseless() h["Set-Cookie"] = [ "cookiename=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/" ] resp = tutils.tresp() resp.headers = h result = resp.get_cookies() assert len(result) == 1 assert "cookiename" in result assert result["cookiename"][0][0] == "" assert len(result["cookiename"][0][1]) == 2
def test_modify(self): r = tresp() assert "content-encoding" not in r.headers r.encode("gzip") r.content = b"foo" assert r.raw_content != b"foo" r.decode() assert r.raw_content == b"foo" with tutils.raises(TypeError): r.content = u"foo"
def test_stripped_chunked_encoding_no_content(): """ https://github.com/mitmproxy/mitmproxy/issues/186 """ r = tutils.treq(content="") r.headers["Transfer-Encoding"] = "chunked" assert "Content-Length" in mock_protocol()._assemble_request_headers(r) r = tutils.tresp(content="") r.headers["Transfer-Encoding"] = "chunked" assert "Content-Length" in mock_protocol()._assemble_response_headers(r)
def test_get_cookies_with_parameters(self): resp = tresp() resp.headers = Headers(set_cookie="cookiename=cookievalue;domain=example.com;expires=Wed Oct 21 16:29:41 2015;path=/; HttpOnly") result = resp.cookies assert len(result) == 1 assert "cookiename" in result assert result["cookiename"][0] == "cookievalue" attrs = result["cookiename"][1] assert len(attrs) == 4 assert attrs["domain"] == "example.com" assert attrs["expires"] == "Wed Oct 21 16:29:41 2015" assert attrs["path"] == "/" assert attrs["httponly"] is None
def test_simple(self): r = tresp(content=b'\xfc') assert r.raw_content == b"\xfc" assert r.content == b"\xfc" assert r.text == u"ü" r.encode("gzip") assert r.text == u"ü" r.decode() assert r.text == u"ü" r.headers["content-type"] = "text/html; charset=latin1" r.content = b"\xc3\xbc" assert r.text == u"ü" r.headers["content-type"] = "text/html; charset=utf8" assert r.text == u"ü"
def test_cannot_encode(self): r = tresp() r.encode("gzip") r.content = None assert r.headers["content-encoding"] assert r.raw_content is None r.headers["content-encoding"] = "zopfli" r.content = b"foo" assert "content-encoding" not in r.headers assert r.raw_content == b"foo" with tutils.raises(ValueError): r.encode("zopfli") assert r.raw_content == b"foo" assert "content-encoding" not in r.headers
def test_get_cookies_with_parameters(self): h = odict.ODictCaseless() h["Set-Cookie"] = [ "cookiename=cookievalue;domain=example.com;expires=Wed Oct 21 16:29:41 2015;path=/; HttpOnly"] resp = tutils.tresp() resp.headers = h result = resp.get_cookies() assert len(result) == 1 assert "cookiename" in result assert result["cookiename"][0][0] == "cookievalue" attrs = result["cookiename"][0][1] assert len(attrs) == 4 assert attrs["domain"] == ["example.com"] assert attrs["expires"] == ["Wed Oct 21 16:29:41 2015"] assert attrs["path"] == ["/"] assert attrs["httponly"] == [None]
def test_cannot_decode(self): r = tresp() r.encode("gzip") r.raw_content = b"foo" with tutils.raises(ValueError): assert r.content assert r.headers["content-encoding"] assert r.get_content(strict=False) == b"foo" with tutils.raises(ValueError): r.decode() assert r.raw_content == b"foo" assert "content-encoding" in r.headers r.decode(strict=False) assert r.content == b"foo" assert "content-encoding" not in r.headers
def test_refresh(self): r = tresp() n = time.time() r.headers["date"] = email.utils.formatdate(n) pre = r.headers["date"] r.refresh(n) assert pre == r.headers["date"] r.refresh(n + 60) d = email.utils.parsedate_tz(r.headers["date"]) d = email.utils.mktime_tz(d) # Weird that this is not exact... assert abs(60 - (d - n)) <= 1 cookie = "MOO=BAR; Expires=Tue, 08-Mar-2011 00:20:38 GMT; Path=foo.com; Secure" r.headers["set-cookie"] = cookie r.refresh() # Cookie refreshing is tested in test_cookies, we just make sure that it's triggered here. assert cookie != r.headers["set-cookie"]
def test_har_extractor(): with tutils.raises(script.ScriptException): with example("har_extractor.py") as ex: pass times = dict( timestamp_start=746203272, timestamp_end=746203272, ) flow = tutils.tflow(req=netutils.treq(**times), resp=netutils.tresp(**times)) with example("har_extractor.py -") as ex: ex.run("response", flow) with open(tutils.test_data.path("data/har_extractor.har")) as fp: test_data = json.load(fp) assert json.loads( ex.ctx.HARLog.json()) == test_data["test_response"]
def test_headers(self): _test_passthrough_attr(tresp(), "headers")
def test_content_basic(self): _test_passthrough_attr(tresp(), "content")
def test_timestamp_end(self): _test_passthrough_attr(tresp(), "timestamp_end")
def teste_http_version(self): _test_decoded_attr(tresp(), "http_version")
def test_add_header(self): m, _ = tscript("add_header.py") f = tutils.tflow(resp=netutils.tresp()) m.response(f) assert f.response.headers["newheader"] == "foo"
def test_timestamp_start(self): _test_passthrough_attr(tresp(), "timestamp_start")