Skip to content

Commit

Permalink
Merge pull request #61 from unt-libraries/fix-26
Browse files Browse the repository at this point in the history
Allow HEAD Requests to app_agent and app_event.
  • Loading branch information
damonkelley committed Feb 10, 2016
2 parents 87569e1 + 03d9b40 commit 89e0a0f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
8 changes: 8 additions & 0 deletions premis_event_service/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,10 @@ def app_event(request, identifier=None):
resp = HttpResponse(atomText, content_type="application/atom+xml")
resp.status_code = 200
return resp

elif request.method == 'HEAD':
return HttpResponse(content_type="application/atom+xml")

elif request.method == 'DELETE' and identifier:
# attempt to retrieve record -- error if unable
try:
Expand Down Expand Up @@ -627,6 +631,8 @@ def app_agent(request, identifier=None):
resp.status_code = 201
resp['Location'] = agent_object.agent_identifier + '/'
return resp
elif request.method == 'HEAD':
return HttpResponse(content_type="application/atom+xml")
else:
return HttpResponseBadRequest("Invalid method for this URL.")
# identifier supplied, will be a GET or DELETE
Expand Down Expand Up @@ -668,3 +674,5 @@ def app_agent(request, identifier=None):
resp = HttpResponse(returnText, content_type="application/atom+xml")
resp.status_code = 200
return resp
elif request.method == 'HEAD':
return HttpResponse(content_type="application/atom+xml")
73 changes: 73 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,42 @@ def test_get_with_identifier_response_content(self, rf):
response = views.app_agent(request, agent.agent_identifier)
assert agent.agent_identifier in response.content

def test_head_without_identifier(self, rf):
request = rf.head('/')
response = views.app_agent(request)
assert response.content == '', 'The message body must be empty'
assert response.status_code == 200

def test_head_and_get_headers_match_without_identifier(self, rf):
head_request, get_request = rf.head('/'), rf.get('/')

head_response = views.app_agent(head_request)
get_response = views.app_agent(get_request)

expected_headers = get_response.serialize_headers()
actual_headers = head_response.serialize_headers()

assert expected_headers == actual_headers, 'The response headers do not match.'

def test_head_with_identifier(self, rf):
agent = factories.AgentFactory.create()
request = rf.head('/')
response = views.app_agent(request, agent.agent_identifier)
assert response.content == '', 'The message body must be empty'
assert response.status_code == 200

def test_head_and_get_headers_match_with_identifier(self, rf):
agent = factories.AgentFactory.create()
head_request, get_request = rf.head('/'), rf.get('/')

head_response = views.app_agent(head_request, agent.agent_identifier)
get_response = views.app_agent(get_request, agent.agent_identifier)

expected_headers = get_response.serialize_headers()
actual_headers = head_response.serialize_headers()

assert expected_headers == actual_headers, 'The response headers do not match.'


class TestAppEvent:
"""Tests for views.app_event."""
Expand Down Expand Up @@ -600,6 +636,43 @@ def test_delete_removes_event(self, rf):
views.app_event(request, event.event_identifier)
assert models.Agent.objects.count() == 0

def test_head_without_identifier(self, rf):
request = rf.head('/')
response = views.app_event(request)
assert response.content == '', 'The message body must be empty'
assert response.status_code == 200

def test_head_and_get_headers_match_without_identifier(self, rf):
head_request, get_request = rf.head('/'), rf.get('/')

head_response = views.app_event(head_request)
get_response = views.app_event(get_request)

expected_headers = get_response.serialize_headers()
actual_headers = head_response.serialize_headers()

assert expected_headers == actual_headers, 'The response headers do not match.'

def test_head_with_identifier(self, rf):
event = factories.EventFactory.create()
request = rf.head('/')
response = views.app_event(request, event.event_identifier)
assert response.content == '', 'The message body must be empty'
assert response.status_code == 200

def test_head_and_get_headers_match_with_identifier(self, rf):
event = factories.EventFactory.create()

head_request = rf.head('/', HTTP_HOST='example.com')
get_request = rf.get('/', HTTP_HOST='example.com')

head_response = views.app_event(head_request, event.event_identifier)
get_response = views.app_event(get_request, event.event_identifier)

expected_headers = get_response.serialize_headers()
actual_headers = head_response.serialize_headers()

assert expected_headers == actual_headers, 'The response headers do not match.'

class TestEventSearch:
"""Tests for views.event_search."""
Expand Down

0 comments on commit 89e0a0f

Please sign in to comment.