Skip to content

Commit

Permalink
Merge f84f4bf into ddcc792
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Jun 18, 2019
2 parents ddcc792 + f84f4bf commit 381fe61
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ exclude_lines = def hug
sys.stdout.buffer.write
class Socket
pragma: no cover
except ImportError:
if MARSHMALLOW_MAJOR_VERSION is None or MARSHMALLOW_MAJOR_VERSION == 2:
3 changes: 1 addition & 2 deletions hug/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,7 @@ def version_router(
self, request, response, api_version=None, versions=None, not_found=None, **kwargs
):
"""Intelligently routes a request to the correct handler based on the version being requested"""
if versions is None:
versions = {}
versions = {} if versions is None else versions
request_version = self.determine_version(request, api_version)
if request_version:
request_version = int(request_version)
Expand Down
2 changes: 1 addition & 1 deletion hug/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ def render_content(self, content, context, request, response, **kwargs):
if size:
response.set_stream(content, size)
else:
response.stream = content
response.stream = content # pragma: no cover
else:
response.data = content

Expand Down
2 changes: 1 addition & 1 deletion hug/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ def allow_origins(
response_headers = {}
if origins:

@hug.response_middleware()
@hug.response_middleware(api=self.route.get("api", None))
def process_data(request, response, resource):
if "ORIGIN" in request.headers:
origin = request.headers["ORIGIN"]
Expand Down
24 changes: 13 additions & 11 deletions tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1042,17 +1042,14 @@ def extend_with():
def extend_with():
return (tests.module_fake_http_and_cli,)

assert hug.test.cli("sub_api", "made_up_go", api=api)

# But not both
with pytest.raises(ValueError):

@hug.extend_api(sub_command="sub_api", command_prefix="api_", http=False)
def extend_with():
return (tests.module_fake_http_and_cli,)


def test_extending_api_with_http_and_cli():
def test_extending_api_with_http_and_cli_sub_module():
"""Test to ensure it's possible to extend the current API so both HTTP and CLI APIs are extended"""
import tests.module_fake_http_and_cli

Expand Down Expand Up @@ -1579,20 +1576,25 @@ def test_multiple_cli(ints: hug.types.Multiple[int]() = []):
assert hug.test.cli(test_multiple_cli, ints=["1", "2", "3"]) == [1, 2, 3]


def test_startup():
def test_startup(hug_api):
"""Test to ensure hug startup decorators work as expected"""
happened_on_startup = []

@hug.startup()
@hug.startup(api=hug_api)
def happens_on_startup(api):
pass
happened_on_startup.append("non-async")

@hug.startup()
@hug.startup(api=hug_api)
@asyncio.coroutine
def async_happens_on_startup(api):
pass
happened_on_startup.append("async")

assert happens_on_startup in hug_api.startup_handlers
assert async_happens_on_startup in hug_api.startup_handlers

assert happens_on_startup in api.startup_handlers
assert async_happens_on_startup in api.startup_handlers
hug_api._ensure_started()
assert "async" in happened_on_startup
assert "non-async" in happened_on_startup


@pytest.mark.skipif(sys.platform == "win32", reason="Currently failing on Windows build")
Expand Down
13 changes: 13 additions & 0 deletions tests/test_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,16 @@ def test_prefixes(self):
def test_suffixes(self):
"""Test to ensure setting suffixes works as expected"""
assert self.route.suffixes(".js", ".xml").route["suffixes"] == (".js", ".xml")

def test_allow_origins_request_handling(self, hug_api):
"""Test to ensure a route with allowed origins works as expected"""
route = URLRouter(api=hug_api)
test_headers = route.allow_origins(
"google.com", methods=("GET", "POST"), credentials=True, headers="OPTIONS", max_age=10
)

@test_headers.get()
def my_endpoint():
return "Success"

assert hug.test.get(hug_api, "/my_endpoint", headers={'ORIGIN': 'google.com'}).data == "Success"
4 changes: 4 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ def test_json():
with pytest.raises(ValueError):
hug.types.json("Invalid JSON")

assert hug.types.json(json.dumps(["a", "b"]).split(",")) == ["a", "b"]
with pytest.raises(ValueError):
assert hug.types.json(["Invalid JSON", "Invalid JSON"])


def test_multi():
"""Test to ensure that the multi type correctly handles a variety of value types"""
Expand Down
12 changes: 6 additions & 6 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ envlist=py{35,36,37,py3}-marshmallow{2,3}, cython-marshmallow{2,3}
deps=
-rrequirements/build_common.txt
marshmallow2: marshmallow <3.0
marshmallow3: marshmallow >=3.0.0rc5
marshmallow3: marshmallow >=3.0.0rc5<3.0.0rc7<3.0.0rc7

whitelist_externals=flake8
commands=py.test --cov-report html --cov hug -n auto tests

[testenv:py37-black]
deps=
-rrequirements/build_style_tools.txt
marshmallow >=3.0.0rc5
marshmallow >=3.0.0rc5<3.0.0rc7

whitelist_externals=flake8
commands=black --check --verbose -l 100 hug

[testenv:py37-vulture]
deps=
-rrequirements/build_style_tools.txt
marshmallow >=3.0.0rc5
marshmallow >=3.0.0rc5<3.0.0rc7

whitelist_externals=flake8
commands=vulture hug --min-confidence 100 --ignore-names req_succeeded
Expand All @@ -30,23 +30,23 @@ commands=vulture hug --min-confidence 100 --ignore-names req_succeeded
[testenv:py37-flake8]
deps=
-rrequirements/build_style_tools.txt
marshmallow >=3.0.0rc5
marshmallow >=3.0.0rc5<3.0.0rc7

whitelist_externals=flake8
commands=flake8 hug

[testenv:py37-bandit]
deps=
-rrequirements/build_style_tools.txt
marshmallow >=3.0.0rc5
marshmallow >=3.0.0rc5<3.0.0rc7

whitelist_externals=flake8
commands=bandit -r hug/ -ll

[testenv:py37-isort]
deps=
-rrequirements/build_style_tools.txt
marshmallow >=3.0.0rc5
marshmallow >=3.0.0rc5<3.0.0rc7

whitelist_externals=flake8
commands=isort -c --diff --recursive hug
Expand Down

0 comments on commit 381fe61

Please sign in to comment.