Skip to content

Commit

Permalink
Return ETag header with 304 response
Browse files Browse the repository at this point in the history
RFC 7232, section 4.1 specifies that when returning a 304 status code the
ETag header must also be included if it would have been sent in a 200 (OK)
response.
  • Loading branch information
Jeroen Bogers committed Mar 20, 2018
1 parent ad6f6d1 commit a154cd1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 3 additions & 1 deletion httpbin/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,9 @@ def etag(etag):

if if_none_match:
if etag in if_none_match or '*' in if_none_match:
return status_code(304)
response = status_code(304)
response.headers['ETag'] = etag
return response
elif if_match:
if etag not in if_match and '*' not in if_match:
return status_code(412)
Expand Down
9 changes: 9 additions & 0 deletions test_httpbin.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,62 +685,71 @@ def test_etag_if_none_match_matches(self):
headers={ 'If-None-Match': 'abc' }
)
self.assertEqual(response.status_code, 304)
self.assertEqual(response.headers.get('ETag'), 'abc')

def test_etag_if_none_match_matches_list(self):
response = self.app.get(
'/etag/abc',
headers={ 'If-None-Match': '"123", "abc"' }
)
self.assertEqual(response.status_code, 304)
self.assertEqual(response.headers.get('ETag'), 'abc')

def test_etag_if_none_match_matches_star(self):
response = self.app.get(
'/etag/abc',
headers={ 'If-None-Match': '*' }
)
self.assertEqual(response.status_code, 304)
self.assertEqual(response.headers.get('ETag'), 'abc')

def test_etag_if_none_match_w_prefix(self):
response = self.app.get(
'/etag/c3piozzzz',
headers={ 'If-None-Match': 'W/"xyzzy", W/"r2d2xxxx", W/"c3piozzzz"' }
)
self.assertEqual(response.status_code, 304)
self.assertEqual(response.headers.get('ETag'), 'c3piozzzz')

def test_etag_if_none_match_has_no_match(self):
response = self.app.get(
'/etag/abc',
headers={ 'If-None-Match': '123' }
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers.get('ETag'), 'abc')

def test_etag_if_match_matches(self):
response = self.app.get(
'/etag/abc',
headers={ 'If-Match': 'abc' }
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers.get('ETag'), 'abc')

def test_etag_if_match_matches_list(self):
response = self.app.get(
'/etag/abc',
headers={ 'If-Match': '"123", "abc"' }
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers.get('ETag'), 'abc')

def test_etag_if_match_matches_star(self):
response = self.app.get(
'/etag/abc',
headers={ 'If-Match': '*' }
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers.get('ETag'), 'abc')

def test_etag_if_match_has_no_match(self):
response = self.app.get(
'/etag/abc',
headers={ 'If-Match': '123' }
)
self.assertEqual(response.status_code, 412)
self.assertNotIn('ETag', response.headers)

def test_etag_with_no_headers(self):
response = self.app.get(
Expand Down

0 comments on commit a154cd1

Please sign in to comment.