gabrielfalcao / HTTPretty

Intercept HTTP requests at the Python socket level. Fakes the whole socket module
https://httpretty.readthedocs.org
MIT License
2.11k stars 276 forks source link

Record/Replay feature returns incorrect HTTP status code #441

Open mhaas opened 3 years ago

mhaas commented 3 years ago

I am not sure if this feature is already released since #10 is still open. However, "record and playback" is present in the main README, so I gave it a shot.

It seems that HTTP status codes are not replayed correctly:

def test_record_replay_2():
    record_file = "/tmp/http_2.json"

    if os.path.exists(record_file):
        context_manager = httpretty.HTTPretty.playback(record_file, allow_net_connect=False,
                                      verbose=True)
    else:
        context_manager = httpretty.HTTPretty.record(record_file, allow_net_connect=True, verbose=True)

    with context_manager:
        response = requests.get("https://httpstat.us/500")
    assert response.status_code == 500

On the second time the test is run, the "playback" feature will be used, but the assertion will fail:

        with context_manager:
            response = requests.get("https://httpstat.us/500")
>       assert response.status_code == 500
E       assert 200 == 500

More interestingly, this issue does not happen when the test is executed as follows:

def test_record_replay():
    record_file = "/tmp/http.json"

    def request_and_test():
        response = requests.get("https://httpstat.us/500")
        assert response.status_code == 500

    with httpretty.HTTPretty.record(record_file, allow_net_connect=True, verbose=True):
        request_and_test()

    with httpretty.HTTPretty.playback(record_file, allow_net_connect=False,
                                      verbose=True):
        request_and_test()

Is this an operator error here or is there a real bug here? I do not see the issue with my code, but that does not mean it is not there.

gabrielfalcao commented 3 years ago

@mhaas thanks for reporting, I'll work on a fix as soon as possible