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

Allow external requests #93

Closed
wants to merge 3 commits into from

Conversation

k4nar
Copy link

@k4nar k4nar commented Dec 17, 2015

Sometimes,you want to mock a specific service, but you still want to be able to reach other services. With this PR, if allow_external_requests is True, calls to non mocked urls don't throw ConnectionError.

This is somewhat similar to #39.

EDIT: If someone is interested by this behavior, note that change may never be included in responses, as per @dcramer's comment in #39.
If you need a way to mock an API while being able to make external requests, request-mock seems to be a good option.

@bersace
Copy link

bersace commented Dec 21, 2015

How to allow external requests with responses.activate ?

@k4nar
Copy link
Author

k4nar commented Dec 21, 2015

@bersace : Like with assert_all_requests_are_fired, you have to instanciate a RequestsMock object. For example:

rsps = RequestsMock(allow_external_requests=True)

@rsps.activate()
def test_some_stuff():
    ...

@bersace
Copy link

bersace commented Dec 21, 2015

ok. cool.

@Daenyth
Copy link

Daenyth commented Jan 11, 2016

Does this have a large benefit over doing the first request inside a with block and the second request outside it? It seems like it would make it easy to make mistakes when writing a test with this enabled.

@k4nar
Copy link
Author

k4nar commented Jan 11, 2016

@Daenyth : If you can make your request outside the with then no, there is no benefit.
However if you are testing a function calling another service that you don't want to mock then you don't really have the choice.

@adamtheturtle
Copy link

To add my 👍 to this: I wanted to add tests to a legacy function which uses requests to interact with two services. In order to use responses for one service, I'd have to create mocks for both, which I'm just not ready to do. Fortunately, switching to requests-mock wasn't too painful but I'd have rather not done this.

@dcramer
Copy link
Member

dcramer commented Aug 3, 2017

i realize this is ancient, but I do want to get support for this in. That said, I'd like to have the behavior be something like responses.allow_external_domains('domain.com', 'otherdomain.com'), since one of responses goals is to ensure you dont accidentally rely on being network connected

@kathawala
Copy link
Contributor

I think this PR should be closed, as the functionality proposed here was added by #174

@k4nar
Copy link
Author

k4nar commented Oct 17, 2017

Cool thanks 👍 .

@k4nar k4nar closed this Oct 17, 2017
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

Successfully merging this pull request may close these issues.

None yet

6 participants