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

Requests custom adapter is ignored #83

Closed
dapicester opened this issue Feb 20, 2019 · 3 comments
Closed

Requests custom adapter is ignored #83

dapicester opened this issue Feb 20, 2019 · 3 comments

Comments

@dapicester
Copy link

Hi, I use a custom adapter to implement a retry in a client class:

 session = requests.Session()
 retry = requests.packages.urllib3.Retry(
   total=5,
   backoff_factor=0.1,
   method_whitelist=('GET', 'POST', 'DELETE'),
   status_forcelist=(502, 503, 504)
)
session.mount(BASE_URL, requests.adapters.HTTPAdapter(max_retries=retry))

Then session is used to make requests like:

session.request('GET', BASE_URL)

Finally I test my client class but the retry doesn't work:

@requests_mock.Mocker()
def test_retry(self, mocker):
  mocker.register_uri('GET', BASE_URL, [
    {'status_code': 502},
    {'status_code': 200, text='ok'}
  )
  response = my_client.get_stuff()
  self.assertEqual(response.status_code, 200) # this fails, got 502

This issue may be related to #20.

@jamielennox
Copy link
Owner

Hi, sorry - I don't think there's a way to support a custom adapter in requests-mock.

Internally it's working at the same adapter level you are, so installing the mocker like that just means that we always return the mock adapter. As a stable interface this was really the easiest way to implement it.

You could have a look at httpretty which is mocking at the socket level for this so is independant of requests, but part of the reason i made this library was following the socket interface was really difficult and there were always edge cases.

@dapicester
Copy link
Author

OK, I'll try httpretty. Thanks for the explanation.

@mazulo
Copy link

mazulo commented Jun 15, 2020

So just for the sake of reference I will post this in every place I was looking for, in case someone else get here looking for this answer...

I spent the last few days trying to achieve this with requests-mock, read this and several other issues and couldn't make it work. In the end httpretty was my final solution with the following code:

@httpretty.activate
def test_with_callback_response():
    def request_callback(request, uri, response_headers):
        return [500, response_headers, json.dumps({"hello": "world"})]

    http_adapter = TimeoutHTTPAdapter()
    session = requests.Session()
    session.mount('http://', adapter=http_adapter)
    session.mount('https://', adapter=http_adapter)

    httpretty.register_uri(
        httpretty.GET,
        re.compile(r'http://.*'),
        responses=[
            httpretty.Response(
                body='{"message": "HTTPretty :)"}',
                status=403,
            ),
            httpretty.Response(
                body='{"message": "HTTPretty :)"}',
                status=429,
            ),
            httpretty.Response(
                body='{"message": "HTTPretty :)"}',
                status=500,
            ),
        ]
    )

    try:
        response = session.get("http://example.co/status/")
    except Exception as e:
        print(len(httpretty.latest_requests()))

The line under except will print 4 because max_retries=3 on my TimeoutHTTPAdapter.

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

No branches or pull requests

3 participants