|
9 | 9 | CreateEtchPacketPayload, |
10 | 10 | ForgeSubmitPayload, |
11 | 11 | ) |
| 12 | +from python_anvil.constants import VALID_HOSTS |
12 | 13 |
|
13 | 14 | from ..api_resources.payload import FillPDFPayload |
14 | 15 | from . import payloads |
@@ -396,3 +397,61 @@ def test_minimum_valid_data_submission(m_request_post, anvil): |
396 | 397 | anvil.forge_submit(payload=payload) |
397 | 398 | assert m_request_post.call_count == 1 |
398 | 399 | assert _expected_data in m_request_post.call_args |
| 400 | + |
| 401 | + def describe_rest_request_absolute_url_behavior(): |
| 402 | + @pytest.mark.parametrize( |
| 403 | + "url, should_raise", |
| 404 | + [ |
| 405 | + ("some/relative/path", True), |
| 406 | + ("https://external.example.com/full/path/file.pdf", True), |
| 407 | + *[(host + "/some-endpoint", False) for host in VALID_HOSTS], |
| 408 | + ], |
| 409 | + ) |
| 410 | + @mock.patch("python_anvil.api_resources.requests.AnvilRequest._request") |
| 411 | + def test_get_behavior(mock_request, anvil, url, should_raise): |
| 412 | + mock_request.return_value = (b"fake_content", 200, {}) |
| 413 | + rest_client = anvil.request_fully_qualified() |
| 414 | + |
| 415 | + if should_raise: |
| 416 | + with pytest.raises( |
| 417 | + ValueError, |
| 418 | + match="URL must start with one of: https://app.useanvil.com", |
| 419 | + ): |
| 420 | + rest_client.get(url) |
| 421 | + else: |
| 422 | + rest_client.get(url) |
| 423 | + mock_request.assert_called_once_with( |
| 424 | + "GET", |
| 425 | + url, |
| 426 | + params=None, |
| 427 | + retry=True, |
| 428 | + ) |
| 429 | + |
| 430 | + @pytest.mark.parametrize( |
| 431 | + "url, should_raise", |
| 432 | + [ |
| 433 | + ("some/relative/path", True), |
| 434 | + ("https://external.example.com/full/path/file.pdf", True), |
| 435 | + *[(host + "/some-endpoint", False) for host in VALID_HOSTS], |
| 436 | + ], |
| 437 | + ) |
| 438 | + @mock.patch("python_anvil.api_resources.requests.AnvilRequest._request") |
| 439 | + def test_post_behavior(mock_request, anvil, url, should_raise): |
| 440 | + mock_request.return_value = (b"fake_content", 200, {}) |
| 441 | + rest_client = anvil.request_fully_qualified() |
| 442 | + |
| 443 | + if should_raise: |
| 444 | + with pytest.raises( |
| 445 | + ValueError, |
| 446 | + match="URL must start with one of: https://app.useanvil.com", |
| 447 | + ): |
| 448 | + rest_client.post(url, data={}) |
| 449 | + else: |
| 450 | + rest_client.post(url, data={}) |
| 451 | + mock_request.assert_called_once_with( |
| 452 | + "POST", |
| 453 | + url, |
| 454 | + json={}, |
| 455 | + retry=True, |
| 456 | + params=None, |
| 457 | + ) |
0 commit comments