本文整理汇总了Python中mitmproxy.test.tutils.tresp函数的典型用法代码示例。如果您正苦于以下问题:Python tresp函数的具体用法?Python tresp怎么用?Python tresp使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了tresp函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_storage_bodies
async def test_storage_bodies(self):
# Need to test for configure
# Need to test for set_order
s = self.start_session(fp=0.5)
f = self.tft()
f2 = self.tft(start=1)
f.request.content = b"A" * 1001
s.request(f)
s.request(f2)
await asyncio.sleep(1.0)
content = s.db_store.con.execute(
"SELECT type_id, content FROM body WHERE body.flow_id == (?);", [f.id]
).fetchall()[0]
assert content == (1, b"A" * 1001)
assert s.db_store.body_ledger == {f.id}
f.response = http.HTTPResponse.wrap(tutils.tresp(content=b"A" * 1001))
f2.response = http.HTTPResponse.wrap(tutils.tresp(content=b"A" * 1001))
# Content length is wrong for some reason -- quick fix
f.response.headers['content-length'] = b"1001"
f2.response.headers['content-length'] = b"1001"
s.response(f)
s.response(f2)
await asyncio.sleep(1.0)
rows = s.db_store.con.execute(
"SELECT type_id, content FROM body WHERE body.flow_id == (?);", [f.id]
).fetchall()
assert len(rows) == 1
rows = s.db_store.con.execute(
"SELECT type_id, content FROM body WHERE body.flow_id == (?);", [f2.id]
).fetchall()
assert len(rows) == 1
assert s.db_store.body_ledger == {f.id}
assert all([lf.__dict__ == rf.__dict__ for lf, rf in list(zip(s.load_view(), [f, f2]))])
示例2: test_eq
def test_eq(self):
data = tutils.tresp(timestamp_start=42, timestamp_end=42).data
same = tutils.tresp(timestamp_start=42, timestamp_end=42).data
assert data == same
other = tutils.tresp(content=b"foo").data
assert data != other
assert data != 0
示例3: test_eq_ne
def test_eq_ne(self):
resp = tutils.tresp(timestamp_start=42, timestamp_end=42)
same = tutils.tresp(timestamp_start=42, timestamp_end=42)
assert resp == same
other = tutils.tresp(timestamp_start=0, timestamp_end=0)
assert resp != other
assert resp != 0
示例4: test_expected_http_body_size
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 pytest.raises(exceptions.HttpSyntaxException):
expected_http_body_size(
treq(headers=Headers(content_length=val))
)
assert expected_http_body_size(
treq(headers=Headers(content_length="42"))
) == 42
# more than 1 content-length headers with same value
assert expected_http_body_size(
treq(headers=Headers([(b'content-length', b'42'), (b'content-length', b'42')]))
) == 42
# more than 1 content-length headers with conflicting value
with pytest.raises(exceptions.HttpSyntaxException):
expected_http_body_size(
treq(headers=Headers([(b'content-length', b'42'), (b'content-length', b'45')]))
)
# no length
assert expected_http_body_size(
treq(headers=Headers())
) == 0
assert expected_http_body_size(
treq(headers=Headers()), tresp(headers=Headers())
) == -1
示例5: test_assemble_response
def test_assemble_response():
assert 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(exceptions.HttpException):
assemble_response(tresp(content=None))
示例6: test_init
def test_init(self):
with pytest.raises(ValueError):
tresp(headers="foobar")
with pytest.raises(UnicodeEncodeError):
tresp(http_version="föö/bä.r")
with pytest.raises(UnicodeEncodeError):
tresp(reason="fööbär")
with pytest.raises(ValueError):
tresp(content="foobar")
assert isinstance(tresp(headers=()).headers, Headers)
示例7: test_update_content_length_header
def test_update_content_length_header(self):
r = tutils.tresp()
assert int(r.headers["content-length"]) == 7
r.encode("gzip")
assert int(r.headers["content-length"]) == 27
r.decode()
assert int(r.headers["content-length"]) == 7
示例8: test_response
def test_response(self, get_request_invuln, logger):
mocked_flow = tflow.tflow(
req=tutils.treq(path=b"index.html?q=1"),
resp=tutils.tresp(content=b'<html></html>')
)
xss.response(mocked_flow)
assert logger.args == []
示例9: test_none
def test_none(self):
r = tutils.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
示例10: test_response
def test_response(self, monkeypatch, logger):
logger.args = []
monkeypatch.setattr("mitmproxy.ctx.log", logger)
monkeypatch.setattr(requests, 'get', self.mocked_requests_invuln)
mocked_flow = tflow.tflow(req=tutils.treq(path=b"index.html?q=1"), resp=tutils.tresp(content=b'<html></html>'))
xss.response(mocked_flow)
assert logger.args == []
示例11: test_get_cookies_simple
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"] == ("cookievalue", CookieAttrs())
示例12: test_storage_flush_with_specials
async def test_storage_flush_with_specials(self):
s = self.start_session(fp=0.5)
f = self.tft()
s.request(f)
await asyncio.sleep(1)
assert len(s._hot_store) == 0
f.response = http.HTTPResponse.wrap(tutils.tresp())
s.response(f)
assert len(s._hot_store) == 1
assert s.load_storage() == [f]
await asyncio.sleep(1)
assert all([lflow.__dict__ == flow.__dict__ for lflow, flow in list(zip(s.load_storage(), [f]))])
f.server_conn.via = tflow.tserver_conn()
s.request(f)
await asyncio.sleep(0.6)
assert len(s._hot_store) == 0
assert all([lflow.__dict__ == flow.__dict__ for lflow, flow in list(zip(s.load_storage(), [f]))])
flows = [self.tft() for _ in range(500)]
s.update(flows)
await asyncio.sleep(0.6)
assert s._flush_period == s._FP_DEFAULT * s._FP_DECREMENT
await asyncio.sleep(3)
assert s._flush_period == s._FP_DEFAULT
示例13: tflow
def tflow(client_conn=True, server_conn=True, req=True, resp=None, err=None):
"""
@type client_conn: bool | None | mitmproxy.proxy.connection.ClientConnection
@type server_conn: bool | None | mitmproxy.proxy.connection.ServerConnection
@type req: bool | None | mitmproxy.proxy.protocol.http.HTTPRequest
@type resp: bool | None | mitmproxy.proxy.protocol.http.HTTPResponse
@type err: bool | None | mitmproxy.proxy.protocol.primitives.Error
@return: mitmproxy.proxy.protocol.http.HTTPFlow
"""
if client_conn is True:
client_conn = tclient_conn()
if server_conn is True:
server_conn = tserver_conn()
if req is True:
req = tutils.treq()
if resp is True:
resp = tutils.tresp()
if err is True:
err = terr()
if req:
req = http.HTTPRequest.wrap(req)
if resp:
resp = http.HTTPResponse.wrap(resp)
f = http.HTTPFlow(client_conn, server_conn)
f.request = req
f.response = resp
f.error = err
f.reply = controller.DummyReply()
return f
示例14: test_unknown_ce
def test_unknown_ce(self):
r = tutils.tresp()
r.headers["content-type"] = "text/html; charset=wtf"
r.raw_content = b"foo"
with pytest.raises(ValueError):
assert r.text == u"foo"
assert r.get_text(strict=False) == u"foo"
示例15: test_set_cookies
def test_set_cookies(self):
resp = tresp()
resp.cookies["foo"] = ("bar", {})
assert len(resp.cookies) == 1
assert resp.cookies["foo"] == ("bar", CookieAttrs())
resp.cookies = [["one", ("uno", CookieAttrs())], ["two", ("due", CookieAttrs())]]
assert list(resp.cookies.keys()) == ["one", "two"]