Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #156 from lendingblock/master
Browse files Browse the repository at this point in the history
1.2.1
  • Loading branch information
lsbardel authored Jan 14, 2019
2 parents afb36a6 + 30c6774 commit aeac115
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion openapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Minimal OpenAPI asynchronous server application
"""

__version__ = '1.2.0'
__version__ = '1.2.1'
2 changes: 1 addition & 1 deletion openapi/db/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ async def get_list(
async with self.db.ensure_connection(conn) as conn:
total = await conn.fetchrow(sql_count, *args_count)
values = await conn.fetch(sql, *args)
pagination = Pagination(self.request.url)
pagination = Pagination(self.full_url())
data = self.dump(dump_schema, values)
return pagination.paginated(
data, total['tbl_row_count'], offset, limit
Expand Down
5 changes: 5 additions & 0 deletions openapi/spec/hdrs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from multidict import istr

X_FORWARDED_PROTO = istr('X-Forwarded-Proto')
X_FORWARDED_HOST = istr('X-Forwarded-Host')
X_FORWARDED_PORT = istr('X-Forwarded-Port')
14 changes: 14 additions & 0 deletions openapi/spec/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

from multidict import MultiDict

from yarl import URL

from openapi.json import loads, dumps

from ..data.dump import dump, dump_list
from ..data.validate import validate
from ..data.exc import ValidationErrors
from ..utils import compact, as_list
from . import hdrs


class ApiPath(web.View):
Expand Down Expand Up @@ -98,6 +102,16 @@ def raiseValidationError(self, message=None, errors=None):
data = self.dump(ValidationErrors, raw)
raise web.HTTPUnprocessableEntity(**self.api_response_data(data))

def full_url(self):
headers = self.request.headers
proto = headers.get(hdrs.X_FORWARDED_PROTO)
host = headers.get(hdrs.X_FORWARDED_HOST)
if proto and host:
url = URL.build(scheme=proto, host=host)
return url.join(self.request.rel_url)
else:
return self.request.url

@classmethod
def api_response_data(cls, data):
return dict(
Expand Down
22 changes: 22 additions & 0 deletions tests/test_pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,25 @@ async def test_invalid_limit_offset(cli):
params={'offset': -10}
)
await jsonBody(response, 422)


async def test_pagination_with_forwarded_host(cli):
response = await cli.post('/tasks', json=dict(title='bla'))
await jsonBody(response, 201)
response = await cli.post('/tasks', json=dict(title='foo'))
await jsonBody(response, 201)
response = await cli.get(
'/tasks',
headers={
'X-Forwarded-Proto': 'https',
'X-Forwarded-Host': 'whenbeer.pub'
},
params={'limit': 10, 'offset': 20}
)
data = await jsonBody(response)
assert len(data) == 0
link = response.headers['Link']
assert link == (
f'<https://whenbeer.pub/tasks?limit=10&offset=0> rel="first", '
f'<https://whenbeer.pub/tasks?limit=10&offset=10> rel="prev"'
)

0 comments on commit aeac115

Please sign in to comment.