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

Commit

Permalink
Fix header comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
vfaronov committed Jul 31, 2016
1 parent 255bc05 commit 34ba6e1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
22 changes: 20 additions & 2 deletions httpolice/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,27 @@ def __contains__(self, other):
return any(val == other for val in self)

def _compare(self, other, op):
# It would be nice to be able to compare headers by values, as in::
#
# response.headers.etag == request.headers.if_match
#
# Unfortunately, there are places
# (such as :meth:`httpolice.blackboard.Blackboard.complain`)
# where we need header-to-header equality to be less magical
# (see ``test/combined_data/1277_4.https``).
# And if we can't do this magic for equality,
# there's no sense in doing it for other operations.
# So we just say that comparing headers to headers is `NotImplemented`
# (fall back to comparing their object identities).
#
# Now, the following form still works::
#
# response.headers.etag == request.headers.if_match.value
#
# so we don't lose all that much.

if isinstance(other, HeaderView):
return self.is_okay and other.is_okay and \
op(self.value, other.value)
return NotImplemented
else:
return self.is_okay and okay(other) and op(self.value, other)

Expand Down
4 changes: 2 additions & 2 deletions httpolice/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ def check_response_itself(resp):
st.too_many_requests]:
complain(1113)

if headers.date < headers.last_modified:
if headers.date < headers.last_modified.value:
complain(1118)

if status == st.not_modified:
Expand Down Expand Up @@ -628,7 +628,7 @@ def check_response_in_context(resp, req):
for tag in req.headers.if_none_match):
complain(1121)

elif req.headers.if_modified_since >= resp.headers.last_modified:
elif req.headers.if_modified_since >= resp.headers.last_modified.value:
complain(1123)

if status in [st.not_modified, st.precondition_failed]:
Expand Down
33 changes: 33 additions & 0 deletions test/combined_data/1277_4.https
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
1277 1277 1277 1277 1277

# There was a bug here: 1277 was not reported for ``X-Accepted-Oauth-Scopes``
# because it compared equal to ``X-Oauth-Scopes``.

======== BEGIN INBOUND STREAM ========
POST /auth/github HTTP/1.1
host: api.travis-ci.org
User-Agent: Travis/1.8.2 (Ubuntu 16.04 like Linux; Ruby 2.3.1-p112; RubyGems 2.5.1; command login) Faraday/0.9.2 Typhoeus/0.8.0
Accept: application/vnd.travis-ci.2+json
Content-Type: application/x-www-form-urlencoded
Content-Length: 53

github_token=f67aaa7bd2a485b42255ca0f14cce4c116496523======== BEGIN OUTBOUND STREAM ========
HTTP/1.1 200 OK
Connection: keep-alive
Server: nginx
Date: Sun, 31 Jul 2016 11:27:38 GMT
Content-Type: application/json
Content-Length: 43
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Expose-Headers: Content-Type, Cache-Control, Expires, Etag, Last-Modified
Strict-Transport-Security: max-age=31536000
X-Endpoint: Travis::Api::App::Endpoint::Authorization
X-Pattern: /github
X-Oauth-Scopes: public
X-Accepted-Oauth-Scopes: public
Vary: Accept,Accept-Encoding
X-Rack-Cache: pass
Via: 1.1 vegur

{"access_token":"VvBfwHnpjL_swMZs_su4WW"}

0 comments on commit 34ba6e1

Please sign in to comment.