Skip to content

Commit

Permalink
Fix header matcher for boto3 (fixes #474) (#488)
Browse files Browse the repository at this point in the history
  • Loading branch information
simahawk authored and neozenith committed Nov 3, 2019
1 parent d843d2a commit 8e78666
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Changelog
- Fix exception when body is empty (@keithprickett)
- Add `pytest-recording` to the documentation as an alternative Pytest plugin (@Stranger6667)
- Fix yarl and python3.5 version issue (@neozenith)
- Fix header matcher for boto3 - fixes #474 (@simahawk)
- 2.1.0 - Add a `rewind` method to reset a cassette (thanks @khamidou)
New error message with more details on why the cassette failed to play a request (thanks @arthurHamon2, @neozenith)
Handle connect tunnel URI (thanks @jeking3)
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/test_matchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ def test_uri_matcher():
b"<member><name>a</name><value><string>1</string></value></member>"
b"</struct></value></data></array></value></param></params></methodCall>"
)
boto3_bytes_headers = {
"X-Amz-Content-SHA256": b"UNSIGNED-PAYLOAD",
"Cache-Control": b"max-age=31536000, public",
"X-Amz-Date": b"20191102T143910Z",
"User-Agent": b"Boto3/1.9.102 Python/3.5.3 Linux/4.15.0-54-generic Botocore/1.12.253 Resource",
"Content-MD5": b"GQqjEXsRqrPyxfTl99nkAg==",
"Content-Type": b"text/plain",
"Expect": b"100-continue",
"Content-Length": "21",
}


@pytest.mark.parametrize(
Expand Down Expand Up @@ -110,6 +120,11 @@ def test_uri_matcher():
"POST", "http://host.com/", '{"b": 2, "a": 1}', {"content-type": "application/json"}
),
),
(
# special case for boto3 bytes headers
request.Request("POST", "http://aws.custom.com/", b"123", boto3_bytes_headers),
request.Request("POST", "http://aws.custom.com/", b"123", boto3_bytes_headers),
),
],
)
def test_body_matcher_does_match(r1, r2):
Expand Down
5 changes: 4 additions & 1 deletion vcr/matchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ def headers(r1, r2):

def _header_checker(value, header="Content-Type"):
def checker(headers):
return value in headers.get(header, "").lower()
_header = headers.get(header, "")
if isinstance(_header, bytes):
_header = _header.decode("utf-8")
return value in _header.lower()

return checker

Expand Down

0 comments on commit 8e78666

Please sign in to comment.