Skip to content

How to use responses with cachecontrol?Β #780

@csm10495

Description

@csm10495

Hey folks,

We're trying to update some libs and hit an odd issue with updating responses. Consider this test:

import responses
import requests
from cachecontrol import CacheControl

def get_client():
    return CacheControl(requests.Session())

@responses.activate
def test_with_cachecontrol():
    client = get_client()
    responses.add(responses.GET, "http://test-website.com", body="Hello, world!")

    response = client.get("http://test-website.com")
    assert response.text == "Hello, world!"

    # force a connection error. Though since we're cached, it should return the cached response.
    responses.add(responses.GET, "http://test-website.com", body=ConnectionError())
    response = client.get("http://test-website.com")
    assert response.text == "Hello, world!"

(This is a simplified version of the test in our code, but should suffice.)

The gist here is we use responses to force a valid response, then after we try to force it to instead raise a ConnectionError. Normally this would lead to the cached version of the data coming back. However once we go to reponses==0.25.8 from where we previously were in responses=0.12.1 this no longer works. We get the ConnectionError raised instead of getting the cached response.

I think responses isn't running the adapters that are otherwise on the session anymore. Is there a way to still run them for this type of testing?

If it helps, heres some more info about dependencies:

CacheControl=0.14.2
requests=2.32.4

python 3.12.6 on Mac

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    Waiting for: Product Owner

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions