Skip to content

Commit

Permalink
headers: dont add link header to search & record response
Browse files Browse the repository at this point in the history
  • Loading branch information
MJedr committed Mar 1, 2023
1 parent 30966b6 commit 0d2ee17
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 33 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ env:

jobs:
Test:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
strategy:
matrix:
python-version: [ 3.6, 3.7, 3.8, 3.9 ]
python-version: [ 3.6, 3.8 ]
requirements-level: [min, pypi]
services:
postgres:
Expand Down
6 changes: 0 additions & 6 deletions invenio_records_rest/serializers/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ def view(pid, record, code=200, headers=None, links_factory=None):
if headers is not None:
response.headers.extend(headers)

if links_factory is not None:
add_link_header(response, links_factory(pid))

return response

return view
Expand All @@ -63,9 +60,6 @@ def view(pid_fetcher, search_result, code=200, headers=None, links=None,
if headers is not None:
response.headers.extend(headers)

if links is not None:
add_link_header(response, links)

return response

return view
Expand Down
4 changes: 4 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
'invenio-indexer>=1.0.0',
'invenio-config>=1.0.2',
'pytest-invenio>=1.2.1',
'pydocstyle<=6.1.1',
'jsonref==0.2',
'jsonresolver==0.2.1'
]

invenio_search_version = '1.2.3,<1.3.0'
Expand All @@ -48,6 +51,7 @@
],
'docs': [
'Sphinx>=3',
'MarkupSafe==2.0.1'
],
'dublincore': [
'dcxml>=0.1.0',
Expand Down
11 changes: 9 additions & 2 deletions tests/test_serializer_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,17 @@ def test_record_responsify(app):

pid = PersistentIdentifier(pid_type='rec', pid_value='1')
rec = Record({'title': 'test'})
resp = rec_serializer(pid, rec, headers=[('X-Test', 'test')])
resp = rec_serializer(
pid,
rec,
headers=[('X-Test', 'test')],
links_factory=lambda x: str(x)
)
assert resp.status_code == 200
assert resp.content_type == 'application/x-custom'
assert resp.get_data(as_text=True) == "1:test"
assert resp.headers['X-Test'] == 'test'
assert not resp.headers.get('Link')

resp = rec_serializer(pid, rec, code=201)
assert resp.status_code == 201
Expand All @@ -62,6 +68,7 @@ def fetcher():
assert resp.get_data(as_text=True) == "5"

resp = search_serializer(
fetcher, result, code=201, headers=[('X-Test', 'test')])
fetcher, result, code=201, headers=[('X-Test', 'test')], links="test")
assert resp.status_code == 201
assert resp.headers['X-Test'] == 'test'
assert not resp.headers.get("Link")
32 changes: 9 additions & 23 deletions tests/test_views_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,39 +134,25 @@ def test_page_links(app, indexed_records, search_url):
res = client.get(search_url, query_string=dict(size=1, page=1))
assert_hits_len(res, 1)

def parse_link_header(response):
"""Parse the links from a REST response's HTTP header."""
return {
k: v for (k, v) in
map(lambda s: re.findall(r'<(.*)>; rel="(.*)"', s)[0][::-1],
[x for x in res.headers['Link'].split(', ')])
}

links = parse_link_header(res)
data = get_json(res)['links']
assert 'self' in data \
and 'self' in links \
and data['self'] == links['self']
assert 'next' in data \
and 'next' in links \
and data['next'] == links['next']
assert 'prev' not in data \
and 'prev' not in links
assert 'self' in data
assert 'next' in data
assert 'prev' not in data

# Assert next URL before calling it
first_url = links['self']
next_url = links['next']
first_url = data['self']
next_url = data['next']
parsed_url = parse_url(next_url)
assert parsed_url['qs']['size'] == ['1']
assert parsed_url['qs']['page'] == ['2']

# Access next URL
res = client.get(to_relative_url(next_url))
assert_hits_len(res, 1)
links = parse_link_header(res)
assert links['self'] == next_url
assert 'next' in links
assert 'prev' in links and links['prev'] == first_url
data = get_json(res)['links']
assert data['self'] == next_url
assert 'next' in data
assert 'prev' in data and data['prev'] == first_url


def test_query(app, indexed_records, search_url):
Expand Down

0 comments on commit 0d2ee17

Please sign in to comment.