diff --git a/CHANGELOG.md b/CHANGELOG.md
index 05d3cef..b9d5f0e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
+## [0.24.0] - 2023-09-04
+### Added
+- Added `match_json` parameter which allows matching on JSON decoded body (matching against python representation instead of bytes).
+
+### Changed
+- Even if it was never documented as a feature, the `match_headers` parameter was not considering header names case when matching.
+ - As this might have been considered a feature by some users, the fact that `match_headers` will now respect casing is documented as a breaking change.
+
+### Fixed
+- Matching on headers does not ignore name case anymore, the name must now be cased as sent (as some servers might expect a specific case).
+- Error message in case a request does not match will now include request headers with mismatching name case as well.
+- Error message in case a request does not match will now include request headers when not provided as lower-cased to `match_headers`.
+- Add `:Any` type hint to `**matchers` function arguments to satisfy strict type checking mode in [`pyright`](https://microsoft.github.io/pyright/#/).
+
## [0.23.1] - 2023-08-02
### Fixed
- Version `0.23.0` introduced a regression removing the support for mutating json content provided in `httpx_mock.add_response`.
@@ -262,7 +276,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- First release, should be considered as unstable for now as design might change.
-[Unreleased]: https://github.com/Colin-b/pytest_httpx/compare/v0.23.1...HEAD
+[Unreleased]: https://github.com/Colin-b/pytest_httpx/compare/v0.24.0...HEAD
+[0.24.0]: https://github.com/Colin-b/pytest_httpx/compare/v0.23.1...v0.24.0
[0.23.1]: https://github.com/Colin-b/pytest_httpx/compare/v0.23.0...v0.23.1
[0.23.0]: https://github.com/Colin-b/pytest_httpx/compare/v0.22.0...v0.23.0
[0.22.0]: https://github.com/Colin-b/pytest_httpx/compare/v0.21.3...v0.22.0
diff --git a/README.md b/README.md
index 11cd0d0..3343571 100644
--- a/README.md
+++ b/README.md
@@ -5,7 +5,7 @@
-
+
@@ -158,7 +158,7 @@ from pytest_httpx import HTTPXMock
def test_headers_matching(httpx_mock: HTTPXMock):
- httpx_mock.add_response(match_headers={'user-agent': 'python-httpx/0.23.0'})
+ httpx_mock.add_response(match_headers={'User-Agent': 'python-httpx/0.24.1'})
with httpx.Client() as client:
response = client.get("https://test_url")
@@ -182,6 +182,33 @@ def test_content_matching(httpx_mock: HTTPXMock):
response = client.post("https://test_url", content=b"This is the body")
```
+##### Matching on HTTP JSON body
+
+Use `match_json` parameter to specify the JSON decoded HTTP body to reply to.
+
+Matching is performed on equality. You can however use `unittest.mock.ANY` to do partial matching.
+
+```python
+import httpx
+from pytest_httpx import HTTPXMock
+from unittest.mock import ANY
+
+def test_json_matching(httpx_mock: HTTPXMock):
+ httpx_mock.add_response(match_json={"a": "json", "b": 2})
+
+ with httpx.Client() as client:
+ response = client.post("https://test_url", json={"a": "json", "b": 2})
+
+
+def test_partial_json_matching(httpx_mock: HTTPXMock):
+ httpx_mock.add_response(match_json={"a": "json", "b": ANY})
+
+ with httpx.Client() as client:
+ response = client.post("https://test_url", json={"a": "json", "b": 2})
+```
+
+Note that `match_content` cannot be provided if `match_json` is also provided.
+
### Add JSON response
Use `json` parameter to add a JSON response using python values.
@@ -515,7 +542,7 @@ def test_timeout(httpx_mock: HTTPXMock):
## Check sent requests
The best way to ensure the content of your requests is still to use the `match_headers` and / or `match_content` parameters when adding a response.
-In the same spirit, ensuring that no request was issued does not necessarily requires any code.
+In the same spirit, ensuring that no request was issued does not necessarily require any code.
In any case, you always have the ability to retrieve the requests that were issued.
diff --git a/pytest_httpx/_httpx_mock.py b/pytest_httpx/_httpx_mock.py
index ca11510..975c531 100644
--- a/pytest_httpx/_httpx_mock.py
+++ b/pytest_httpx/_httpx_mock.py
@@ -1,5 +1,6 @@
import copy
import inspect
+import json
import re
from typing import List, Union, Optional, Callable, Tuple, Pattern, Any, Dict, Awaitable
@@ -15,12 +16,18 @@ def __init__(
method: Optional[str] = None,
match_headers: Optional[Dict[str, Any]] = None,
match_content: Optional[bytes] = None,
+ match_json: Optional[Any] = None,
):
self.nb_calls = 0
self.url = httpx.URL(url) if url and isinstance(url, str) else url
self.method = method.upper() if method else method
self.headers = match_headers
+ if match_content is not None and match_json is not None:
+ raise ValueError(
+ "Only one way of matching against the body can be provided. If you want to match against the JSON decoded representation, use match_json. Otherwise, use match_content."
+ )
self.content = match_content
+ self.json = match_json
def match(self, request: httpx.Request) -> bool:
return (
@@ -57,16 +64,31 @@ def _headers_match(self, request: httpx.Request) -> bool:
if not self.headers:
return True
+ encoding = request.headers.encoding
+ request_headers = {}
+ # Can be cleaned based on the outcome of https://github.com/encode/httpx/discussions/2841
+ for raw_name, raw_value in request.headers.raw:
+ if raw_name in request_headers:
+ request_headers[raw_name] += b", " + raw_value
+ else:
+ request_headers[raw_name] = raw_value
+
return all(
- request.headers.get(header_name) == header_value
+ request_headers.get(header_name.encode(encoding))
+ == header_value.encode(encoding)
for header_name, header_value in self.headers.items()
)
def _content_match(self, request: httpx.Request) -> bool:
- if self.content is None:
+ if self.content is None and self.json is None:
return True
-
- return request.read() == self.content
+ if self.content is not None:
+ return request.read() == self.content
+ try:
+ # httpx._content.encode_json hard codes utf-8 encoding.
+ return json.loads(request.read().decode("utf-8")) == self.json
+ except json.decoder.JSONDecodeError:
+ return False
def __str__(self) -> str:
matcher_description = f"Match {self.method or 'all'} requests"
@@ -76,8 +98,12 @@ def __str__(self) -> str:
matcher_description += f" with {self.headers} headers"
if self.content is not None:
matcher_description += f" and {self.content} body"
+ elif self.json is not None:
+ matcher_description += f" and {self.json} json body"
elif self.content is not None:
matcher_description += f" with {self.content} body"
+ elif self.json is not None:
+ matcher_description += f" with {self.json} json body"
return matcher_description
@@ -100,13 +126,13 @@ def add_response(
self,
status_code: int = 200,
http_version: str = "HTTP/1.1",
- headers: _httpx_internals.HeaderTypes = None,
+ headers: Optional[_httpx_internals.HeaderTypes] = None,
content: Optional[bytes] = None,
text: Optional[str] = None,
html: Optional[str] = None,
stream: Any = None,
json: Any = None,
- **matchers,
+ **matchers: Any,
) -> None:
"""
Mock the response that will be sent if a request match.
@@ -124,6 +150,7 @@ def add_response(
:param method: HTTP method identifying the request(s) to match.
:param match_headers: HTTP headers identifying the request(s) to match. Must be a dictionary.
:param match_content: Full HTTP body identifying the request(s) to match. Must be bytes.
+ :param match_json: JSON decoded HTTP body identifying the request(s) to match. Must be JSON encodable.
"""
json = copy.deepcopy(json) if json is not None else None
@@ -148,7 +175,7 @@ def add_callback(
[httpx.Request],
Union[Optional[httpx.Response], Awaitable[Optional[httpx.Response]]],
],
- **matchers,
+ **matchers: Any,
) -> None:
"""
Mock the action that will take place if a request match.
@@ -160,10 +187,11 @@ def add_callback(
:param method: HTTP method identifying the request(s) to match.
:param match_headers: HTTP headers identifying the request(s) to match. Must be a dictionary.
:param match_content: Full HTTP body identifying the request(s) to match. Must be bytes.
+ :param match_json: JSON decoded HTTP body identifying the request(s) to match. Must be JSON encodable.
"""
self._callbacks.append((_RequestMatcher(**matchers), callback))
- def add_exception(self, exception: Exception, **matchers) -> None:
+ def add_exception(self, exception: Exception, **matchers: Any) -> None:
"""
Raise an exception if a request match.
@@ -173,6 +201,7 @@ def add_exception(self, exception: Exception, **matchers) -> None:
:param method: HTTP method identifying the request(s) to match.
:param match_headers: HTTP headers identifying the request(s) to match. Must be a dictionary.
:param match_content: Full HTTP body identifying the request(s) to match. Must be bytes.
+ :param match_json: JSON decoded HTTP body identifying the request(s) to match. Must be JSON encodable.
"""
def exception_callback(request: httpx.Request) -> None:
@@ -220,19 +249,36 @@ async def _handle_async_request(
def _explain_that_no_response_was_found(self, request: httpx.Request) -> str:
matchers = [matcher for matcher, _ in self._callbacks]
+ headers_encoding = request.headers.encoding
expect_headers = set(
[
- header
+ # httpx uses lower cased header names as internal key
+ header.lower().encode(headers_encoding)
for matcher in matchers
if matcher.headers
for header in matcher.headers
]
)
- expect_body = any([matcher.content is not None for matcher in matchers])
+ expect_body = any(
+ [
+ matcher.content is not None or matcher.json is not None
+ for matcher in matchers
+ ]
+ )
request_description = f"{request.method} request on {request.url}"
if expect_headers:
- request_description += f" with {dict({name: value for name, value in request.headers.items() if name in expect_headers})} headers"
+ present_headers = {}
+ # Can be cleaned based on the outcome of https://github.com/encode/httpx/discussions/2841
+ for name, lower_name, value in request.headers._list:
+ if lower_name in expect_headers:
+ name = name.decode(headers_encoding)
+ if name in present_headers:
+ present_headers[name] += f", {value.decode(headers_encoding)}"
+ else:
+ present_headers[name] = value.decode(headers_encoding)
+
+ request_description += f" with {present_headers} headers"
if expect_body:
request_description += f" and {request.read()} body"
elif expect_body:
@@ -275,28 +321,30 @@ def _get_callback(
matcher.nb_calls += 1
return callback
- def get_requests(self, **matchers) -> List[httpx.Request]:
+ def get_requests(self, **matchers: Any) -> List[httpx.Request]:
"""
Return all requests sent that match (empty list if no requests were matched).
:param url: Full URL identifying the requests to retrieve.
Can be a str, a re.Pattern instance or a httpx.URL instance.
- :param method: HTTP method identifying the requests to retrieve. Must be a upper cased string value.
+ :param method: HTTP method identifying the requests to retrieve. Must be an upper-cased string value.
:param match_headers: HTTP headers identifying the requests to retrieve. Must be a dictionary.
:param match_content: Full HTTP body identifying the requests to retrieve. Must be bytes.
+ :param match_json: JSON decoded HTTP body identifying the requests to retrieve. Must be JSON encodable.
"""
matcher = _RequestMatcher(**matchers)
return [request for request in self._requests if matcher.match(request)]
- def get_request(self, **matchers) -> Optional[httpx.Request]:
+ def get_request(self, **matchers: Any) -> Optional[httpx.Request]:
"""
Return the single request that match (or None).
:param url: Full URL identifying the request to retrieve.
Can be a str, a re.Pattern instance or a httpx.URL instance.
- :param method: HTTP method identifying the request to retrieve. Must be a upper cased string value.
+ :param method: HTTP method identifying the request to retrieve. Must be an upper-cased string value.
:param match_headers: HTTP headers identifying the request to retrieve. Must be a dictionary.
:param match_content: Full HTTP body identifying the request to retrieve. Must be bytes.
+ :param match_json: JSON decoded HTTP body identifying the request to retrieve. Must be JSON encodable.
:raises AssertionError: in case more than one request match.
"""
requests = self.get_requests(**matchers)
diff --git a/pytest_httpx/version.py b/pytest_httpx/version.py
index 3e3aba8..6eb5d9a 100644
--- a/pytest_httpx/version.py
+++ b/pytest_httpx/version.py
@@ -3,4 +3,4 @@
# Major should be incremented in case there is a breaking change. (eg: 2.5.8 -> 3.0.0)
# Minor should be incremented in case there is an enhancement. (eg: 2.5.8 -> 2.6.0)
# Patch should be incremented in case there is a bug fix. (eg: 2.5.8 -> 2.5.9)
-__version__ = "0.23.1"
+__version__ = "0.24.0"
diff --git a/tests/test_httpx_async.py b/tests/test_httpx_async.py
index bf61950..1f1ab99 100644
--- a/tests/test_httpx_async.py
+++ b/tests/test_httpx_async.py
@@ -6,6 +6,7 @@
import httpx
import pytest
from pytest import Testdir
+from unittest.mock import ANY
import pytest_httpx
from pytest_httpx import HTTPXMock
@@ -1085,7 +1086,7 @@ async def test_request_retrieval_with_more_than_one(httpx_mock):
@pytest.mark.asyncio
async def test_headers_matching(httpx_mock: HTTPXMock) -> None:
httpx_mock.add_response(
- match_headers={"user-agent": f"python-httpx/{httpx.__version__}"}
+ match_headers={"User-Agent": f"python-httpx/{httpx.__version__}"}
)
async with httpx.AsyncClient() as client:
@@ -1093,13 +1094,94 @@ async def test_headers_matching(httpx_mock: HTTPXMock) -> None:
assert response.content == b""
+@pytest.mark.asyncio
+async def test_multi_value_headers_matching(httpx_mock: HTTPXMock) -> None:
+ httpx_mock.add_response(match_headers={"my-custom-header": "value1, value2"})
+
+ async with httpx.AsyncClient() as client:
+ response = await client.get(
+ "https://test_url",
+ headers=[("my-custom-header", "value1"), ("my-custom-header", "value2")],
+ )
+ assert response.content == b""
+
+
+@pytest.mark.asyncio
+async def test_multi_value_headers_not_matching_single_value_issued(
+ httpx_mock: HTTPXMock,
+) -> None:
+ httpx_mock.add_response(match_headers={"my-custom-header": "value1"})
+
+ async with httpx.AsyncClient() as client:
+ with pytest.raises(httpx.TimeoutException) as exception_info:
+ await client.get(
+ "https://test_url",
+ headers=[
+ ("my-custom-header", "value1"),
+ ("my-custom-header", "value2"),
+ ],
+ )
+ assert (
+ str(exception_info.value)
+ == """No response can be found for GET request on https://test_url with {'my-custom-header': 'value1, value2'} headers amongst:
+Match all requests with {'my-custom-header': 'value1'} headers"""
+ )
+
+ # Clean up responses to avoid assertion failure
+ httpx_mock.reset(assert_all_responses_were_requested=False)
+
+
+@pytest.mark.asyncio
+async def test_multi_value_headers_not_matching_multi_value_issued(
+ httpx_mock: HTTPXMock,
+) -> None:
+ httpx_mock.add_response(match_headers={"my-custom-header": "value1, value2"})
+
+ async with httpx.AsyncClient() as client:
+ with pytest.raises(httpx.TimeoutException) as exception_info:
+ await client.get(
+ "https://test_url",
+ headers=[
+ ("my-custom-header", "value1"),
+ ("my-custom-header", "value3"),
+ ],
+ )
+ assert (
+ str(exception_info.value)
+ == """No response can be found for GET request on https://test_url with {'my-custom-header': 'value1, value3'} headers amongst:
+Match all requests with {'my-custom-header': 'value1, value2'} headers"""
+ )
+
+ # Clean up responses to avoid assertion failure
+ httpx_mock.reset(assert_all_responses_were_requested=False)
+
+
+@pytest.mark.asyncio
+async def test_headers_matching_respect_case(httpx_mock: HTTPXMock) -> None:
+ httpx_mock.add_response(
+ match_headers={"user-agent": f"python-httpx/{httpx.__version__}"}
+ )
+
+ async with httpx.AsyncClient() as client:
+ with pytest.raises(httpx.TimeoutException) as exception_info:
+ await client.get("https://test_url")
+ assert (
+ str(exception_info.value)
+ == f"""No response can be found for GET request on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}'}} headers amongst:
+Match all requests with {{'user-agent': 'python-httpx/{httpx.__version__}'}} headers"""
+ )
+
+ # Clean up responses to avoid assertion failure
+ httpx_mock.reset(assert_all_responses_were_requested=False)
+
+
@pytest.mark.asyncio
async def test_headers_not_matching(httpx_mock: HTTPXMock) -> None:
httpx_mock.add_response(
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url2",
- "host2": "test_url",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url2",
+ "Host2": "test_url",
}
)
@@ -1108,8 +1190,30 @@ async def test_headers_not_matching(httpx_mock: HTTPXMock) -> None:
await client.get("https://test_url")
assert (
str(exception_info.value)
- == f"""No response can be found for GET request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers amongst:
-Match all requests with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url2', 'host2': 'test_url'}} headers"""
+ == f"""No response can be found for GET request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers amongst:
+Match all requests with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2', 'Host2': 'test_url'}} headers"""
+ )
+
+ # Clean up responses to avoid assertion failure
+ httpx_mock.reset(assert_all_responses_were_requested=False)
+
+
+@pytest.mark.asyncio
+async def test_url_not_matching_upper_case_headers_matching(
+ httpx_mock: HTTPXMock,
+) -> None:
+ httpx_mock.add_response(
+ method="GET",
+ url="https://test_url?q=b",
+ match_headers={"MyHeader": "Something"},
+ )
+ async with httpx.AsyncClient() as client:
+ with pytest.raises(httpx.TimeoutException) as exception_info:
+ await client.get("https://test_url", headers={"MyHeader": "Something"})
+ assert (
+ str(exception_info.value)
+ == """No response can be found for GET request on https://test_url with {'MyHeader': 'Something'} headers amongst:
+Match GET requests on https://test_url?q=b with {'MyHeader': 'Something'} headers"""
)
# Clean up responses to avoid assertion failure
@@ -1142,10 +1246,82 @@ async def test_content_not_matching(httpx_mock: HTTPXMock) -> None:
httpx_mock.reset(assert_all_responses_were_requested=False)
+@pytest.mark.asyncio
+async def test_json_matching(httpx_mock: HTTPXMock) -> None:
+ httpx_mock.add_response(match_json={"a": 1, "b": 2})
+
+ async with httpx.AsyncClient() as client:
+ response = await client.post("https://test_url", json={"b": 2, "a": 1})
+ assert response.read() == b""
+
+
+@pytest.mark.asyncio
+async def test_json_partial_matching(httpx_mock: HTTPXMock) -> None:
+ httpx_mock.add_response(match_json={"a": 1, "b": ANY})
+
+ async with httpx.AsyncClient() as client:
+ response = await client.post("https://test_url", json={"b": 2, "a": 1})
+ assert response.read() == b""
+
+
+@pytest.mark.asyncio
+async def test_json_not_matching(httpx_mock: HTTPXMock) -> None:
+ httpx_mock.add_response(match_json={"a": 1, "b": 2})
+
+ async with httpx.AsyncClient() as client:
+ with pytest.raises(httpx.TimeoutException) as exception_info:
+ await client.post("https://test_url", json={"c": 3, "b": 2, "a": 1})
+ assert (
+ str(exception_info.value)
+ == """No response can be found for POST request on https://test_url with b'{"c": 3, "b": 2, "a": 1}' body amongst:
+Match all requests with {'a': 1, 'b': 2} json body"""
+ )
+
+ # Clean up responses to avoid assertion failure
+ httpx_mock.reset(assert_all_responses_were_requested=False)
+
+
+@pytest.mark.asyncio
+async def test_headers_and_json_not_matching(httpx_mock: HTTPXMock) -> None:
+ httpx_mock.add_response(
+ match_json={"a": 1, "b": 2},
+ match_headers={"foo": "bar"},
+ )
+
+ async with httpx.AsyncClient() as client:
+ with pytest.raises(httpx.TimeoutException) as exception_info:
+ await client.post("https://test_url", json={"c": 3, "b": 2, "a": 1})
+ assert (
+ str(exception_info.value)
+ == """No response can be found for POST request on https://test_url with {} headers and b'{"c": 3, "b": 2, "a": 1}' body amongst:
+Match all requests with {'foo': 'bar'} headers and {'a': 1, 'b': 2} json body"""
+ )
+
+ # Clean up responses to avoid assertion failure
+ httpx_mock.reset(assert_all_responses_were_requested=False)
+
+
+@pytest.mark.asyncio
+async def test_match_json_invalid_json(httpx_mock: HTTPXMock) -> None:
+ httpx_mock.add_response(match_json={"a": 1, "b": 2})
+
+ async with httpx.AsyncClient() as client:
+ with pytest.raises(httpx.TimeoutException) as exception_info:
+ await client.post("https://test_url", content=b"foobar")
+ assert (
+ str(exception_info.value)
+ == """No response can be found for POST request on https://test_url with b'foobar' body amongst:
+Match all requests with {'a': 1, 'b': 2} json body"""
+ )
+
+ # Clean up responses to avoid assertion failure
+ httpx_mock.reset(assert_all_responses_were_requested=False)
+
+
@pytest.mark.asyncio
async def test_headers_and_content_matching(httpx_mock: HTTPXMock) -> None:
httpx_mock.add_response(
- match_headers={"user-agent": f"python-httpx/{httpx.__version__}"},
+ match_headers={"User-Agent": f"python-httpx/{httpx.__version__}"},
match_content=b"This is the body",
)
@@ -1158,8 +1334,8 @@ async def test_headers_and_content_matching(httpx_mock: HTTPXMock) -> None:
async def test_headers_not_matching_and_content_matching(httpx_mock: HTTPXMock) -> None:
httpx_mock.add_response(
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url2",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url2",
},
match_content=b"This is the body",
)
@@ -1169,8 +1345,8 @@ async def test_headers_not_matching_and_content_matching(httpx_mock: HTTPXMock)
await client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match all requests with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url2'}} headers and b'This is the body' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match all requests with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body' body"""
)
# Clean up responses to avoid assertion failure
@@ -1181,8 +1357,8 @@ async def test_headers_not_matching_and_content_matching(httpx_mock: HTTPXMock)
async def test_headers_matching_and_content_not_matching(httpx_mock: HTTPXMock) -> None:
httpx_mock.add_response(
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url",
},
match_content=b"This is the body2",
)
@@ -1192,8 +1368,8 @@ async def test_headers_matching_and_content_not_matching(httpx_mock: HTTPXMock)
await client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match all requests with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url'}} headers and b'This is the body2' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match all requests with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url'}} headers and b'This is the body2' body"""
)
# Clean up responses to avoid assertion failure
@@ -1204,8 +1380,8 @@ async def test_headers_matching_and_content_not_matching(httpx_mock: HTTPXMock)
async def test_headers_and_content_not_matching(httpx_mock: HTTPXMock) -> None:
httpx_mock.add_response(
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url2",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url2",
},
match_content=b"This is the body2",
)
@@ -1215,8 +1391,8 @@ async def test_headers_and_content_not_matching(httpx_mock: HTTPXMock) -> None:
await client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match all requests with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url2'}} headers and b'This is the body2' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match all requests with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body"""
)
# Clean up responses to avoid assertion failure
@@ -1227,7 +1403,7 @@ async def test_headers_and_content_not_matching(httpx_mock: HTTPXMock) -> None:
async def test_url_and_headers_and_content_matching(httpx_mock: HTTPXMock) -> None:
httpx_mock.add_response(
url="https://test_url",
- match_headers={"user-agent": f"python-httpx/{httpx.__version__}"},
+ match_headers={"User-Agent": f"python-httpx/{httpx.__version__}"},
match_content=b"This is the body",
)
@@ -1243,8 +1419,8 @@ async def test_headers_not_matching_and_url_and_content_matching(
httpx_mock.add_response(
url="https://test_url",
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url2",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url2",
},
match_content=b"This is the body",
)
@@ -1254,8 +1430,8 @@ async def test_headers_not_matching_and_url_and_content_matching(
await client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match all requests on https://test_url with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url2'}} headers and b'This is the body' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match all requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body' body"""
)
# Clean up responses to avoid assertion failure
@@ -1269,8 +1445,8 @@ async def test_url_and_headers_not_matching_and_content_matching(
httpx_mock.add_response(
url="https://test_url2",
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url2",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url2",
},
match_content=b"This is the body",
)
@@ -1280,8 +1456,8 @@ async def test_url_and_headers_not_matching_and_content_matching(
await client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match all requests on https://test_url2 with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url2'}} headers and b'This is the body' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match all requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body' body"""
)
# Clean up responses to avoid assertion failure
@@ -1295,8 +1471,8 @@ async def test_url_and_headers_matching_and_content_not_matching(
httpx_mock.add_response(
url="https://test_url",
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url",
},
match_content=b"This is the body2",
)
@@ -1306,8 +1482,8 @@ async def test_url_and_headers_matching_and_content_not_matching(
await client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match all requests on https://test_url with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url'}} headers and b'This is the body2' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match all requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url'}} headers and b'This is the body2' body"""
)
# Clean up responses to avoid assertion failure
@@ -1321,8 +1497,8 @@ async def test_headers_matching_and_url_and_content_not_matching(
httpx_mock.add_response(
url="https://test_url2",
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url",
},
match_content=b"This is the body2",
)
@@ -1332,8 +1508,8 @@ async def test_headers_matching_and_url_and_content_not_matching(
await client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match all requests on https://test_url2 with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url'}} headers and b'This is the body2' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match all requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url'}} headers and b'This is the body2' body"""
)
# Clean up responses to avoid assertion failure
@@ -1347,8 +1523,8 @@ async def test_url_matching_and_headers_and_content_not_matching(
httpx_mock.add_response(
url="https://test_url",
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url2",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url2",
},
match_content=b"This is the body2",
)
@@ -1358,8 +1534,8 @@ async def test_url_matching_and_headers_and_content_not_matching(
await client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match all requests on https://test_url with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url2'}} headers and b'This is the body2' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match all requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body"""
)
# Clean up responses to avoid assertion failure
@@ -1371,8 +1547,8 @@ async def test_url_and_headers_and_content_not_matching(httpx_mock: HTTPXMock) -
httpx_mock.add_response(
url="https://test_url2",
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url2",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url2",
},
match_content=b"This is the body2",
)
@@ -1382,8 +1558,8 @@ async def test_url_and_headers_and_content_not_matching(httpx_mock: HTTPXMock) -
await client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match all requests on https://test_url2 with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url2'}} headers and b'This is the body2' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match all requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body"""
)
# Clean up responses to avoid assertion failure
@@ -1397,7 +1573,7 @@ async def test_method_and_url_and_headers_and_content_matching(
httpx_mock.add_response(
url="https://test_url",
method="POST",
- match_headers={"user-agent": f"python-httpx/{httpx.__version__}"},
+ match_headers={"User-Agent": f"python-httpx/{httpx.__version__}"},
match_content=b"This is the body",
)
@@ -1414,8 +1590,8 @@ async def test_headers_not_matching_and_method_and_url_and_content_matching(
url="https://test_url",
method="POST",
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url2",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url2",
},
match_content=b"This is the body",
)
@@ -1425,8 +1601,8 @@ async def test_headers_not_matching_and_method_and_url_and_content_matching(
await client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match POST requests on https://test_url with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url2'}} headers and b'This is the body' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match POST requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body' body"""
)
# Clean up responses to avoid assertion failure
@@ -1441,8 +1617,8 @@ async def test_url_and_headers_not_matching_and_method_and_content_matching(
url="https://test_url2",
method="POST",
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url2",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url2",
},
match_content=b"This is the body",
)
@@ -1452,8 +1628,8 @@ async def test_url_and_headers_not_matching_and_method_and_content_matching(
await client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match POST requests on https://test_url2 with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url2'}} headers and b'This is the body' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match POST requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body' body"""
)
# Clean up responses to avoid assertion failure
@@ -1468,8 +1644,8 @@ async def test_method_and_url_and_headers_matching_and_content_not_matching(
url="https://test_url",
method="POST",
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url",
},
match_content=b"This is the body2",
)
@@ -1479,8 +1655,8 @@ async def test_method_and_url_and_headers_matching_and_content_not_matching(
await client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match POST requests on https://test_url with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url'}} headers and b'This is the body2' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match POST requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url'}} headers and b'This is the body2' body"""
)
# Clean up responses to avoid assertion failure
@@ -1495,8 +1671,8 @@ async def test_method_and_headers_matching_and_url_and_content_not_matching(
url="https://test_url2",
method="POST",
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url",
},
match_content=b"This is the body2",
)
@@ -1506,8 +1682,8 @@ async def test_method_and_headers_matching_and_url_and_content_not_matching(
await client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match POST requests on https://test_url2 with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url'}} headers and b'This is the body2' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match POST requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url'}} headers and b'This is the body2' body"""
)
# Clean up responses to avoid assertion failure
@@ -1522,8 +1698,8 @@ async def test_method_and_url_matching_and_headers_and_content_not_matching(
url="https://test_url",
method="POST",
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url2",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url2",
},
match_content=b"This is the body2",
)
@@ -1533,8 +1709,8 @@ async def test_method_and_url_matching_and_headers_and_content_not_matching(
await client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match POST requests on https://test_url with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url2'}} headers and b'This is the body2' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match POST requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body"""
)
# Clean up responses to avoid assertion failure
@@ -1549,8 +1725,8 @@ async def test_method_matching_and_url_and_headers_and_content_not_matching(
url="https://test_url2",
method="POST",
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url2",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url2",
},
match_content=b"This is the body2",
)
@@ -1560,8 +1736,8 @@ async def test_method_matching_and_url_and_headers_and_content_not_matching(
await client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match POST requests on https://test_url2 with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url2'}} headers and b'This is the body2' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match POST requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body"""
)
# Clean up responses to avoid assertion failure
@@ -1576,8 +1752,8 @@ async def test_method_and_url_and_headers_and_content_not_matching(
url="https://test_url2",
method="PUT",
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url2",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url2",
},
match_content=b"This is the body2",
)
@@ -1587,8 +1763,8 @@ async def test_method_and_url_and_headers_and_content_not_matching(
await client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match PUT requests on https://test_url2 with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url2'}} headers and b'This is the body2' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match PUT requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body"""
)
# Clean up responses to avoid assertion failure
diff --git a/tests/test_httpx_sync.py b/tests/test_httpx_sync.py
index 233b206..5e63bbc 100644
--- a/tests/test_httpx_sync.py
+++ b/tests/test_httpx_sync.py
@@ -1,4 +1,6 @@
import re
+from typing import Any
+from unittest.mock import ANY
import httpx
import pytest
@@ -139,6 +141,25 @@ def test_response_with_html_string_body(httpx_mock: HTTPXMock) -> None:
assert response.text == "test content"
+def test_url_not_matching_upper_case_headers_matching(httpx_mock: HTTPXMock) -> None:
+ httpx_mock.add_response(
+ method="GET",
+ url="https://test_url?q=b",
+ match_headers={"MyHeader": "Something"},
+ )
+ with httpx.Client() as client:
+ with pytest.raises(httpx.TimeoutException) as exception_info:
+ client.get("https://test_url", headers={"MyHeader": "Something"})
+ assert (
+ str(exception_info.value)
+ == """No response can be found for GET request on https://test_url with {'MyHeader': 'Something'} headers amongst:
+Match GET requests on https://test_url?q=b with {'MyHeader': 'Something'} headers"""
+ )
+
+ # Clean up responses to avoid assertion failure
+ httpx_mock.reset(assert_all_responses_were_requested=False)
+
+
def test_stream_response_streaming(httpx_mock: HTTPXMock) -> None:
httpx_mock.add_response(
url="https://test_url",
@@ -841,7 +862,7 @@ def test_request_retrieval_with_more_than_one(httpx_mock):
def test_headers_matching(httpx_mock: HTTPXMock) -> None:
httpx_mock.add_response(
- match_headers={"user-agent": f"python-httpx/{httpx.__version__}"}
+ match_headers={"User-Agent": f"python-httpx/{httpx.__version__}"}
)
with httpx.Client() as client:
@@ -849,12 +870,89 @@ def test_headers_matching(httpx_mock: HTTPXMock) -> None:
assert response.content == b""
+def test_multi_value_headers_matching(httpx_mock: HTTPXMock) -> None:
+ httpx_mock.add_response(match_headers={"my-custom-header": "value1, value2"})
+
+ with httpx.Client() as client:
+ response = client.get(
+ "https://test_url",
+ headers=[("my-custom-header", "value1"), ("my-custom-header", "value2")],
+ )
+ assert response.content == b""
+
+
+def test_multi_value_headers_not_matching_single_value_issued(
+ httpx_mock: HTTPXMock,
+) -> None:
+ httpx_mock.add_response(match_headers={"my-custom-header": "value1"})
+
+ with httpx.Client() as client:
+ with pytest.raises(httpx.TimeoutException) as exception_info:
+ client.get(
+ "https://test_url",
+ headers=[
+ ("my-custom-header", "value1"),
+ ("my-custom-header", "value2"),
+ ],
+ )
+ assert (
+ str(exception_info.value)
+ == """No response can be found for GET request on https://test_url with {'my-custom-header': 'value1, value2'} headers amongst:
+Match all requests with {'my-custom-header': 'value1'} headers"""
+ )
+
+ # Clean up responses to avoid assertion failure
+ httpx_mock.reset(assert_all_responses_were_requested=False)
+
+
+def test_multi_value_headers_not_matching_multi_value_issued(
+ httpx_mock: HTTPXMock,
+) -> None:
+ httpx_mock.add_response(match_headers={"my-custom-header": "value1, value2"})
+
+ with httpx.Client() as client:
+ with pytest.raises(httpx.TimeoutException) as exception_info:
+ client.get(
+ "https://test_url",
+ headers=[
+ ("my-custom-header", "value1"),
+ ("my-custom-header", "value3"),
+ ],
+ )
+ assert (
+ str(exception_info.value)
+ == """No response can be found for GET request on https://test_url with {'my-custom-header': 'value1, value3'} headers amongst:
+Match all requests with {'my-custom-header': 'value1, value2'} headers"""
+ )
+
+ # Clean up responses to avoid assertion failure
+ httpx_mock.reset(assert_all_responses_were_requested=False)
+
+
+def test_headers_matching_respect_case(httpx_mock: HTTPXMock) -> None:
+ httpx_mock.add_response(
+ match_headers={"user-agent": f"python-httpx/{httpx.__version__}"}
+ )
+
+ with httpx.Client() as client:
+ with pytest.raises(httpx.TimeoutException) as exception_info:
+ client.get("https://test_url")
+ assert (
+ str(exception_info.value)
+ == f"""No response can be found for GET request on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}'}} headers amongst:
+Match all requests with {{'user-agent': 'python-httpx/{httpx.__version__}'}} headers"""
+ )
+
+ # Clean up responses to avoid assertion failure
+ httpx_mock.reset(assert_all_responses_were_requested=False)
+
+
def test_headers_not_matching(httpx_mock: HTTPXMock) -> None:
httpx_mock.add_response(
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url2",
- "host2": "test_url",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url2",
+ "Host2": "test_url",
}
)
@@ -863,8 +961,8 @@ def test_headers_not_matching(httpx_mock: HTTPXMock) -> None:
client.get("https://test_url")
assert (
str(exception_info.value)
- == f"""No response can be found for GET request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers amongst:
-Match all requests with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url2', 'host2': 'test_url'}} headers"""
+ == f"""No response can be found for GET request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers amongst:
+Match all requests with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2', 'Host2': 'test_url'}} headers"""
)
# Clean up responses to avoid assertion failure
@@ -895,9 +993,86 @@ def test_content_not_matching(httpx_mock: HTTPXMock) -> None:
httpx_mock.reset(assert_all_responses_were_requested=False)
+def test_match_json_and_match_content_error(httpx_mock: HTTPXMock) -> None:
+ with pytest.raises(ValueError) as exception_info:
+ httpx_mock.add_response(match_json={"a": 1}, match_content=b"")
+
+ assert (
+ str(exception_info.value)
+ == "Only one way of matching against the body can be provided. If you want to match against the JSON decoded representation, use match_json. Otherwise, use match_content."
+ )
+
+
+def test_json_matching(httpx_mock: HTTPXMock) -> None:
+ httpx_mock.add_response(match_json={"a": 1, "b": 2})
+
+ with httpx.Client() as client:
+ response = client.post("https://test_url", json={"b": 2, "a": 1})
+ assert response.read() == b""
+
+
+def test_json_partial_matching(httpx_mock: HTTPXMock) -> None:
+ httpx_mock.add_response(match_json={"a": 1, "b": ANY})
+
+ with httpx.Client() as client:
+ response = client.post("https://test_url", json={"b": 2, "a": 1})
+ assert response.read() == b""
+
+
+def test_json_not_matching(httpx_mock: HTTPXMock) -> None:
+ httpx_mock.add_response(match_json={"a": 1, "b": 2})
+
+ with httpx.Client() as client:
+ with pytest.raises(httpx.TimeoutException) as exception_info:
+ client.post("https://test_url", json={"c": 3, "b": 2, "a": 1})
+ assert (
+ str(exception_info.value)
+ == """No response can be found for POST request on https://test_url with b'{"c": 3, "b": 2, "a": 1}' body amongst:
+Match all requests with {'a': 1, 'b': 2} json body"""
+ )
+
+ # Clean up responses to avoid assertion failure
+ httpx_mock.reset(assert_all_responses_were_requested=False)
+
+
+def test_headers_and_json_not_matching(httpx_mock: HTTPXMock) -> None:
+ httpx_mock.add_response(
+ match_json={"a": 1, "b": 2},
+ match_headers={"foo": "bar"},
+ )
+
+ with httpx.Client() as client:
+ with pytest.raises(httpx.TimeoutException) as exception_info:
+ client.post("https://test_url", json={"c": 3, "b": 2, "a": 1})
+ assert (
+ str(exception_info.value)
+ == """No response can be found for POST request on https://test_url with {} headers and b'{"c": 3, "b": 2, "a": 1}' body amongst:
+Match all requests with {'foo': 'bar'} headers and {'a': 1, 'b': 2} json body"""
+ )
+
+ # Clean up responses to avoid assertion failure
+ httpx_mock.reset(assert_all_responses_were_requested=False)
+
+
+def test_match_json_invalid_json(httpx_mock: HTTPXMock) -> None:
+ httpx_mock.add_response(match_json={"a": 1, "b": 2})
+
+ with httpx.Client() as client:
+ with pytest.raises(httpx.TimeoutException) as exception_info:
+ client.post("https://test_url", content=b"foobar")
+ assert (
+ str(exception_info.value)
+ == """No response can be found for POST request on https://test_url with b'foobar' body amongst:
+Match all requests with {'a': 1, 'b': 2} json body"""
+ )
+
+ # Clean up responses to avoid assertion failure
+ httpx_mock.reset(assert_all_responses_were_requested=False)
+
+
def test_headers_and_content_matching(httpx_mock: HTTPXMock) -> None:
httpx_mock.add_response(
- match_headers={"user-agent": f"python-httpx/{httpx.__version__}"},
+ match_headers={"User-Agent": f"python-httpx/{httpx.__version__}"},
match_content=b"This is the body",
)
@@ -909,8 +1084,8 @@ def test_headers_and_content_matching(httpx_mock: HTTPXMock) -> None:
def test_headers_not_matching_and_content_matching(httpx_mock: HTTPXMock) -> None:
httpx_mock.add_response(
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url2",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url2",
},
match_content=b"This is the body",
)
@@ -920,8 +1095,8 @@ def test_headers_not_matching_and_content_matching(httpx_mock: HTTPXMock) -> Non
client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match all requests with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url2'}} headers and b'This is the body' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match all requests with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body' body"""
)
# Clean up responses to avoid assertion failure
@@ -931,8 +1106,8 @@ def test_headers_not_matching_and_content_matching(httpx_mock: HTTPXMock) -> Non
def test_headers_matching_and_content_not_matching(httpx_mock: HTTPXMock) -> None:
httpx_mock.add_response(
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url",
},
match_content=b"This is the body2",
)
@@ -942,8 +1117,8 @@ def test_headers_matching_and_content_not_matching(httpx_mock: HTTPXMock) -> Non
client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match all requests with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url'}} headers and b'This is the body2' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match all requests with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url'}} headers and b'This is the body2' body"""
)
# Clean up responses to avoid assertion failure
@@ -953,8 +1128,8 @@ def test_headers_matching_and_content_not_matching(httpx_mock: HTTPXMock) -> Non
def test_headers_and_content_not_matching(httpx_mock: HTTPXMock) -> None:
httpx_mock.add_response(
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url2",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url2",
},
match_content=b"This is the body2",
)
@@ -964,8 +1139,8 @@ def test_headers_and_content_not_matching(httpx_mock: HTTPXMock) -> None:
client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match all requests with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url2'}} headers and b'This is the body2' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match all requests with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body"""
)
# Clean up responses to avoid assertion failure
@@ -975,7 +1150,7 @@ def test_headers_and_content_not_matching(httpx_mock: HTTPXMock) -> None:
def test_url_and_headers_and_content_matching(httpx_mock: HTTPXMock) -> None:
httpx_mock.add_response(
url="https://test_url",
- match_headers={"user-agent": f"python-httpx/{httpx.__version__}"},
+ match_headers={"User-Agent": f"python-httpx/{httpx.__version__}"},
match_content=b"This is the body",
)
@@ -990,8 +1165,8 @@ def test_headers_not_matching_and_url_and_content_matching(
httpx_mock.add_response(
url="https://test_url",
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url2",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url2",
},
match_content=b"This is the body",
)
@@ -1001,8 +1176,8 @@ def test_headers_not_matching_and_url_and_content_matching(
client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match all requests on https://test_url with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url2'}} headers and b'This is the body' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match all requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body' body"""
)
# Clean up responses to avoid assertion failure
@@ -1015,8 +1190,8 @@ def test_url_and_headers_not_matching_and_content_matching(
httpx_mock.add_response(
url="https://test_url2",
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url2",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url2",
},
match_content=b"This is the body",
)
@@ -1026,8 +1201,8 @@ def test_url_and_headers_not_matching_and_content_matching(
client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match all requests on https://test_url2 with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url2'}} headers and b'This is the body' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match all requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body' body"""
)
# Clean up responses to avoid assertion failure
@@ -1040,8 +1215,8 @@ def test_url_and_headers_matching_and_content_not_matching(
httpx_mock.add_response(
url="https://test_url",
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url",
},
match_content=b"This is the body2",
)
@@ -1051,8 +1226,8 @@ def test_url_and_headers_matching_and_content_not_matching(
client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match all requests on https://test_url with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url'}} headers and b'This is the body2' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match all requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url'}} headers and b'This is the body2' body"""
)
# Clean up responses to avoid assertion failure
@@ -1065,8 +1240,8 @@ def test_headers_matching_and_url_and_content_not_matching(
httpx_mock.add_response(
url="https://test_url2",
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url",
},
match_content=b"This is the body2",
)
@@ -1076,8 +1251,8 @@ def test_headers_matching_and_url_and_content_not_matching(
client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match all requests on https://test_url2 with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url'}} headers and b'This is the body2' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match all requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url'}} headers and b'This is the body2' body"""
)
# Clean up responses to avoid assertion failure
@@ -1090,8 +1265,8 @@ def test_url_matching_and_headers_and_content_not_matching(
httpx_mock.add_response(
url="https://test_url",
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url2",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url2",
},
match_content=b"This is the body2",
)
@@ -1101,8 +1276,8 @@ def test_url_matching_and_headers_and_content_not_matching(
client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match all requests on https://test_url with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url2'}} headers and b'This is the body2' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match all requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body"""
)
# Clean up responses to avoid assertion failure
@@ -1113,8 +1288,8 @@ def test_url_and_headers_and_content_not_matching(httpx_mock: HTTPXMock) -> None
httpx_mock.add_response(
url="https://test_url2",
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url2",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url2",
},
match_content=b"This is the body2",
)
@@ -1124,8 +1299,8 @@ def test_url_and_headers_and_content_not_matching(httpx_mock: HTTPXMock) -> None
client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match all requests on https://test_url2 with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url2'}} headers and b'This is the body2' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match all requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body"""
)
# Clean up responses to avoid assertion failure
@@ -1136,7 +1311,7 @@ def test_method_and_url_and_headers_and_content_matching(httpx_mock: HTTPXMock)
httpx_mock.add_response(
url="https://test_url",
method="POST",
- match_headers={"user-agent": f"python-httpx/{httpx.__version__}"},
+ match_headers={"User-Agent": f"python-httpx/{httpx.__version__}"},
match_content=b"This is the body",
)
@@ -1152,8 +1327,8 @@ def test_headers_not_matching_and_method_and_url_and_content_matching(
url="https://test_url",
method="POST",
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url2",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url2",
},
match_content=b"This is the body",
)
@@ -1163,8 +1338,8 @@ def test_headers_not_matching_and_method_and_url_and_content_matching(
client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match POST requests on https://test_url with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url2'}} headers and b'This is the body' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match POST requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body' body"""
)
# Clean up responses to avoid assertion failure
@@ -1178,8 +1353,8 @@ def test_url_and_headers_not_matching_and_method_and_content_matching(
url="https://test_url2",
method="POST",
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url2",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url2",
},
match_content=b"This is the body",
)
@@ -1189,8 +1364,8 @@ def test_url_and_headers_not_matching_and_method_and_content_matching(
client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match POST requests on https://test_url2 with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url2'}} headers and b'This is the body' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match POST requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body' body"""
)
# Clean up responses to avoid assertion failure
@@ -1204,8 +1379,8 @@ def test_method_and_url_and_headers_matching_and_content_not_matching(
url="https://test_url",
method="POST",
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url",
},
match_content=b"This is the body2",
)
@@ -1215,8 +1390,8 @@ def test_method_and_url_and_headers_matching_and_content_not_matching(
client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match POST requests on https://test_url with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url'}} headers and b'This is the body2' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match POST requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url'}} headers and b'This is the body2' body"""
)
# Clean up responses to avoid assertion failure
@@ -1230,8 +1405,8 @@ def test_method_and_headers_matching_and_url_and_content_not_matching(
url="https://test_url2",
method="POST",
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url",
},
match_content=b"This is the body2",
)
@@ -1241,8 +1416,8 @@ def test_method_and_headers_matching_and_url_and_content_not_matching(
client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match POST requests on https://test_url2 with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url'}} headers and b'This is the body2' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match POST requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url'}} headers and b'This is the body2' body"""
)
# Clean up responses to avoid assertion failure
@@ -1256,8 +1431,8 @@ def test_method_and_url_matching_and_headers_and_content_not_matching(
url="https://test_url",
method="POST",
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url2",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url2",
},
match_content=b"This is the body2",
)
@@ -1267,8 +1442,8 @@ def test_method_and_url_matching_and_headers_and_content_not_matching(
client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match POST requests on https://test_url with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url2'}} headers and b'This is the body2' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match POST requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body"""
)
# Clean up responses to avoid assertion failure
@@ -1282,8 +1457,8 @@ def test_method_matching_and_url_and_headers_and_content_not_matching(
url="https://test_url2",
method="POST",
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url2",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url2",
},
match_content=b"This is the body2",
)
@@ -1293,8 +1468,8 @@ def test_method_matching_and_url_and_headers_and_content_not_matching(
client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match POST requests on https://test_url2 with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url2'}} headers and b'This is the body2' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match POST requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body"""
)
# Clean up responses to avoid assertion failure
@@ -1308,8 +1483,8 @@ def test_method_and_url_and_headers_and_content_not_matching(
url="https://test_url2",
method="PUT",
match_headers={
- "user-agent": f"python-httpx/{httpx.__version__}",
- "host": "test_url2",
+ "User-Agent": f"python-httpx/{httpx.__version__}",
+ "Host": "test_url2",
},
match_content=b"This is the body2",
)
@@ -1319,8 +1494,8 @@ def test_method_and_url_and_headers_and_content_not_matching(
client.post("https://test_url", content=b"This is the body")
assert (
str(exception_info.value)
- == f"""No response can be found for POST request on https://test_url with {{'host': 'test_url', 'user-agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
-Match PUT requests on https://test_url2 with {{'user-agent': 'python-httpx/{httpx.__version__}', 'host': 'test_url2'}} headers and b'This is the body2' body"""
+ == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst:
+Match PUT requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body"""
)
# Clean up responses to avoid assertion failure