gabrielfalcao / HTTPretty

Intercept HTTP requests at the Python socket level. Fakes the whole socket module

Home Page:https://httpretty.readthedocs.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Regular expression for URL -> TypeError: wrap_socket() missing 1 required positional argument: 'sock'

akkana opened this issue · comments

This might be a documentation issue, maybe I'm just not understanding the documentation. But the doc says that in register_uri(), uri can be a regular expression.

So I have a simple test that calls req = requests.get("https://example.com/file-one.html"), and before calling it, I call register_uri with the same url,
httpretty.register_uri(httpretty.GET, "https://example.com/file-one.html", body=mock_body)
and that works. But if I change the line to
httpretty.register_uri(httpretty.GET, re.compile("/file-one"), body=mock_body)
or any other pattern that does match the full url, I get a TypeError:

Traceback (most recent call last):
  File "./httpretty_test.py", line 46, in <module>
    test_something()
  File "/usr/lib/python3/dist-packages/httpretty/core.py", line 1638, in wrapper
    return test(*args, **kw)
  File "./httpretty_test.py", line 42, in test_something
    some_function()
  File "./httpretty_test.py", line 17, in some_function
    req = requests.get(patmatchurl)
  File "/usr/lib/python3/dist-packages/requests/api.py", line 76, in get
    return request('get', url, params=params, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 530, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 643, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/adapters.py", line 439, in send
    resp = conn.urlopen(
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 670, in urlopen
    httplib_response = self._make_request(
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 381, in _make_request
    self._validate_conn(conn)
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 978, in _validate_conn
    conn.connect()
  File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 361, in connect
    self.sock = ssl_wrap_socket(
  File "/usr/lib/python3/dist-packages/httpretty/core.py", line 600, in fake_wrap_socket
    return orig_wrap_socket_fn(*args, **kw)
  File "/usr/lib/python3/dist-packages/urllib3/util/ssl_.py", line 377, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "/usr/lib/python3/dist-packages/httpretty/core.py", line 600, in fake_wrap_socket
    return orig_wrap_socket_fn(*args, **kw)
TypeError: wrap_socket() missing 1 required positional argument: 'sock'

What am I not understanding about how to use a regular expression uri?

Apparently I can't attach a Python file, so here's a standalone test program:

import requests
import httpretty
import re


patmatchurl = "https://example.com/file-one.html"

# Neither of these patterns work even though they both match patmatchurl:
patmatchpat = re.compile("/file-one")
# patmatchpat = re.compile(".*/file-one.*")

# External function that would ordinarily be in the module being tested.
def some_function():
    print("Trying a URL that matches the regexp")
    req = requests.get(patmatchurl)
    print("status code is", req.status_code)
    print("text is:", req.text)


# Fabricate a mock response.
def mock_body(request, url, response_headers):
    print("called mock_body for:", url)
    return [200, response_headers, "Mocked " + url]


# The unit test function.
@httpretty.activate
def test_something():
    # This, using the full patmatchurl, works:
    # httpretty.register_uri(httpretty.GET, patmatchurl, body=mock_body)

    # Supposedly regular expressions also work, but they don't seem to.
    # If you comment the previous line out and uncomment this one,
    # this regexp that matches patmatchurl doesn't call mock_body
    # but instead gives a long traceback ending in
    # TypeError: wrap_socket() missing 1 required positional argument: 'sock'
    httpretty.register_uri(httpretty.GET, patmatchpat, body=mock_body)

    print("Set up httpretty; calling the test function")
    some_function()


if __name__ == '__main__':
    test_something()

@akkana I believe this bug has been fixed in the release 1.1.0 but since you took time to write such neat test I'll create a PR based on your code to ensure the problem is really fixed.