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 does not patch when requests.Session was already used with the same domain #381

Open
maingoh opened this issue Sep 23, 2019 · 1 comment

Comments

@maingoh
Copy link

maingoh commented Sep 23, 2019

With the code below, the last call should return a 502 but it returns a 404.
If I remove or change the first session.get() to call another domain it will work.
This is a bit frustrating as I cannot fully use this library to test an http client with a fixed domain.

import httpretty
import requests

session = requests.Session()

r = session.get("https://google.com/foo")

with httpretty.enabled():

    httpretty.register_uri(
        httpretty.GET,
        'https://google.com/bar',
        status=502,
    )

    r = session.get("https://google.com/bar")
    print(r)
    print("nb requests done with httpretty:", len(httpretty.httpretty.latest_requests))

httpretty 0.9.6
requests 2.22.0

Thank you very much,

@iNishant
Copy link

iNishant commented Apr 5, 2024

While this is indeed frustrating, it is expected.

  • The first time you use the session r = session.get("https://google.com/foo"), a connection pool will be made for the host google.com and will be stored in a pool list inside the session object.
  • A real socket/connection will be created to make the actual request. Since you are using a Session, the real socket will stay in the pool for google.com after the first request completes.
  • Now when you activate httpretty using httpretty.enabled(), socket.socket is patched (https://github.com/gabrielfalcao/HTTPretty/blob/main/httpretty/core.py#L1813), but this impacts only new sockets being created.
  • When you make the second request r = session.get("https://google.com/bar"), the session already has a pool for the domain google.com (with a real socket) https://github.com/urllib3/urllib3/blob/5c5fcb0556ab2c121e2a3723cf9d78cb5a780854/src/urllib3/poolmanager.py#L343. So requests ends up using the real socket, hence leading to a 404.
  • Since httpretty's fake socket was never created while making the second request, the registered mocks don't work.

Workaround is probably to use a mocked response for the first request also?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants