|
| 1 | +import json |
| 2 | +import uuid |
| 3 | + |
1 | 4 | import pytest |
2 | 5 | import logging |
| 6 | +import requests_mock |
| 7 | +from collections import Counter |
3 | 8 |
|
4 | | -from ucloud.core.transport import RequestsTransport, Request, Response, utils |
| 9 | +from tests.test_unit.test_core.consts import TEST_URL |
| 10 | +from ucloud.core import exc |
| 11 | +from ucloud.core.transport import RequestsTransport, Request, Response, utils, http |
5 | 12 |
|
6 | 13 | logger = logging.getLogger(__name__) |
7 | 14 |
|
8 | 15 |
|
9 | | -@pytest.fixture(scope="function", autouse=True) |
10 | | -def transport(): |
| 16 | +@pytest.fixture(name="transport", scope="function", autouse=True) |
| 17 | +def transport_factory(): |
11 | 18 | return RequestsTransport() |
12 | 19 |
|
13 | 20 |
|
14 | | -class TestTransport: |
15 | | - def test_transport_send(self, transport): |
16 | | - req = Request( |
17 | | - url="http://httpbin.org/anything", |
18 | | - method="post", |
19 | | - json={"foo": "bar"}, |
20 | | - ) |
21 | | - resp = transport.send(req) |
22 | | - assert resp.text |
23 | | - assert resp.json()["json"] == {"foo": "bar"} |
24 | | - |
25 | | - def test_transport_handler(self, transport): |
26 | | - global_env = {} |
27 | | - |
28 | | - def request_handler(r): |
29 | | - global_env["req"] = r |
30 | | - return r |
31 | | - |
32 | | - def response_handler(r): |
33 | | - global_env["resp"] = r |
34 | | - return r |
35 | | - |
36 | | - transport.middleware.request(handler=request_handler) |
37 | | - transport.middleware.response(handler=response_handler) |
38 | | - |
39 | | - req = Request( |
40 | | - url="http://httpbin.org/anything", |
41 | | - method="post", |
42 | | - json={"foo": "bar"}, |
43 | | - ) |
| 21 | +@pytest.mark.parametrize( |
| 22 | + argnames=("status_code", "content", "expect", "expect_exc", "retryable"), |
| 23 | + argvalues=( |
| 24 | + ( |
| 25 | + 200, |
| 26 | + '{"Action": "Mock", "RetCode": 0}', |
| 27 | + {"Action": "Mock", "RetCode": 0}, |
| 28 | + None, |
| 29 | + False, |
| 30 | + ), |
| 31 | + (500, "{}", None, exc.HTTPStatusException, False), |
| 32 | + (429, "{}", None, exc.HTTPStatusException, True), |
| 33 | + (500, "x", None, exc.HTTPStatusException, False), |
| 34 | + (200, "x", None, exc.InvalidResponseException, False), |
| 35 | + ), |
| 36 | +) |
| 37 | +def test_transport( |
| 38 | + transport, status_code, content, expect, expect_exc, retryable |
| 39 | +): |
| 40 | + with requests_mock.Mocker() as m: |
| 41 | + m.post(TEST_URL, text=content, status_code=status_code) |
| 42 | + |
| 43 | + got_exc = None |
| 44 | + try: |
| 45 | + resp = transport.send(Request(url=TEST_URL, method="post", json={})) |
| 46 | + assert resp.json() == expect |
| 47 | + except Exception as e: |
| 48 | + got_exc = e |
| 49 | + |
| 50 | + if expect_exc: |
| 51 | + assert str(got_exc) |
| 52 | + assert got_exc.retryable == retryable |
| 53 | + assert isinstance(got_exc, expect_exc) |
| 54 | + |
| 55 | + |
| 56 | +def test_transport_handler(transport): |
| 57 | + req_key, resp_key, exc_key = "req", "resp", "exc" |
| 58 | + counter = Counter({req_key: 0, resp_key: 0, exc_key: 0}) |
| 59 | + |
| 60 | + def request_handler(r): |
| 61 | + counter[req_key] += 1 |
| 62 | + return r |
| 63 | + |
| 64 | + def response_handler(r): |
| 65 | + counter[resp_key] += 1 |
| 66 | + return r |
| 67 | + |
| 68 | + def exception_handler(r): |
| 69 | + counter[exc_key] += 1 |
| 70 | + return r |
| 71 | + |
| 72 | + transport.middleware.request(handler=request_handler) |
| 73 | + transport.middleware.response(handler=response_handler) |
| 74 | + transport.middleware.exception(handler=exception_handler) |
| 75 | + |
| 76 | + expect = {"foo": "bar"} |
| 77 | + req = Request(url=TEST_URL, method="post", json=expect) |
| 78 | + |
| 79 | + with requests_mock.Mocker() as m: |
| 80 | + request_uuid = str(uuid.uuid4()) |
| 81 | + m.post(TEST_URL, text=json.dumps(expect), status_code=200, |
| 82 | + headers={http.REQUEST_UUID_HEADER_KEY: request_uuid}) |
44 | 83 | resp = transport.send(req) |
45 | 84 | assert resp.text |
46 | | - assert resp.json()["json"] == {"foo": "bar"} |
47 | | - |
48 | | - assert "req" in global_env |
49 | | - assert "resp" in global_env |
50 | | - |
51 | | - |
52 | | -class TestResponse: |
53 | | - def test_guess_json_utf(self): |
54 | | - import json |
55 | | - |
56 | | - encodings = [ |
57 | | - "utf-32", |
58 | | - "utf-8-sig", |
59 | | - "utf-16", |
60 | | - "utf-8", |
61 | | - "utf-16-be", |
62 | | - "utf-16-le", |
63 | | - "utf-32-be", |
64 | | - "utf-32-le", |
65 | | - ] |
66 | | - for e in encodings: |
67 | | - s = json.dumps("表意字符").encode(e) |
68 | | - assert utils.guess_json_utf(s) == e |
69 | | - |
70 | | - def test_response_empty_content(self): |
71 | | - r = Response("http://foo.bar", "post") |
72 | | - assert not r.text |
| 85 | + assert resp.json() == expect |
| 86 | + assert resp.request_uuid == request_uuid |
| 87 | + |
| 88 | + with pytest.raises(Exception): |
| 89 | + transport.send(Request(url="/")) |
| 90 | + |
| 91 | + assert counter[req_key] == 2 |
| 92 | + assert counter[resp_key] == 1 |
| 93 | + assert counter[exc_key] == 1 |
| 94 | + |
| 95 | + |
| 96 | +def test_guess_json_utf(): |
| 97 | + encodings = [ |
| 98 | + "utf-32", |
| 99 | + "utf-8-sig", |
| 100 | + "utf-16", |
| 101 | + "utf-8", |
| 102 | + "utf-16-be", |
| 103 | + "utf-16-le", |
| 104 | + "utf-32-be", |
| 105 | + "utf-32-le", |
| 106 | + ] |
| 107 | + for e in encodings: |
| 108 | + s = json.dumps("表意字符").encode(e) |
| 109 | + assert utils.guess_json_utf(s) == e |
| 110 | + |
| 111 | + |
| 112 | +def test_request_methods(): |
| 113 | + req = Request( |
| 114 | + TEST_URL, data={"foo": 42}, json={"bar": 42}, params={"q": "search"} |
| 115 | + ) |
| 116 | + assert req.payload() == {"foo": 42, "bar": 42, "q": "search"} |
| 117 | + |
| 118 | + |
| 119 | +def test_response_methods(): |
| 120 | + r = Response(TEST_URL, "post") |
| 121 | + assert not r.text |
| 122 | + assert r.json() is None |
| 123 | + |
| 124 | + r = Response(TEST_URL, "post", content=b"\xd6", encoding="utf-8") |
| 125 | + with pytest.raises(exc.InvalidResponseException): |
73 | 126 | assert r.json() is None |
0 commit comments