Allow sanic test client to bind to a random port (#1376)

This commit is contained in:
Daniel Thorn
2019-03-04 13:23:03 -08:00
committed by Stephen Sadowski
parent 348964fe12
commit d5813152ab
3 changed files with 72 additions and 10 deletions

View File

@@ -0,0 +1,34 @@
import socket
from sanic.testing import PORT, SanicTestClient
from sanic.response import json, text
# ------------------------------------------------------------ #
# UTF-8
# ------------------------------------------------------------ #
def test_test_client_port_none(app):
@app.get('/get')
def handler(request):
return text('OK')
test_client = SanicTestClient(app, port=None)
request, response = test_client.get('/get')
assert response.text == 'OK'
request, response = test_client.post('/get')
assert response.status == 405
def test_test_client_port_default(app):
@app.get('/get')
def handler(request):
return json(request.transport.get_extra_info('sockname')[1])
test_client = SanicTestClient(app)
assert test_client.port == PORT
request, response = test_client.get('/get')
assert response.json == PORT