getsentry / responses

A utility for mocking out the Python Requests library.
Apache License 2.0
4.14k stars 354 forks source link

How to pass through real request by default? It will raise requests.exceptions.ConnectionError #632

Closed vba34520 closed 1 year ago

vba34520 commented 1 year ago

main.py

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return 'Hello World!'

@app.route('/user/<name>')
def user(name):
    return f'Hello {name}!'

if __name__ == '__main__':
    app.run()

test_main.py

import requests
import responses

def test_api():
    with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
        rsps.get(url='http://127.0.0.1:5000/', body='test')
        resp = requests.get('http://127.0.0.1:5000/')
        assert resp.text == 'test'  # mock interface
        resp = requests.get('http://127.0.0.1:5000/user/responses')
        assert resp.text == 'Hello responses!'  # real interface

It raised requests.exceptions.ConnectionError: Connection refused by Responses - the call doesn't match any registered mock.

How to fix it? Thanks!

beliaev-maksim commented 1 year ago

Hello, Did you already go through the steps described in the readme?

vba34520 commented 1 year ago

No, I did not.

Is it possible to go through automatially?

Because there are many interfaces from different origin servers to call.

beliaev-maksim commented 1 year ago

Yes, please go through readme and let us know