mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-26 02:10:59 +00:00
asgi: refactor tests to new proxy core
This commit is contained in:
parent
df164f42e1
commit
47f8e1d115
@ -1,21 +1,22 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
import sys
|
|
||||||
from unittest import mock
|
import pytest
|
||||||
|
|
||||||
import flask
|
import flask
|
||||||
import pytest
|
|
||||||
from flask import request
|
from flask import request
|
||||||
|
|
||||||
from .. import tservers
|
|
||||||
from mitmproxy.addons import asgiapp
|
from mitmproxy.addons import asgiapp
|
||||||
from mitmproxy.test import tflow
|
from mitmproxy.addons.proxyserver import Proxyserver
|
||||||
|
from mitmproxy.addons import next_layer
|
||||||
|
from mitmproxy.test import taddons
|
||||||
|
|
||||||
tapp = flask.Flask(__name__)
|
tapp = flask.Flask(__name__)
|
||||||
|
|
||||||
|
|
||||||
@tapp.route("/")
|
@tapp.route("/")
|
||||||
def hello():
|
def hello():
|
||||||
|
print("CALLED")
|
||||||
return "testapp"
|
return "testapp"
|
||||||
|
|
||||||
|
|
||||||
@ -40,45 +41,49 @@ async def noresponseapp(scope, receive, send):
|
|||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
class TestApp(tservers.HTTPProxyTest):
|
@pytest.mark.asyncio
|
||||||
def addons(self):
|
async def test_asgi_full():
|
||||||
return [
|
ps = Proxyserver()
|
||||||
asgiapp.WSGIApp(tapp, "testapp", 80),
|
addons = [
|
||||||
asgiapp.ASGIApp(errapp, "errapp", 80),
|
asgiapp.WSGIApp(tapp, "testapp", 80),
|
||||||
asgiapp.ASGIApp(noresponseapp, "noresponseapp", 80),
|
asgiapp.ASGIApp(errapp, "errapp", 80),
|
||||||
]
|
asgiapp.ASGIApp(noresponseapp, "noresponseapp", 80),
|
||||||
|
]
|
||||||
|
with taddons.context(ps, *addons) as tctx:
|
||||||
|
tctx.master.addons.add(next_layer.NextLayer())
|
||||||
|
tctx.configure(ps, listen_host="127.0.0.1", listen_port=0)
|
||||||
|
ps.running()
|
||||||
|
assert await tctx.master.await_log("Proxy server listening", level="info")
|
||||||
|
proxy_addr = ps.server.sockets[0].getsockname()[:2]
|
||||||
|
|
||||||
def test_simple(self):
|
reader, writer = await asyncio.open_connection(*proxy_addr)
|
||||||
p = self.pathoc()
|
req = f"GET http://testapp:80/ HTTP/1.1\r\n\r\n"
|
||||||
with p.connect():
|
writer.write(req.encode())
|
||||||
ret = p.request("get:'http://testapp/'")
|
header = await reader.readuntil(b"\r\n\r\n")
|
||||||
assert b"testapp" in ret.content
|
assert header.startswith(b"HTTP/1.1 200 OK")
|
||||||
|
body = await reader.readuntil(b"testapp")
|
||||||
|
assert body == b"testapp"
|
||||||
|
|
||||||
def test_parameters(self):
|
reader, writer = await asyncio.open_connection(*proxy_addr)
|
||||||
p = self.pathoc()
|
req = f"GET http://testapp:80/parameters?param1=1¶m2=2 HTTP/1.1\r\n\r\n"
|
||||||
with p.connect():
|
writer.write(req.encode())
|
||||||
ret = p.request("get:'http://testapp/parameters?param1=1¶m2=2'")
|
header = await reader.readuntil(b"\r\n\r\n")
|
||||||
assert b'{"param1": "1", "param2": "2"}' == ret.data.content
|
assert header.startswith(b"HTTP/1.1 200 OK")
|
||||||
|
body = await reader.readuntil(b"}")
|
||||||
|
assert body == b'{"param1": "1", "param2": "2"}'
|
||||||
|
|
||||||
def test_app_err(self):
|
reader, writer = await asyncio.open_connection(*proxy_addr)
|
||||||
p = self.pathoc()
|
req = f"GET http://errapp:80/?foo=bar HTTP/1.1\r\n\r\n"
|
||||||
with p.connect():
|
writer.write(req.encode())
|
||||||
ret = p.request("get:'http://errapp/?foo=bar'")
|
header = await reader.readuntil(b"\r\n\r\n")
|
||||||
assert ret.status_code == 500
|
assert header.startswith(b"HTTP/1.1 500")
|
||||||
assert b"ASGI Error" in ret.content
|
body = await reader.readuntil(b"ASGI Error")
|
||||||
|
assert body == b"ASGI Error"
|
||||||
|
|
||||||
def test_app_no_response(self):
|
reader, writer = await asyncio.open_connection(*proxy_addr)
|
||||||
p = self.pathoc()
|
req = f"GET http://noresponseapp:80/ HTTP/1.1\r\n\r\n"
|
||||||
with p.connect():
|
writer.write(req.encode())
|
||||||
ret = p.request("get:'http://noresponseapp/'")
|
header = await reader.readuntil(b"\r\n\r\n")
|
||||||
assert ret.status_code == 500
|
assert header.startswith(b"HTTP/1.1 500")
|
||||||
assert b"ASGI Error" in ret.content
|
body = await reader.readuntil(b"ASGI Error")
|
||||||
|
assert body == b"ASGI Error"
|
||||||
@pytest.mark.skipif(sys.version_info < (3, 8), reason='requires Python 3.8 or higher')
|
|
||||||
def test_app_not_serve_loading_flows(self):
|
|
||||||
with mock.patch('mitmproxy.addons.asgiapp.serve') as mck:
|
|
||||||
flow = tflow.tflow()
|
|
||||||
flow.request.host = "testapp"
|
|
||||||
flow.request.port = 80
|
|
||||||
asyncio.run_coroutine_threadsafe(self.master.load_flow(flow), self.master.channel.loop).result()
|
|
||||||
mck.assert_not_awaited()
|
|
||||||
|
Loading…
Reference in New Issue
Block a user