Can't use timeout setting in register_uri or direct requests

Bug #1650858 reported by Meet
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
requests-mock
Won't Fix
Undecided
Unassigned

Bug Description

Not able to use

with requests_mock.Mocker() as m:
    m.head("https://google.com", timeout=15, status_code=404)
    # and also
    m.register_uri('HEAD', "https://google.com", timeout=15, status_code=404)

It throws:

TypeError: Too many arguments provided. Unexpected arguments timeout.

Is there any way to make mock the server to respond after a given amount of time?

Meet (meet123mangukiya)
description: updated
Revision history for this message
Jamie Lennox (jamielennox) wrote :

So yea, the error I think is fairly explanatory that timeout isn't a vaild argument. I'm not sure i would want to add it either, adding a pause for a web response is a fairly unusual situation for a unit test as you'll be stuck waiting for the pause.

You can however do it yourself pretty easily.

In times I've wanted to test an actual timeout i've done

m.head('https://www.google.com', exc=requests.Timeout)

which will trigger the same exception requests does when it times out. If you actually want to wait you could do:

def response_wait(request, context):
    time.sleep(15)
    return u''

m.head('https://www.google.com', text=response_wait, status_code=404)

More on dynamic responses: http://requests-mock.readthedocs.io/en/latest/response.html#dynamic-response

Does that satisfy your question?

Revision history for this message
Jamie Lennox (jamielennox) wrote :

Also to clarify

m.head(**kwargs)

is an alias of

m.register_uri('HEAD', **kwargs)

so i wouldn't expect something to work in one place and not the other.

Revision history for this message
Jamie Lennox (jamielennox) wrote :

Closing this as we haven't looked at it for a while.

I don't see much value in having a sleep period in a mocking library. If you want to trigger a timeout you can raise the exception directly but i don't think slowing down unit testing is that useful and there is a hack around it to add this functionality if you really want it.

Changed in requests-mock:
status: New → Won't Fix
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.