Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

httpretty.errors.UnmockedError: Failed to socket.setblocking because because a real socket does not exist #5016

Closed
AltafHussain4748 opened this issue Apr 9, 2022 · 4 comments
Labels
debugging Working with user to figure out if there is an issue

Comments

@AltafHussain4748
Copy link

I am using moto a python library to mock aws services. On my local everything works fine but on teamcity I get the error

httpretty.errors.UnmockedError: Failed to socket.setblocking because because a real socket does not exist

I am usning httpretty==1.1.4 with python3.8.

@bblommers
Copy link
Collaborator

Hi @AltafHussain4748, which version of Moto are you using?

Can you also share an example test case that throws this error, and a full stacktrace?

@bblommers bblommers added the debugging Working with user to figure out if there is an issue label Apr 11, 2022
@bblommers
Copy link
Collaborator

Closing this - there's not much we can do without something reproducible.

@YugoHino
Copy link

YugoHino commented Aug 3, 2023

Hi ,
I'm also facing same error. When there is env variable http_proxy/https_proxy, this error is shown as following. Once I remove http_proxy/https_proxy, there is no error and complete with success for testing.

I tested with this simple examples as following,

sample_httpretty
|-- sample_httpretty
| |-- init.py
| |-- get_something.py
|-- tests
| |-- init.py
| |-- test_something.py
|-- pytest.ini

# sample_httpretty/get_something.py
import requests


class getSomething(object):
    def get_something(self):
        url = "https://httpbin.org/get"
        r = requests.get(url)
        return r
# tests/test_something.py
import json
import unittest

import httpretty

from sample_httpretty.get_something import getSomething


class testGetSomething(unittest.TestCase):
    def test_get_something(self):
        res_test = {
            "args": {},
            "headers": {
                "Accept": "*/*",
                "Accept-Encoding": "gzip, deflate",
                "Host": "httpbin.org",
                "User-Agent": "python-requests/2.31.0",
                "X-Amzn-Trace-Id": "Root=1-64cb067e-588612f81f427ba53bcc98a1",
            },
            "origin": "123.123.123.123",
            "url": "https://httpbin.org/get",
        }
        httpretty.enable(allow_net_connect=False)
        httpretty.register_uri(
            httpretty.GET,
            "https://httpbin.org/get",
            body=json.dumps(res_test),
            content_type="application/json",
        )
        res = getSomething().get_something()
        print(res.text)

============================================================================================================================= FAILURES ==============================================================================================================================
________________________________________________________________________________________________________________ testGetSomething.test_get_something ________________________________________________________________________________________________________________

self = <tests.test_something.testGetSomething testMethod=test_get_something>

def test_get_something(self):
    res_test = {
        "args": {},
        "headers": {
            "Accept": "*/*",
            "Accept-Encoding": "gzip, deflate",
            "Host": "httpbin.org",
            "User-Agent": "python-requests/2.31.0",
            "X-Amzn-Trace-Id": "Root=1-64cb067e-588612f81f427ba53bcc98a1",
        },
        "origin": "123.123.123.123",
        "url": "https://httpbin.org/get",
    }
    httpretty.enable(allow_net_connect=False)
    httpretty.register_uri(
        httpretty.GET,
        "https://httpbin.org/get",
        body=json.dumps(res_test),
        content_type="application/json",
    )
  res = getSomething().get_something()

tests\test_something.py:30:


sample_httpretty\get_something.py:8: in get_something
r = requests.get(url)
.venv\lib\site-packages\requests\api.py:73: in get
return request("get", url, params=params, **kwargs)
.venv\lib\site-packages\requests\api.py:59: in request
return session.request(method=method, url=url, **kwargs)
.venv\lib\site-packages\requests\sessions.py:589: in request
resp = self.send(prep, **send_kwargs)
.venv\lib\site-packages\requests\sessions.py:703: in send
r = adapter.send(request, **kwargs)
.venv\lib\site-packages\requests\adapters.py:486: in send
resp = conn.urlopen(
.venv\lib\site-packages\urllib3\connectionpool.py:776: in urlopen
self._prepare_proxy(conn)
.venv\lib\site-packages\urllib3\connectionpool.py:1041: in _prepare_proxy
conn.connect()
.venv\lib\site-packages\urllib3\connection.py:611: in connect
self.sock = sock = self._new_conn()
.venv\lib\site-packages\urllib3\connection.py:203: in _new_conn
sock = connection.create_connection(
.venv\lib\site-packages\urllib3\util\connection.py:73: in create_connection
sock.connect(sa)
.venv\lib\site-packages\httpretty\core.py:609: in connect
self.connect_truesock(address=address)


self = httpretty.core.socket("proxy01.ad.local:8080"), request = None, address = ('proxy01.ad.local', 8080)

def connect_truesock(self, request=None, address=None):
    address = address or self._address

    if self.__truesock_is_connected__:
        return self.truesock

    if request:
        logger.warning('real call to socket.connect() for {request}'.format(**locals()))
    elif address:
        logger.warning('real call to socket.connect() for {address}'.format(**locals()))
    else:
        logger.warning('real call to socket.connect()')

    if httpretty.allow_net_connect and not self.truesock:
        self.truesock = self.create_socket(address)
    elif not self.truesock:
      raise UnmockedError('Failed to socket.connect() because because a real socket was never created.', request=request, address=address)

E httpretty.errors.UnmockedError: Failed to socket.connect() because because a real socket was never created.
E
E address: proxy01.ad.local:8080 | Tip: You could try setting (allow_net_connect=True) to allow unregistered requests through a real TCP connection in addition to (verbose=True) to debug the issue.

.venv\lib\site-packages\httpretty\core.py:645: UnmockedError
------------------------------------------------------------------------------------------------------------------------- Captured log call -------------------------------------------------------------------------------------------------------------------------
WARNING httpretty.core:core.py:638 real call to socket.connect() for ('proxy01.ad.local', 8080)
====================================================================================================================== short test summary info ======================================================================================================================
FAILED tests/test_something.py::testGetSomething::test_get_something - httpretty.errors.UnmockedError: Failed to socket.connect() because because a real socket was never created.
========================================================================================================================= 1 failed in 0.58s =========================================================================================================================

@YugoHino
Copy link

YugoHino commented Aug 3, 2023

This looks same as #122

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
debugging Working with user to figure out if there is an issue
Projects
None yet
Development

No branches or pull requests

3 participants