nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: lundberg/respx
Pull request #26 (2019-11-21 10:51:52)

Support modifying response headers in custom matcher


Patch:
diff --git a/respx/models.py b/respx/models.py
index c769e31..faaf64c 100644
--- a/respx/models.py
+++ b/respx/models.py
@@ -8,7 +8,9 @@ from httpx.models import URL, AsyncRequest, Headers, HeaderTypes
 
 Regex = type(re.compile(""))
 Kwargs = typing.Dict[str, typing.Any]
-ContentDataTypes = typing.Union[bytes, str, typing.List, typing.Dict, typing.Callable]
+ContentDataTypes = typing.Union[
+    bytes, str, typing.List, typing.Dict, typing.Callable, Exception,
+]
 
 istype = lambda t, o: isinstance(o, t)
 isregex = partial(istype, Regex)
@@ -30,10 +32,9 @@ class ResponseTemplate:
 
     @property
     def headers(self) -> Headers:
-        headers = Headers({"Content-Type": "text/plain"})
-        if self._headers:
-            headers.update(self._headers)
-        return headers
+        if "Content-Type" not in self._headers:
+            self._headers["Content-Type"] = "text/plain"
+        return self._headers
 
     @property
     def content(self) -> bytes:


Test patch:
diff --git a/tests/test_mock.py b/tests/test_mock.py
index ba3b145..6b31cf8 100644
--- a/tests/test_mock.py
+++ b/tests/test_mock.py
@@ -307,6 +307,7 @@ class HTTPXMockTestCase(asynctest.TestCase):
     def test_custom_matcher(self):
         def matcher(request, response):
             if request.url.host == "foo":
+                response.headers["X-Foo"] = "Bar"
                 response.content = lambda request, id: f"foobar #{id}"
                 response.context["id"] = 123
                 return response
@@ -320,7 +321,9 @@ class HTTPXMockTestCase(asynctest.TestCase):
             self.assertEqual(response.status_code, 202)
             self.assertEqual(
                 response.headers,
-                httpx.Headers({"Content-Type": "text/plain", "X-Ham": "Spam"}),
+                httpx.Headers(
+                    {"Content-Type": "text/plain", "X-Ham": "Spam", "X-Foo": "Bar"}
+                ),
             )
             self.assertEqual(response.text, "foobar #123")
             self.assertTrue(request.called)
Repository: lundberg/respx
Pull request #58 (2020-06-01 10:11:11)

Patch proxy transports


Patch:
diff --git a/respx/mocks.py b/respx/mocks.py
index 2cf7dfd..8a5ca56 100644
--- a/respx/mocks.py
+++ b/respx/mocks.py
@@ -16,8 +16,10 @@ class MockTransport(BaseMockTransport):
     targets = [
         "httpcore._sync.connection.SyncHTTPConnection.request",
         "httpcore._sync.connection_pool.SyncConnectionPool.request",
+        "httpcore._sync.http_proxy.SyncHTTPProxy.request",
         "httpcore._async.connection.AsyncHTTPConnection.request",
         "httpcore._async.connection_pool.AsyncConnectionPool.request",
+        "httpcore._async.http_proxy.AsyncHTTPProxy.request",
         "httpx._transports.asgi.ASGITransport.request",
         "httpx._transports.wsgi.WSGITransport.request",
         "httpx._transports.urllib3.URLLib3Transport.request",


Test patch:
diff --git a/tests/test_mock.py b/tests/test_mock.py
index a0ac6e8..30a08f2 100644
--- a/tests/test_mock.py
+++ b/tests/test_mock.py
@@ -354,6 +354,21 @@ async def test_asgi():
             assert response.json() == {"status": "ok"}
 
 
+@pytest.mark.asyncio
+async def test_proxies():
+    with respx.mock:
+        respx.get("https://foo.bar/", content={"foo": "bar"})
+        with httpx.Client(proxies={"https": "https://1.1.1.1:1"}) as client:
+            response = client.get("https://foo.bar/")
+        assert response.json() == {"foo": "bar"}
+
+    async with respx.mock:
+        respx.get("https://foo.bar/", content={"foo": "bar"})
+        async with httpx.AsyncClient(proxies={"https": "https://1.1.1.1:1"}) as client:
+            response = await client.get("https://foo.bar/")
+        assert response.json() == {"foo": "bar"}
+
+
 @pytest.mark.filterwarnings("ignore::DeprecationWarning")
 def test_deprecated():
     url = "https://foo.bar/"
Repository: lundberg/respx
Pull request #192 (2021-12-16 14:41:16)

Allow first path segments containing colons
Using `urljoin` for merging path segments where the first segment contains a colon will cause the base to be ignored.
```python
>>> urljoin("/", "foo:bar")
'foo:bar'
```

See [RFC3986](https://www.rfc-editor.org/rfc/rfc3986#section-4.2)
> A path segment that contains a colon character (e.g., "this:that") cannot be used as the first segment of a relative-path reference, as it would be mistaken for a scheme name. Such a segment must be preceded by a dot-segment (e.g., "./this:that") to make a relative-path reference.

The easiest way is probably to not use `urljoin` and simply add a leading `/` when needed.




Patch:
diff --git a/respx/patterns.py b/respx/patterns.py
index 96be89b..5e9436c 100644
--- a/respx/patterns.py
+++ b/respx/patterns.py
@@ -388,7 +388,8 @@ class Path(Pattern):
         return request.url.path
 
     def strip_base(self, value: str) -> str:
-        value = urljoin("/", value[len(self.base.value) :])
+        value = value[len(self.base.value) :]
+        value = "/" + value if not value.startswith("/") else value
         return value
 
 
diff --git a/respx/router.py b/respx/router.py
index 7b9bcfc..d5e3989 100644
--- a/respx/router.py
+++ b/respx/router.py
@@ -296,7 +296,7 @@ class Router:
                 # Await async side effect and wrap any exception
                 if inspect.isawaitable(prospect):
                     try:
-                        prospect = await prospect  # type: ignore
+                        prospect = await prospect
                     except Exception as error:
                         raise SideEffectError(route, origin=error) from error
 


Test patch:
diff --git a/tests/test_router.py b/tests/test_router.py
index adec9ff..48e03a6 100644
--- a/tests/test_router.py
+++ b/tests/test_router.py
@@ -95,6 +95,7 @@ def test_pass_through():
         ("http://foo.bar/api/baz/", {"url": "/baz/"}, False),
         ("https://ham.spam/api/baz/", {"url": "/baz/"}, False),
         ("https://foo.bar/baz/", {"url": "/baz/"}, False),
+        ("https://foo.bar/api/hej:svejs", {"url": "/hej:svejs"}, True),
     ],
 )
 def test_base_url(url, lookups, expected):