-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow sanic test client to bind to a random port (#1376)
- Loading branch information
1 parent
348964f
commit d581315
Showing
3 changed files
with
72 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |