Reformatted code to use spaces instead of tabs
This commit is contained in:
@@ -4,13 +4,13 @@ import os
|
||||
import inspect
|
||||
|
||||
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
|
||||
sys.path.insert(0,currentdir + '/../../../')
|
||||
sys.path.insert(0, currentdir + '/../../../')
|
||||
|
||||
import timeit
|
||||
|
||||
from sanic.response import json
|
||||
|
||||
print(json({ "test":True }).output())
|
||||
print(json({"test": True}).output())
|
||||
|
||||
print("Running New 100,000 times")
|
||||
times = 0
|
||||
@@ -20,7 +20,7 @@ for n in range(6):
|
||||
print("Took {} seconds".format(time))
|
||||
total_time += time
|
||||
times += 1
|
||||
print("Average: {}".format(total_time/times))
|
||||
print("Average: {}".format(total_time / times))
|
||||
|
||||
print("Running Old 100,000 times")
|
||||
times = 0
|
||||
@@ -30,4 +30,4 @@ for n in range(6):
|
||||
print("Took {} seconds".format(time))
|
||||
total_time += time
|
||||
times += 1
|
||||
print("Average: {}".format(total_time/times))
|
||||
print("Average: {}".format(total_time / times))
|
||||
|
||||
@@ -3,15 +3,17 @@ import os
|
||||
import inspect
|
||||
|
||||
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
|
||||
sys.path.insert(0,currentdir + '/../../../')
|
||||
sys.path.insert(0, currentdir + '/../../../')
|
||||
|
||||
from sanic import Sanic
|
||||
from sanic.response import json
|
||||
|
||||
app = Sanic("test")
|
||||
|
||||
|
||||
@app.route("/")
|
||||
async def test(request):
|
||||
return json({ "test": True })
|
||||
return json({"test": True})
|
||||
|
||||
app.run(host="0.0.0.0", port=sys.argv[1])
|
||||
|
||||
app.run(host="0.0.0.0", port=sys.argv[1])
|
||||
|
||||
@@ -3,7 +3,7 @@ import os
|
||||
import inspect
|
||||
|
||||
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
|
||||
sys.path.insert(0,currentdir + '/../../../')
|
||||
sys.path.insert(0, currentdir + '/../../../')
|
||||
|
||||
from sanic import Sanic
|
||||
from sanic.response import json, text
|
||||
@@ -11,36 +11,44 @@ from sanic.exceptions import ServerError
|
||||
|
||||
app = Sanic("test")
|
||||
|
||||
|
||||
@app.route("/")
|
||||
async def test(request):
|
||||
return json({ "test": True })
|
||||
|
||||
return json({"test": True})
|
||||
|
||||
|
||||
@app.route("/sync", methods=['GET', 'POST'])
|
||||
def test(request):
|
||||
return json({ "test": True })
|
||||
return json({"test": True})
|
||||
|
||||
|
||||
@app.route("/text/<name>/<butt:int>")
|
||||
def rtext(request, name, butt):
|
||||
return text("yeehaww {} {}".format(name, butt))
|
||||
|
||||
|
||||
@app.route("/exception")
|
||||
def exception(request):
|
||||
raise ServerError("yep")
|
||||
|
||||
|
||||
@app.route("/exception/async")
|
||||
async def test(request):
|
||||
raise ServerError("asunk")
|
||||
|
||||
|
||||
@app.route("/post_json")
|
||||
def post_json(request):
|
||||
return json({ "received": True, "message": request.json })
|
||||
return json({"received": True, "message": request.json})
|
||||
|
||||
|
||||
@app.route("/query_string")
|
||||
def query_string(request):
|
||||
return json({ "parsed": True, "args": request.args, "url": request.url, "query_string": request.query_string })
|
||||
return json({"parsed": True, "args": request.args, "url": request.url, "query_string": request.query_string})
|
||||
|
||||
|
||||
import sys
|
||||
|
||||
app.run(host="0.0.0.0", port=sys.argv[1])
|
||||
|
||||
|
||||
@@ -55,7 +63,7 @@ app.run(host="0.0.0.0", port=sys.argv[1])
|
||||
# for n in range(30):
|
||||
# connection = await asyncio_redis.Connection.create(host='192.168.99.100', port=6379)
|
||||
# sanic.redis.append(connection)
|
||||
|
||||
|
||||
|
||||
# c=0
|
||||
# @app.route("/postgres")
|
||||
|
||||
@@ -49,5 +49,3 @@ def test_invalid_usage_exception():
|
||||
def test_not_found_exception():
|
||||
request, response = sanic_endpoint_test(exception_app, uri='/404')
|
||||
assert response.status == 404
|
||||
|
||||
|
||||
|
||||
@@ -4,78 +4,85 @@ from sanic.request import Request
|
||||
from sanic.response import json, text, HTTPResponse
|
||||
from sanic.utils import sanic_endpoint_test
|
||||
|
||||
|
||||
# ------------------------------------------------------------ #
|
||||
# GET
|
||||
# ------------------------------------------------------------ #
|
||||
|
||||
def test_middleware_request():
|
||||
app = Sanic('test_middleware_request')
|
||||
app = Sanic('test_middleware_request')
|
||||
|
||||
results = []
|
||||
@app.middleware
|
||||
async def handler(request):
|
||||
results.append(request)
|
||||
results = []
|
||||
|
||||
@app.route('/')
|
||||
async def handler(request):
|
||||
return text('OK')
|
||||
@app.middleware
|
||||
async def handler(request):
|
||||
results.append(request)
|
||||
|
||||
request, response = sanic_endpoint_test(app)
|
||||
@app.route('/')
|
||||
async def handler(request):
|
||||
return text('OK')
|
||||
|
||||
request, response = sanic_endpoint_test(app)
|
||||
|
||||
assert response.text == 'OK'
|
||||
assert type(results[0]) is Request
|
||||
|
||||
assert response.text == 'OK'
|
||||
assert type(results[0]) is Request
|
||||
|
||||
def test_middleware_response():
|
||||
app = Sanic('test_middleware_response')
|
||||
app = Sanic('test_middleware_response')
|
||||
|
||||
results = []
|
||||
@app.middleware('request')
|
||||
async def process_response(request):
|
||||
results.append(request)
|
||||
@app.middleware('response')
|
||||
async def process_response(request, response):
|
||||
results.append(request)
|
||||
results.append(response)
|
||||
results = []
|
||||
|
||||
@app.route('/')
|
||||
async def handler(request):
|
||||
return text('OK')
|
||||
@app.middleware('request')
|
||||
async def process_response(request):
|
||||
results.append(request)
|
||||
|
||||
request, response = sanic_endpoint_test(app)
|
||||
@app.middleware('response')
|
||||
async def process_response(request, response):
|
||||
results.append(request)
|
||||
results.append(response)
|
||||
|
||||
@app.route('/')
|
||||
async def handler(request):
|
||||
return text('OK')
|
||||
|
||||
request, response = sanic_endpoint_test(app)
|
||||
|
||||
assert response.text == 'OK'
|
||||
assert type(results[0]) is Request
|
||||
assert type(results[1]) is Request
|
||||
assert issubclass(type(results[2]), HTTPResponse)
|
||||
|
||||
assert response.text == 'OK'
|
||||
assert type(results[0]) is Request
|
||||
assert type(results[1]) is Request
|
||||
assert issubclass(type(results[2]), HTTPResponse)
|
||||
|
||||
def test_middleware_override_request():
|
||||
app = Sanic('test_middleware_override_request')
|
||||
app = Sanic('test_middleware_override_request')
|
||||
|
||||
@app.middleware
|
||||
async def halt_request(request):
|
||||
return text('OK')
|
||||
@app.middleware
|
||||
async def halt_request(request):
|
||||
return text('OK')
|
||||
|
||||
@app.route('/')
|
||||
async def handler(request):
|
||||
return text('FAIL')
|
||||
@app.route('/')
|
||||
async def handler(request):
|
||||
return text('FAIL')
|
||||
|
||||
response = sanic_endpoint_test(app, gather_request=False)
|
||||
response = sanic_endpoint_test(app, gather_request=False)
|
||||
|
||||
assert response.status == 200
|
||||
assert response.text == 'OK'
|
||||
|
||||
assert response.status == 200
|
||||
assert response.text == 'OK'
|
||||
|
||||
def test_middleware_override_response():
|
||||
app = Sanic('test_middleware_override_response')
|
||||
app = Sanic('test_middleware_override_response')
|
||||
|
||||
@app.middleware('response')
|
||||
async def process_response(request, response):
|
||||
return text('OK')
|
||||
@app.middleware('response')
|
||||
async def process_response(request, response):
|
||||
return text('OK')
|
||||
|
||||
@app.route('/')
|
||||
async def handler(request):
|
||||
return text('FAIL')
|
||||
@app.route('/')
|
||||
async def handler(request):
|
||||
return text('FAIL')
|
||||
|
||||
request, response = sanic_endpoint_test(app)
|
||||
request, response = sanic_endpoint_test(app)
|
||||
|
||||
assert response.status == 200
|
||||
assert response.text == 'OK'
|
||||
assert response.status == 200
|
||||
assert response.text == 'OK'
|
||||
|
||||
@@ -3,77 +3,80 @@ from sanic import Sanic
|
||||
from sanic.response import json, text
|
||||
from sanic.utils import sanic_endpoint_test
|
||||
|
||||
|
||||
# ------------------------------------------------------------ #
|
||||
# GET
|
||||
# ------------------------------------------------------------ #
|
||||
|
||||
def test_sync():
|
||||
app = Sanic('test_text')
|
||||
app = Sanic('test_text')
|
||||
|
||||
@app.route('/')
|
||||
def handler(request):
|
||||
return text('Hello')
|
||||
@app.route('/')
|
||||
def handler(request):
|
||||
return text('Hello')
|
||||
|
||||
request, response = sanic_endpoint_test(app)
|
||||
request, response = sanic_endpoint_test(app)
|
||||
|
||||
assert response.text == 'Hello'
|
||||
|
||||
assert response.text == 'Hello'
|
||||
|
||||
def test_text():
|
||||
app = Sanic('test_text')
|
||||
app = Sanic('test_text')
|
||||
|
||||
@app.route('/')
|
||||
async def handler(request):
|
||||
return text('Hello')
|
||||
@app.route('/')
|
||||
async def handler(request):
|
||||
return text('Hello')
|
||||
|
||||
request, response = sanic_endpoint_test(app)
|
||||
request, response = sanic_endpoint_test(app)
|
||||
|
||||
assert response.text == 'Hello'
|
||||
assert response.text == 'Hello'
|
||||
|
||||
|
||||
def test_json():
|
||||
app = Sanic('test_json')
|
||||
app = Sanic('test_json')
|
||||
|
||||
@app.route('/')
|
||||
async def handler(request):
|
||||
return json({"test":True})
|
||||
@app.route('/')
|
||||
async def handler(request):
|
||||
return json({"test": True})
|
||||
|
||||
request, response = sanic_endpoint_test(app)
|
||||
request, response = sanic_endpoint_test(app)
|
||||
|
||||
try:
|
||||
results = json_loads(response.text)
|
||||
except:
|
||||
raise ValueError("Expected JSON response but got '{}'".format(response))
|
||||
try:
|
||||
results = json_loads(response.text)
|
||||
except:
|
||||
raise ValueError("Expected JSON response but got '{}'".format(response))
|
||||
|
||||
assert results.get('test') == True
|
||||
assert results.get('test') == True
|
||||
|
||||
|
||||
def test_query_string():
|
||||
app = Sanic('test_query_string')
|
||||
app = Sanic('test_query_string')
|
||||
|
||||
@app.route('/')
|
||||
async def handler(request):
|
||||
return text('OK')
|
||||
@app.route('/')
|
||||
async def handler(request):
|
||||
return text('OK')
|
||||
|
||||
request, response = sanic_endpoint_test(app, params=[("test1", 1), ("test2", "false"), ("test2", "true")])
|
||||
request, response = sanic_endpoint_test(app, params=[("test1", 1), ("test2", "false"), ("test2", "true")])
|
||||
|
||||
assert request.args.get('test1') == '1'
|
||||
assert request.args.get('test2') == 'false'
|
||||
|
||||
assert request.args.get('test1') == '1'
|
||||
assert request.args.get('test2') == 'false'
|
||||
|
||||
# ------------------------------------------------------------ #
|
||||
# POST
|
||||
# ------------------------------------------------------------ #
|
||||
|
||||
def test_post_json():
|
||||
app = Sanic('test_post_json')
|
||||
app = Sanic('test_post_json')
|
||||
|
||||
@app.route('/')
|
||||
async def handler(request):
|
||||
return text('OK')
|
||||
@app.route('/')
|
||||
async def handler(request):
|
||||
return text('OK')
|
||||
|
||||
payload = {'test': 'OK'}
|
||||
headers = {'content-type': 'application/json'}
|
||||
payload = {'test': 'OK'}
|
||||
headers = {'content-type': 'application/json'}
|
||||
|
||||
request, response = sanic_endpoint_test(app, data=json_dumps(payload), headers=headers)
|
||||
request, response = sanic_endpoint_test(app, data=json_dumps(payload), headers=headers)
|
||||
|
||||
assert request.json.get('test') == 'OK'
|
||||
assert response.text == 'OK'
|
||||
assert request.json.get('test') == 'OK'
|
||||
assert response.text == 'OK'
|
||||
|
||||
@@ -3,93 +3,97 @@ from sanic import Sanic
|
||||
from sanic.response import json, text
|
||||
from sanic.utils import sanic_endpoint_test
|
||||
|
||||
|
||||
# ------------------------------------------------------------ #
|
||||
# UTF-8
|
||||
# ------------------------------------------------------------ #
|
||||
|
||||
def test_dynamic_route():
|
||||
app = Sanic('test_dynamic_route')
|
||||
app = Sanic('test_dynamic_route')
|
||||
|
||||
results = []
|
||||
results = []
|
||||
|
||||
@app.route('/folder/<name>')
|
||||
async def handler(request, name):
|
||||
results.append(name)
|
||||
return text('OK')
|
||||
@app.route('/folder/<name>')
|
||||
async def handler(request, name):
|
||||
results.append(name)
|
||||
return text('OK')
|
||||
|
||||
request, response = sanic_endpoint_test(app, uri='/folder/test123')
|
||||
request, response = sanic_endpoint_test(app, uri='/folder/test123')
|
||||
|
||||
assert response.text == 'OK'
|
||||
assert results[0] == 'test123'
|
||||
|
||||
assert response.text == 'OK'
|
||||
assert results[0] == 'test123'
|
||||
|
||||
def test_dynamic_route_string():
|
||||
app = Sanic('test_dynamic_route_string')
|
||||
app = Sanic('test_dynamic_route_string')
|
||||
|
||||
results = []
|
||||
results = []
|
||||
|
||||
@app.route('/folder/<name:string>')
|
||||
async def handler(request, name):
|
||||
results.append(name)
|
||||
return text('OK')
|
||||
@app.route('/folder/<name:string>')
|
||||
async def handler(request, name):
|
||||
results.append(name)
|
||||
return text('OK')
|
||||
|
||||
request, response = sanic_endpoint_test(app, uri='/folder/test123')
|
||||
request, response = sanic_endpoint_test(app, uri='/folder/test123')
|
||||
|
||||
assert response.text == 'OK'
|
||||
assert results[0] == 'test123'
|
||||
|
||||
assert response.text == 'OK'
|
||||
assert results[0] == 'test123'
|
||||
|
||||
def test_dynamic_route_int():
|
||||
app = Sanic('test_dynamic_route_int')
|
||||
app = Sanic('test_dynamic_route_int')
|
||||
|
||||
results = []
|
||||
results = []
|
||||
|
||||
@app.route('/folder/<folder_id:int>')
|
||||
async def handler(request, folder_id):
|
||||
results.append(folder_id)
|
||||
return text('OK')
|
||||
@app.route('/folder/<folder_id:int>')
|
||||
async def handler(request, folder_id):
|
||||
results.append(folder_id)
|
||||
return text('OK')
|
||||
|
||||
request, response = sanic_endpoint_test(app, uri='/folder/12345')
|
||||
assert response.text == 'OK'
|
||||
assert type(results[0]) is int
|
||||
request, response = sanic_endpoint_test(app, uri='/folder/12345')
|
||||
assert response.text == 'OK'
|
||||
assert type(results[0]) is int
|
||||
|
||||
request, response = sanic_endpoint_test(app, uri='/folder/asdf')
|
||||
assert response.status == 404
|
||||
request, response = sanic_endpoint_test(app, uri='/folder/asdf')
|
||||
assert response.status == 404
|
||||
|
||||
|
||||
def test_dynamic_route_number():
|
||||
app = Sanic('test_dynamic_route_int')
|
||||
app = Sanic('test_dynamic_route_int')
|
||||
|
||||
results = []
|
||||
results = []
|
||||
|
||||
@app.route('/weight/<weight:number>')
|
||||
async def handler(request, weight):
|
||||
results.append(weight)
|
||||
return text('OK')
|
||||
@app.route('/weight/<weight:number>')
|
||||
async def handler(request, weight):
|
||||
results.append(weight)
|
||||
return text('OK')
|
||||
|
||||
request, response = sanic_endpoint_test(app, uri='/weight/12345')
|
||||
assert response.text == 'OK'
|
||||
assert type(results[0]) is float
|
||||
request, response = sanic_endpoint_test(app, uri='/weight/12345')
|
||||
assert response.text == 'OK'
|
||||
assert type(results[0]) is float
|
||||
|
||||
request, response = sanic_endpoint_test(app, uri='/weight/1234.56')
|
||||
assert response.status == 200
|
||||
request, response = sanic_endpoint_test(app, uri='/weight/1234.56')
|
||||
assert response.status == 200
|
||||
|
||||
request, response = sanic_endpoint_test(app, uri='/weight/1234-56')
|
||||
assert response.status == 404
|
||||
|
||||
request, response = sanic_endpoint_test(app, uri='/weight/1234-56')
|
||||
assert response.status == 404
|
||||
|
||||
def test_dynamic_route_regex():
|
||||
app = Sanic('test_dynamic_route_int')
|
||||
app = Sanic('test_dynamic_route_int')
|
||||
|
||||
@app.route('/folder/<folder_id:[A-Za-z0-9]{0,4}>')
|
||||
async def handler(request, folder_id):
|
||||
return text('OK')
|
||||
@app.route('/folder/<folder_id:[A-Za-z0-9]{0,4}>')
|
||||
async def handler(request, folder_id):
|
||||
return text('OK')
|
||||
|
||||
request, response = sanic_endpoint_test(app, uri='/folder/test')
|
||||
assert response.status == 200
|
||||
request, response = sanic_endpoint_test(app, uri='/folder/test')
|
||||
assert response.status == 200
|
||||
|
||||
request, response = sanic_endpoint_test(app, uri='/folder/test1')
|
||||
assert response.status == 404
|
||||
request, response = sanic_endpoint_test(app, uri='/folder/test1')
|
||||
assert response.status == 404
|
||||
|
||||
request, response = sanic_endpoint_test(app, uri='/folder/test-123')
|
||||
assert response.status == 404
|
||||
request, response = sanic_endpoint_test(app, uri='/folder/test-123')
|
||||
assert response.status == 404
|
||||
|
||||
request, response = sanic_endpoint_test(app, uri='/folder/')
|
||||
assert response.status == 200
|
||||
request, response = sanic_endpoint_test(app, uri='/folder/')
|
||||
assert response.status == 200
|
||||
|
||||
@@ -3,52 +3,56 @@ from sanic import Sanic
|
||||
from sanic.response import json, text
|
||||
from sanic.utils import sanic_endpoint_test
|
||||
|
||||
|
||||
# ------------------------------------------------------------ #
|
||||
# UTF-8
|
||||
# ------------------------------------------------------------ #
|
||||
|
||||
def test_utf8_query_string():
|
||||
app = Sanic('test_utf8_query_string')
|
||||
app = Sanic('test_utf8_query_string')
|
||||
|
||||
@app.route('/')
|
||||
async def handler(request):
|
||||
return text('OK')
|
||||
@app.route('/')
|
||||
async def handler(request):
|
||||
return text('OK')
|
||||
|
||||
request, response = sanic_endpoint_test(app, params=[("utf8", '✓')])
|
||||
assert request.args.get('utf8') == '✓'
|
||||
|
||||
request, response = sanic_endpoint_test(app, params=[("utf8", '✓')])
|
||||
assert request.args.get('utf8') == '✓'
|
||||
|
||||
def test_utf8_response():
|
||||
app = Sanic('test_utf8_response')
|
||||
app = Sanic('test_utf8_response')
|
||||
|
||||
@app.route('/')
|
||||
async def handler(request):
|
||||
return text('✓')
|
||||
@app.route('/')
|
||||
async def handler(request):
|
||||
return text('✓')
|
||||
|
||||
request, response = sanic_endpoint_test(app)
|
||||
assert response.text == '✓'
|
||||
|
||||
request, response = sanic_endpoint_test(app)
|
||||
assert response.text == '✓'
|
||||
|
||||
def skip_test_utf8_route():
|
||||
app = Sanic('skip_test_utf8_route')
|
||||
app = Sanic('skip_test_utf8_route')
|
||||
|
||||
@app.route('/')
|
||||
async def handler(request):
|
||||
return text('OK')
|
||||
@app.route('/')
|
||||
async def handler(request):
|
||||
return text('OK')
|
||||
|
||||
# UTF-8 Paths are not supported
|
||||
request, response = sanic_endpoint_test(app, route='/✓', uri='/✓')
|
||||
assert response.text == 'OK'
|
||||
|
||||
# UTF-8 Paths are not supported
|
||||
request, response = sanic_endpoint_test(app, route='/✓', uri='/✓')
|
||||
assert response.text == 'OK'
|
||||
|
||||
def test_utf8_post_json():
|
||||
app = Sanic('test_utf8_post_json')
|
||||
app = Sanic('test_utf8_post_json')
|
||||
|
||||
@app.route('/')
|
||||
async def handler(request):
|
||||
return text('OK')
|
||||
@app.route('/')
|
||||
async def handler(request):
|
||||
return text('OK')
|
||||
|
||||
payload = {'test': '✓'}
|
||||
headers = {'content-type': 'application/json'}
|
||||
payload = {'test': '✓'}
|
||||
headers = {'content-type': 'application/json'}
|
||||
|
||||
request, response = sanic_endpoint_test(app, data=json_dumps(payload), headers=headers)
|
||||
request, response = sanic_endpoint_test(app, data=json_dumps(payload), headers=headers)
|
||||
|
||||
assert request.json.get('test') == '✓'
|
||||
assert response.text == 'OK'
|
||||
assert request.json.get('test') == '✓'
|
||||
assert response.text == 'OK'
|
||||
|
||||
Reference in New Issue
Block a user