summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Falcão <gabriel@nacaolivre.org>2021-05-14 01:11:43 +0200
committerGabriel Falcão <gabriel@nacaolivre.org>2021-05-14 01:13:21 +0200
commit2f9a12b6bf305fb7704d2664d606fc45b6754d79 (patch)
tree474a2f859374f9427f39b834ae18ac201c22c10c
parent519e6d332448c8f97f341944956fe50d5630c2a1 (diff)
downloadhttpretty-fix/414/httpx.tar.gz
implement support to httpx libraryfix/414/httpx
closes #414
-rw-r--r--httpretty/core.py25
-rw-r--r--tests/functional/bugfixes/test_414_httpx.py12
-rw-r--r--tests/functional/test_debug.py10
-rw-r--r--tests/functional/test_httpx.py15
4 files changed, 31 insertions, 31 deletions
diff --git a/httpretty/core.py b/httpretty/core.py
index f381eaf..d1ccbf9 100644
--- a/httpretty/core.py
+++ b/httpretty/core.py
@@ -409,6 +409,8 @@ class fakesock(object):
"""drop-in replacement for :py:class:`socket.socket`
"""
_entry = None
+ _read_buf = None
+
debuglevel = 0
_sent_data = []
is_secure = False
@@ -708,7 +710,9 @@ class fakesock(object):
matcher, entries = httpretty.match_uriinfo(info)
if not entries:
+ logger.debug('no entries matching {}'.format(request))
self._entry = None
+ self._read_buf = None
self.real_sendall(data, request=request)
return
@@ -727,8 +731,9 @@ class fakesock(object):
if self.truesock:
self.truesock.settimeout(new_timeout)
- def send(self, *args, **kwargs):
- return self.forward_and_trace('send', *args, **kwargs)
+ def send(self, data, *args, **kwargs):
+ self.sendall(data, *args, **kwargs)
+ return len(data)
def sendto(self, *args, **kwargs):
return self.forward_and_trace('sendto', *args, **kwargs)
@@ -742,12 +747,20 @@ class fakesock(object):
def recvfrom(self, *args, **kwargs):
return self.forward_and_trace('recvfrom', *args, **kwargs)
- def recv(self, buffersize=None, *args, **kwargs):
- buffersize = buffersize or self._bufsize
- return self.forward_and_trace('recv', buffersize, *args, **kwargs)
+ def recv(self, buffersize=0, *args, **kwargs):
+ if not self._read_buf:
+ self._read_buf = io.BytesIO()
+
+ if self._entry:
+ self._entry.fill_filekind(self._read_buf)
+
+ if not self._read_buf:
+ raise UnmockedError('socket cannot recv(): {!r}'.format(self))
+
+ return self._read_buf.read(buffersize)
def __getattr__(self, name):
- if name in ('getsockopt', ) and not self.truesock:
+ if name in ('getsockopt', 'selected_alpn_protocol') and not self.truesock:
self.truesock = self.create_socket()
elif httpretty.allow_net_connect and not self.truesock:
# can't call self.connect_truesock() here because we
diff --git a/tests/functional/bugfixes/test_414_httpx.py b/tests/functional/bugfixes/test_414_httpx.py
new file mode 100644
index 0000000..6360ddc
--- /dev/null
+++ b/tests/functional/bugfixes/test_414_httpx.py
@@ -0,0 +1,12 @@
+import httpretty
+import httpx
+from sure import expect
+
+@httpretty.activate(verbose=True, allow_net_connect=False)
+def test_httpx():
+ httpretty.register_uri(httpretty.GET, "https://blog.falcao.it/",
+ body="Posts")
+
+ response = httpx.get('https://blog.falcao.it')
+
+ expect(response.text).to.equal("Posts")
diff --git a/tests/functional/test_debug.py b/tests/functional/test_debug.py
index 86de965..ff00840 100644
--- a/tests/functional/test_debug.py
+++ b/tests/functional/test_debug.py
@@ -58,16 +58,6 @@ def test_httpretty_debugs_socket_sendto(context):
)
-@httprettified
-@scenario(create_socket)
-def test_httpretty_debugs_socket_recv(context):
- "HTTPretty should forward_and_trace socket.recv"
-
- expect(context.sock.recv).when.called.to.throw(
- "not connected"
- )
-
-
@skip('not currently supported')
@httprettified
@scenario(create_socket)
diff --git a/tests/functional/test_httpx.py b/tests/functional/test_httpx.py
deleted file mode 100644
index 4cb51c4..0000000
--- a/tests/functional/test_httpx.py
+++ /dev/null
@@ -1,15 +0,0 @@
-import httpretty
-import httpx
-
-
-def test_one():
- httpretty.enable() # enable HTTPretty so that it will monkey patch the socket module
- httpretty.register_uri(httpretty.GET, "http://yipit.com/",
- body="Find the best daily deals")
-
- response = httpx.get('http://yipit.com')
-
- assert response.text == "Find the best daily deals"
-
- httpretty.disable() # disable afterwards, so that you will have no problems in code that uses that socket module
- httpretty.reset() # reset HTTPretty state (clean up registered urls and request history)