|
| 1 | +import pytest |
| 2 | +import responses |
| 3 | + |
| 4 | +from posit.connect.sessions import Session |
| 5 | + |
| 6 | + |
| 7 | +@responses.activate |
| 8 | +def test_post_no_redirect(): |
| 9 | + url = "https://connect.example.com/api" |
| 10 | + responses.add(responses.POST, url, json={"result": "ok"}, status=200) |
| 11 | + |
| 12 | + session = Session() |
| 13 | + response = session.post(url, data={"key": "value"}) |
| 14 | + |
| 15 | + assert response.status_code == 200 |
| 16 | + assert len(responses.calls) == 1 |
| 17 | + # Confirm that the request method was POST. |
| 18 | + assert responses.calls[0].request.method == "POST" |
| 19 | + |
| 20 | + |
| 21 | +@responses.activate |
| 22 | +def test_post_with_redirect_preserve(): |
| 23 | + initial_url = "http://connect.example.com/api" |
| 24 | + redirect_url = "http://connect.example.com/redirect" |
| 25 | + |
| 26 | + responses.add(responses.POST, initial_url, status=302, headers={"location": "/redirect"}) |
| 27 | + responses.add(responses.POST, redirect_url, json={"result": "redirected"}, status=200) |
| 28 | + |
| 29 | + session = Session() |
| 30 | + response = session.post(initial_url, data={"key": "value"}, preserve_post=True) |
| 31 | + |
| 32 | + assert response.status_code == 200 |
| 33 | + assert len(responses.calls) == 2 |
| 34 | + |
| 35 | + # Both calls should use the POST method. |
| 36 | + assert responses.calls[0].request.method == "POST" |
| 37 | + assert responses.calls[1].request.method == "POST" |
| 38 | + |
| 39 | + |
| 40 | +@responses.activate |
| 41 | +def test_post_with_redirect_no_preserve(): |
| 42 | + initial_url = "http://connect.example.com/api" |
| 43 | + redirect_url = "http://connect.example.com/redirect" |
| 44 | + |
| 45 | + responses.add(responses.POST, initial_url, status=302, headers={"location": "/redirect"}) |
| 46 | + responses.add(responses.GET, redirect_url, json={"result": "redirected"}, status=200) |
| 47 | + |
| 48 | + session = Session() |
| 49 | + response = session.post(initial_url, data={"key": "value"}, preserve_post=False) |
| 50 | + |
| 51 | + assert response.status_code == 200 |
| 52 | + assert len(responses.calls) == 2 |
| 53 | + # The initial call is a POST, but the follow-up should be a GET since preserve_post is False |
| 54 | + assert responses.calls[0].request.method == "POST" |
| 55 | + assert responses.calls[1].request.method == "GET" |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.parametrize("status_code", [307, 308]) |
| 59 | +@responses.activate |
| 60 | +def test_post_redirect_307_308(status_code): |
| 61 | + initial_url = "http://connect.example.com/api" |
| 62 | + redirect_url = "http://connect.example.com/redirect" |
| 63 | + |
| 64 | + # For 307 and 308 redirects, the HTTP spec mandates preserving the method. |
| 65 | + responses.add( |
| 66 | + responses.POST, initial_url, status=status_code, headers={"location": "/redirect"} |
| 67 | + ) |
| 68 | + responses.add(responses.POST, redirect_url, json={"result": "redirected"}, status=200) |
| 69 | + |
| 70 | + session = Session() |
| 71 | + # Even with preserve_post=False, a 307 or 308 redirect should use POST. |
| 72 | + response = session.post(initial_url, data={"key": "value"}, preserve_post=False) |
| 73 | + |
| 74 | + assert response.status_code == 200 |
| 75 | + assert len(responses.calls) == 2 |
| 76 | + # Confirm that the method for the redirect is still POST. |
| 77 | + assert responses.calls[1].request.method == "POST" |
| 78 | + |
| 79 | + |
| 80 | +@responses.activate |
| 81 | +def test_post_redirect_max_redirects(): |
| 82 | + initial_url = "http://connect.example.com/api" |
| 83 | + redirect1_url = "http://connect.example.com/redirect1" |
| 84 | + redirect2_url = "http://connect.example.com/redirect2" |
| 85 | + |
| 86 | + # Build a chain of 3 redirects. |
| 87 | + responses.add(responses.POST, initial_url, status=302, headers={"location": "/redirect1"}) |
| 88 | + responses.add(responses.POST, redirect1_url, status=302, headers={"location": "/redirect2"}) |
| 89 | + responses.add(responses.POST, redirect2_url, status=302, headers={"location": "/redirect3"}) |
| 90 | + |
| 91 | + session = Session() |
| 92 | + # Limit to 2 redirects; thus, the third redirect response should not be followed. |
| 93 | + response = session.post( |
| 94 | + initial_url, data={"key": "value"}, max_redirects=2, preserve_post=True |
| 95 | + ) |
| 96 | + |
| 97 | + # The calls should include: initial, first redirect, and second redirect. |
| 98 | + assert len(responses.calls) == 3 |
| 99 | + # The final response is the one from the second redirect. |
| 100 | + assert response.status_code == 302 |
| 101 | + # The Location header should point to the third URL. |
| 102 | + assert response.headers.get("location") == "/redirect3" |
| 103 | + |
| 104 | + |
| 105 | +@responses.activate |
| 106 | +def test_post_redirect_no_location(): |
| 107 | + url = "http://connect.example.com/api" |
| 108 | + # Simulate a redirect response that lacks a Location header. |
| 109 | + responses.add(responses.POST, url, status=302, headers={}) |
| 110 | + |
| 111 | + session = Session() |
| 112 | + response = session.post(url, data={"key": "value"}) |
| 113 | + |
| 114 | + # The loop should break immediately since there is no location to follow. |
| 115 | + assert len(responses.calls) == 1 |
| 116 | + assert response.status_code == 302 |
| 117 | + |
| 118 | + |
| 119 | +@responses.activate |
| 120 | +def test_post_redirect_location_none_explicit(): |
| 121 | + url = "http://connect.example.com/api" |
| 122 | + |
| 123 | + # Use a callback to explicitly return a None for the "location" header. |
| 124 | + def request_callback(request): |
| 125 | + return (302, {"location": ""}, "Redirect without location") |
| 126 | + |
| 127 | + responses.add_callback(responses.POST, url, callback=request_callback) |
| 128 | + |
| 129 | + session = Session() |
| 130 | + response = session.post(url, data={"key": "value"}) |
| 131 | + |
| 132 | + # The redirect loop should break since location is None. |
| 133 | + assert len(responses.calls) == 1 |
| 134 | + assert response.status_code == 302 |
0 commit comments