Skip to content

Commit

Permalink
Merge pull request #26 from imbolc/master
Browse files Browse the repository at this point in the history
the same behavior for returning and raising HTTPException
  • Loading branch information
asvetlov committed Jan 5, 2016
2 parents 32def6f + a0d09ed commit 6aaba3b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 25 deletions.
10 changes: 3 additions & 7 deletions aiohttp_session/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,9 @@ def get_session(request):
return session


def session_middleware(storage, *, max_http_status=400):
def session_middleware(storage):

assert isinstance(storage, AbstractStorage), storage
assert isinstance(max_http_status, int), max_http_status

@asyncio.coroutine
def factory(app, handler):
Expand All @@ -129,8 +128,6 @@ def middleware(request):
try:
response = yield from handler(request)
except web.HTTPException as exc:
if exc.status_code > max_http_status:
raise exc
response = exc
raise_response = True
if not isinstance(response, web.StreamResponse):
Expand All @@ -154,11 +151,10 @@ def middleware(request):
return factory


def setup(app, storage, *, max_http_status=400):
def setup(app, storage):
"""Setup the library in aiohttp fashion."""

app.middlewares.append(
session_middleware(storage, max_http_status=max_http_status))
app.middlewares.append(session_middleware(storage))


class AbstractStorage(metaclass=abc.ABCMeta):
Expand Down
23 changes: 5 additions & 18 deletions tests/test_http_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ def find_unused_port(self):
return port

@asyncio.coroutine
def create_server(self, routes, max_http_status):
middleware = session_middleware(SimpleCookieStorage(),
max_http_status=max_http_status)
def create_server(self, routes):
middleware = session_middleware(SimpleCookieStorage())
app = web.Application(middlewares=[middleware], loop=self.loop)
for method, path, handler in routes:
app.router.add_route(method, path, handler)
Expand Down Expand Up @@ -67,24 +66,12 @@ def get_routes():
]

@asyncio.coroutine
def go_good_http_status():
_, _, url = yield from self.create_server(get_routes(),
max_http_status=400)
def go():
_, _, url = yield from self.create_server(get_routes())
resp = yield from request('GET', url + '/save', loop=self.loop)
self.assertEqual(200, resp.status)
self.assertEqual(resp.url[-5:], '/show')
text = yield from resp.text()
assert text == 'works'

@asyncio.coroutine
def go_bad_http_status():
_, _, url = yield from self.create_server(get_routes(),
max_http_status=200)
resp = yield from request('GET', url + '/save', loop=self.loop)
self.assertEqual(200, resp.status)
self.assertEqual(resp.url[-5:], '/show')
text = yield from resp.text()
assert text == 'None'

self.loop.run_until_complete(go_good_http_status())
self.loop.run_until_complete(go_bad_http_status())
self.loop.run_until_complete(go())

0 comments on commit 6aaba3b

Please sign in to comment.