2018-12-27 17:20:58 +00:00
|
|
|
import inspect
|
|
|
|
|
2018-11-11 16:57:57 +00:00
|
|
|
from sanic import helpers
|
2018-12-26 23:28:42 +00:00
|
|
|
from sanic.config import Config
|
2018-12-26 23:19:54 +00:00
|
|
|
import pytest
|
2018-11-07 20:29:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_has_message_body():
|
|
|
|
tests = (
|
|
|
|
(100, False),
|
|
|
|
(102, False),
|
|
|
|
(204, False),
|
|
|
|
(200, True),
|
|
|
|
(304, False),
|
|
|
|
(400, True),
|
|
|
|
)
|
|
|
|
for status_code, expected in tests:
|
2018-11-11 16:57:57 +00:00
|
|
|
assert helpers.has_message_body(status_code) is expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_is_entity_header():
|
|
|
|
tests = (
|
|
|
|
("allow", True),
|
|
|
|
("extension-header", True),
|
|
|
|
("", False),
|
|
|
|
("test", False),
|
|
|
|
)
|
|
|
|
for header, expected in tests:
|
|
|
|
assert helpers.is_entity_header(header) is expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_is_hop_by_hop_header():
|
|
|
|
tests = (
|
|
|
|
("connection", True),
|
|
|
|
("upgrade", True),
|
|
|
|
("", False),
|
|
|
|
("test", False),
|
|
|
|
)
|
|
|
|
for header, expected in tests:
|
|
|
|
assert helpers.is_hop_by_hop_header(header) is expected
|
2018-11-19 15:30:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_remove_entity_headers():
|
|
|
|
tests = (
|
2018-12-30 11:18:06 +00:00
|
|
|
({}, {}),
|
|
|
|
({"Allow": "GET, POST, HEAD"}, {}),
|
2018-11-19 15:30:53 +00:00
|
|
|
(
|
|
|
|
{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"Expires": "Wed, 21 Oct 2015 07:28:00 GMT",
|
2018-12-30 11:18:06 +00:00
|
|
|
"Foo": "Bar",
|
2018-11-19 15:30:53 +00:00
|
|
|
},
|
2018-12-30 11:18:06 +00:00
|
|
|
{"Expires": "Wed, 21 Oct 2015 07:28:00 GMT", "Foo": "Bar"},
|
2018-11-19 15:30:53 +00:00
|
|
|
),
|
|
|
|
(
|
2018-12-30 11:18:06 +00:00
|
|
|
{"Allow": "GET, POST, HEAD", "Content-Location": "/test"},
|
|
|
|
{"Content-Location": "/test"},
|
2018-11-19 15:30:53 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
for header, expected in tests:
|
|
|
|
assert helpers.remove_entity_headers(header) == expected
|
2018-12-26 23:19:54 +00:00
|
|
|
|
|
|
|
|
2018-12-27 17:20:58 +00:00
|
|
|
def test_import_string_class():
|
2018-12-26 23:19:54 +00:00
|
|
|
obj = helpers.import_string('sanic.config.Config')
|
|
|
|
assert isinstance(obj, Config)
|
|
|
|
|
|
|
|
|
2018-12-27 17:20:58 +00:00
|
|
|
def test_import_string_module():
|
|
|
|
module = helpers.import_string('sanic.config')
|
|
|
|
assert inspect.ismodule(module)
|
|
|
|
|
|
|
|
|
2018-12-26 23:19:54 +00:00
|
|
|
def test_import_string_exception():
|
|
|
|
with pytest.raises(ImportError):
|
|
|
|
helpers.import_string('test.test.test')
|