Skip to content

Commit

Permalink
fix: fix User-Agent header overriding the passed one
Browse files Browse the repository at this point in the history
  • Loading branch information
hebertjulio authored and Stranger6667 committed Oct 3, 2020
1 parent 512dff4 commit d80402f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Changelog

**Fixed**

- Fix ``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`_
Expand Down Expand Up @@ -1378,6 +1379,7 @@ Deprecated
.. _0.2.0: https://github.com/schemathesis/schemathesis/compare/v0.1.0...v0.2.0

.. _#768: https://github.com/schemathesis/schemathesis/issues/768
.. _#757: https://github.com/schemathesis/schemathesis/issues/757
.. _#748: https://github.com/schemathesis/schemathesis/issues/748
.. _#738: https://github.com/schemathesis/schemathesis/issues/738
.. _#734: https://github.com/schemathesis/schemathesis/issues/734
Expand Down
12 changes: 9 additions & 3 deletions src/schemathesis/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ def from_uri(
**kwargs: Any,
) -> BaseOpenAPISchema:
"""Load a remote resource and parse to schema instance."""
kwargs.setdefault("headers", {}).setdefault("User-Agent", USER_AGENT)
headers = kwargs.setdefault("headers", {})
if "user-agent" not in {header.lower() for header in headers}:
kwargs["headers"]["User-Agent"] = USER_AGENT
response = requests.get(uri, **kwargs)
try:
response.raise_for_status()
Expand Down Expand Up @@ -192,7 +194,9 @@ def from_wsgi(
validate_schema: bool = True,
**kwargs: Any,
) -> BaseOpenAPISchema:
kwargs.setdefault("headers", {}).setdefault("User-Agent", USER_AGENT)
headers = kwargs.setdefault("headers", {})
if "user-agent" not in {header.lower() for header in headers}:
kwargs["headers"]["User-Agent"] = USER_AGENT
client = Client(app, WSGIResponse)
response = client.get(schema_path, **kwargs)
# Raising exception to provide unified behavior
Expand Down Expand Up @@ -259,7 +263,9 @@ def from_asgi(
validate_schema: bool = True,
**kwargs: Any,
) -> BaseOpenAPISchema:
kwargs.setdefault("headers", {}).setdefault("User-Agent", USER_AGENT)
headers = kwargs.setdefault("headers", {})
if "user-agent" not in {header.lower() for header in headers}:
kwargs["headers"]["User-Agent"] = USER_AGENT
client = ASGIClient(app)
response = client.get(schema_path, **kwargs)
# Raising exception to provide unified behavior
Expand Down
2 changes: 1 addition & 1 deletion src/schemathesis/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def _get_headers(self, headers: Optional[Dict[str, str]] = None) -> Dict[str, An
final_headers = self.headers.copy() if self.headers is not None else {}
if headers:
final_headers.update(headers)
if "User-Agent" not in final_headers:
if "user-agent" not in {header.lower() for header in final_headers}:
final_headers["User-Agent"] = USER_AGENT
return final_headers

Expand Down
6 changes: 4 additions & 2 deletions src/schemathesis/runner/impl/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ def network_test(
"""A single test body will be executed against the target."""
# pylint: disable=too-many-arguments
headers = headers or {}
headers.setdefault("User-Agent", USER_AGENT)
if "user-agent" not in {header.lower() for header in headers}:
headers["User-Agent"] = USER_AGENT
timeout = prepare_timeout(request_timeout)
response = _network_test(
case,
Expand Down Expand Up @@ -368,7 +369,8 @@ def _prepare_wsgi_headers(
headers: Optional[Dict[str, Any]], auth: Optional[RawAuth], auth_type: Optional[str]
) -> Dict[str, Any]:
headers = headers or {}
headers.setdefault("User-Agent", USER_AGENT)
if "user-agent" not in {header.lower() for header in headers}:
headers["User-Agent"] = USER_AGENT
wsgi_auth = get_wsgi_auth(auth, auth_type)
if wsgi_auth:
headers["Authorization"] = wsgi_auth
Expand Down
1 change: 1 addition & 0 deletions test/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def test_as_requests_kwargs(override, server, base_url, swagger_20, converter):
(None, {"User-Agent": USER_AGENT, "X-Key": "foo"}),
({"User-Agent": "foo/1.0"}, {"User-Agent": "foo/1.0", "X-Key": "foo"}),
({"X-Value": "bar"}, {"X-Value": "bar", "User-Agent": USER_AGENT, "X-Key": "foo"}),
({"UsEr-agEnT": "foo/1.0"}, {"UsEr-agEnT": "foo/1.0", "X-Key": "foo"}),
),
)
def test_as_requests_kwargs_override_user_agent(server, openapi2_base_url, swagger_20, headers, expected):
Expand Down

0 comments on commit d80402f

Please sign in to comment.