File: test_testing.py

package info (click to toggle)
python-cross-web 0.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 744 kB
  • sloc: python: 2,262; sh: 23; makefile: 10
file content (96 lines) | stat: -rw-r--r-- 2,882 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import pytest
from cross_web.request._testing import TestingRequestAdapter
from cross_web.request._base import FormData


@pytest.mark.asyncio
async def test_testing_request_adapter_defaults() -> None:
    adapter = TestingRequestAdapter()

    assert adapter.method == "POST"
    assert adapter.query_params == {}
    assert adapter.headers == {}
    assert adapter.content_type is None
    assert adapter.url == "http://testserver/"
    assert adapter.cookies == {}

    body = await adapter.get_body()
    assert body == b""

    form_data = await adapter.get_form_data()
    assert form_data.files == {}
    assert form_data.form == {}


@pytest.mark.asyncio
async def test_testing_request_adapter_custom_values() -> None:
    adapter = TestingRequestAdapter(
        method="GET",
        query_params={"key": "value"},
        headers={"X-Custom": "header"},
        content_type="application/json",
        url="http://example.com/test",
        cookies={"session": "abc123"},
        form_data=FormData(files={"file": "data"}, form={"field": "value"}),
    )

    assert adapter.method == "GET"
    assert adapter.query_params == {"key": "value"}
    assert adapter.headers == {"X-Custom": "header"}
    assert adapter.content_type == "application/json"
    assert adapter.url == "http://example.com/test"
    assert adapter.cookies == {"session": "abc123"}

    form_data = await adapter.get_form_data()
    assert form_data.files == {"file": "data"}
    assert form_data.form == {"field": "value"}


@pytest.mark.asyncio
async def test_testing_request_adapter_json_body() -> None:
    json_data = {"test": "data", "number": 123}
    adapter = TestingRequestAdapter(json=json_data)

    body = await adapter.get_body()
    assert body == b'{"test": "data", "number": 123}'


@pytest.mark.asyncio
async def test_testing_request_adapter_mixed_data() -> None:
    # Test that json takes precedence over form_data for body
    adapter = TestingRequestAdapter(
        json={"json": "data"},
        form_data=FormData(files={}, form={"form": "data"}),
    )

    body = await adapter.get_body()
    assert body == b'{"json": "data"}'

    # But form_data is still accessible via get_form_data
    form_data = await adapter.get_form_data()
    assert form_data.form == {"form": "data"}


@pytest.mark.asyncio
async def test_testing_request_adapter_empty_json() -> None:
    adapter = TestingRequestAdapter(json={})

    body = await adapter.get_body()
    assert body == b"{}"


@pytest.mark.asyncio
async def test_testing_request_adapter_complex_json() -> None:
    complex_data = {
        "nested": {"inner": [1, 2, 3]},
        "boolean": True,
        "null": None,
        "string": "test",
    }
    adapter = TestingRequestAdapter(json=complex_data)

    body = await adapter.get_body()
    import json

    decoded = json.loads(body)
    assert decoded == complex_data