|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 | 15 | """Tests for firebase_admin._http_client."""
|
| 16 | +import pytest |
| 17 | +from pytest_localserver import plugin |
16 | 18 | import requests
|
17 | 19 |
|
18 | 20 | from firebase_admin import _http_client
|
19 | 21 | from tests import testutils
|
20 | 22 |
|
21 | 23 |
|
| 24 | +# Fixture for mocking a HTTP server |
| 25 | +httpserver = plugin.httpserver |
| 26 | + |
22 | 27 | _TEST_URL = 'http://firebase.test.url/'
|
23 | 28 |
|
24 | 29 |
|
@@ -59,8 +64,57 @@ def test_base_url():
|
59 | 64 | assert recorder[0].method == 'GET'
|
60 | 65 | assert recorder[0].url == _TEST_URL + 'foo'
|
61 | 66 |
|
| 67 | +def test_credential(): |
| 68 | + client = _http_client.HttpClient( |
| 69 | + credential=testutils.MockGoogleCredential()) |
| 70 | + assert client.session is not None |
| 71 | + recorder = _instrument(client, 'body') |
| 72 | + resp = client.request('get', _TEST_URL) |
| 73 | + assert resp.status_code == 200 |
| 74 | + assert resp.text == 'body' |
| 75 | + assert len(recorder) == 1 |
| 76 | + assert recorder[0].method == 'GET' |
| 77 | + assert recorder[0].url == _TEST_URL |
| 78 | + assert recorder[0].headers['Authorization'] == 'Bearer mock-token' |
| 79 | + |
62 | 80 | def _instrument(client, payload, status=200):
|
63 | 81 | recorder = []
|
64 | 82 | adapter = testutils.MockAdapter(payload, status, recorder)
|
65 | 83 | client.session.mount(_TEST_URL, adapter)
|
66 | 84 | return recorder
|
| 85 | + |
| 86 | + |
| 87 | +class TestHttpRetry(object): |
| 88 | + """Unit tests for the default HTTP retry configuration.""" |
| 89 | + |
| 90 | + @classmethod |
| 91 | + def setup_class(cls): |
| 92 | + # Turn off exponential backoff for faster execution |
| 93 | + _http_client.DEFAULT_RETRY_CONFIG.backoff_factor = 0 |
| 94 | + |
| 95 | + def test_retry_on_503(self, httpserver): |
| 96 | + httpserver.serve_content({}, 503) |
| 97 | + client = _http_client.JsonHttpClient( |
| 98 | + credential=testutils.MockGoogleCredential(), base_url=httpserver.url) |
| 99 | + with pytest.raises(requests.exceptions.HTTPError) as excinfo: |
| 100 | + client.request('get', '/') |
| 101 | + assert excinfo.value.response.status_code == 503 |
| 102 | + assert len(httpserver.requests) == 5 |
| 103 | + |
| 104 | + def test_retry_on_500(self, httpserver): |
| 105 | + httpserver.serve_content({}, 500) |
| 106 | + client = _http_client.JsonHttpClient( |
| 107 | + credential=testutils.MockGoogleCredential(), base_url=httpserver.url) |
| 108 | + with pytest.raises(requests.exceptions.HTTPError) as excinfo: |
| 109 | + client.request('get', '/') |
| 110 | + assert excinfo.value.response.status_code == 500 |
| 111 | + assert len(httpserver.requests) == 5 |
| 112 | + |
| 113 | + def test_no_retry_on_404(self, httpserver): |
| 114 | + httpserver.serve_content({}, 404) |
| 115 | + client = _http_client.JsonHttpClient( |
| 116 | + credential=testutils.MockGoogleCredential(), base_url=httpserver.url) |
| 117 | + with pytest.raises(requests.exceptions.HTTPError) as excinfo: |
| 118 | + client.request('get', '/') |
| 119 | + assert excinfo.value.response.status_code == 404 |
| 120 | + assert len(httpserver.requests) == 1 |
0 commit comments