2021-01-28 07:22:22 +00:00
|
|
|
from sanic_testing.testing import PORT, SanicTestClient
|
|
|
|
|
2019-03-04 21:23:03 +00:00
|
|
|
from sanic.response import json, text
|
2019-04-23 22:44:42 +01:00
|
|
|
|
2019-03-04 21:23:03 +00:00
|
|
|
# ------------------------------------------------------------ #
|
|
|
|
# UTF-8
|
|
|
|
# ------------------------------------------------------------ #
|
|
|
|
|
|
|
|
|
|
|
|
def test_test_client_port_none(app):
|
2019-04-23 22:44:42 +01:00
|
|
|
@app.get("/get")
|
2019-03-04 21:23:03 +00:00
|
|
|
def handler(request):
|
2019-04-23 22:44:42 +01:00
|
|
|
return text("OK")
|
2019-03-04 21:23:03 +00:00
|
|
|
|
|
|
|
test_client = SanicTestClient(app, port=None)
|
|
|
|
|
2019-04-23 22:44:42 +01:00
|
|
|
request, response = test_client.get("/get")
|
|
|
|
assert response.text == "OK"
|
2019-03-04 21:23:03 +00:00
|
|
|
|
2019-04-23 22:44:42 +01:00
|
|
|
request, response = test_client.post("/get")
|
2019-03-04 21:23:03 +00:00
|
|
|
assert response.status == 405
|
|
|
|
|
|
|
|
|
|
|
|
def test_test_client_port_default(app):
|
2019-04-23 22:44:42 +01:00
|
|
|
@app.get("/get")
|
2019-03-04 21:23:03 +00:00
|
|
|
def handler(request):
|
2019-04-23 22:44:42 +01:00
|
|
|
return json(request.transport.get_extra_info("sockname")[1])
|
2019-03-04 21:23:03 +00:00
|
|
|
|
|
|
|
test_client = SanicTestClient(app)
|
2020-03-26 04:42:46 +00:00
|
|
|
assert test_client.port == PORT # Can be None before request
|
2019-03-04 21:23:03 +00:00
|
|
|
|
2019-04-23 22:44:42 +01:00
|
|
|
request, response = test_client.get("/get")
|
2020-03-26 04:42:46 +00:00
|
|
|
assert test_client.port > 0
|
|
|
|
assert response.json == test_client.port
|