Skip to content

Commit

Permalink
fix: Escaping header values in VCR cassettes
Browse files Browse the repository at this point in the history
Ref: #783
  • Loading branch information
Stranger6667 committed Oct 3, 2020
1 parent 239c7d5 commit e4b0f11
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
4 changes: 3 additions & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ Changelog

**Fixed**

- Fix ``User-Agent`` header overriding the passed one. `#757`_
- ``User-Agent`` header overriding the passed one. `#757`_
- Default ``User-Agent`` header in ``Case.call``. `#717`_
- Status of individual interactions in VCR cassettes. Before this change, all statuses were taken from the overall test outcome,
rather than from the check results for a particular response. `#695`_
- Escaping header values in VCR cassettes. `#783`_

**Changed**

Expand Down Expand Up @@ -1378,6 +1379,7 @@ Deprecated
.. _0.3.0: https://github.com/schemathesis/schemathesis/compare/v0.2.0...v0.3.0
.. _0.2.0: https://github.com/schemathesis/schemathesis/compare/v0.1.0...v0.2.0

.. _#783: https://github.com/schemathesis/schemathesis/issues/783
.. _#768: https://github.com/schemathesis/schemathesis/issues/768
.. _#757: https://github.com/schemathesis/schemathesis/issues/757
.. _#748: https://github.com/schemathesis/schemathesis/issues/748
Expand Down
3 changes: 2 additions & 1 deletion src/schemathesis/cli/cassettes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import base64
import json
import re
import sys
import threading
Expand Down Expand Up @@ -103,7 +104,7 @@ def worker(file_handle: click.utils.LazyFile, queue: Queue) -> None:
stream = file_handle.open()

def format_header_values(values: List[str]) -> str:
return "\n".join(f" - '{v}'" for v in values)
return "\n".join(f" - {json.dumps(v)}" for v in values)

def format_headers(headers: Dict[str, List[str]]) -> str:
return "\n".join(f" {name}:\n{format_header_values(values)}" for name, values in headers.items())
Expand Down
20 changes: 20 additions & 0 deletions test/cli/test_cassettes.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,26 @@ async def test_replay(openapi_version, cli, schema_url, app, reset_app, cassette
compare_headers(request, serialized["headers"])


@pytest.mark.endpoints("headers")
async def test_headers_serialization(cli, openapi2_schema_url, cassette_path):
# See GH-783
# When headers contain control characters that are not directly representable in YAML
result = cli.run(
openapi2_schema_url,
f"--store-network-log={cassette_path}",
"--hypothesis-max-examples=100",
"--hypothesis-seed=1",
"--validate-schema=false",
)
# Then tests should pass
assert result.exit_code == ExitCode.OK, result.stdout
# And cassette can be replayed
result = cli.replay(str(cassette_path))
assert result.exit_code == ExitCode.OK, result.stdout
# And should be loadable
load_cassette(cassette_path)


def test_multiple_cookies(base_url):
headers = {"User-Agent": USER_AGENT}
response = requests.get(f"{base_url}/success", cookies={"foo": "bar", "baz": "spam"}, headers=headers)
Expand Down
6 changes: 6 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ def schema_url(server, app):
return f"http://127.0.0.1:{server['port']}/schema.yaml"


@pytest.fixture()
def openapi2_schema_url(server, openapi_2_app):
"""URL of the schema of the running application."""
return f"http://127.0.0.1:{server['port']}/schema.yaml"


@pytest.fixture()
def openapi3_schema_url(server, openapi_3_app):
"""URL of the schema of the running application."""
Expand Down

0 comments on commit e4b0f11

Please sign in to comment.