getsentry / responses

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

RequestsMock doesn't catch requests inside loop.run_in_executor(None, ...) #703

Open andrei-shabanski opened 5 months ago

andrei-shabanski commented 5 months ago

Describe the bug

RequestsMock doesn't catch requests that are sent inside the default asyncio executor. As a result, real HTTP requests are sent.

Additional context

Python 3.9 - 3.11

Version of responses

0.24.1

Steps to Reproduce

import asyncio
import responses
import requests

def send_request():
    # Here a real HTTP request will be sent
    response = requests.get('https://example.com/')
    print(response.content)

def main():
    with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
        rsps.get('https://example.com/', body='test')

        loop = asyncio.get_event_loop()
        loop.run_in_executor(None, send_request)
        # but   send_request()  works correct and DOES NOT send a real HTTP request

main()

Expected Result

test should be printed.

Actual Result

A real HTTP request is sent to https://example.com/, and a page body b'<!doctype html>\n<html>\n<head>\n <title>Example Domain</title>.... is printed.