File: test_aiohttp_validation.py

package info (click to toggle)
python-openapi-core 0.19.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,008 kB
  • sloc: python: 18,868; makefile: 47
file content (100 lines) | stat: -rw-r--r-- 2,708 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
97
98
99
100
from __future__ import annotations

from typing import TYPE_CHECKING
from unittest import mock

import pytest

if TYPE_CHECKING:
    from aiohttp.test_utils import TestClient


async def test_aiohttp_integration_valid_input(client: TestClient):
    # Given
    given_query_string = {
        "q": "string",
    }
    given_headers = {
        "content-type": "application/json",
        "Host": "localhost",
    }
    given_data = {"param1": 1}
    expected_status_code = 200
    expected_response_data = {"data": "data"}
    # When
    response = await client.post(
        "/browse/12/",
        params=given_query_string,
        json=given_data,
        headers=given_headers,
    )
    response_data = await response.json()
    # Then
    assert response.status == expected_status_code
    assert response_data == expected_response_data


async def test_aiohttp_integration_invalid_server(client: TestClient, request):
    if "no_validation" in request.node.name:
        pytest.skip("No validation for given handler.")
    # Given
    given_query_string = {
        "q": "string",
    }
    given_headers = {
        "content-type": "application/json",
        "Host": "petstore.swagger.io",
    }
    given_data = {"param1": 1}
    expected_status_code = 400
    expected_response_data = {
        "errors": [
            {
                "message": (
                    "Server not found for "
                    "http://petstore.swagger.io/browse/12/"
                ),
            }
        ]
    }
    # When
    response = await client.post(
        "/browse/12/",
        params=given_query_string,
        json=given_data,
        headers=given_headers,
    )
    response_data = await response.json()
    # Then
    assert response.status == expected_status_code
    assert response_data == expected_response_data


async def test_aiohttp_integration_invalid_input(
    client: TestClient, response_getter, request
):
    if "no_validation" in request.node.name:
        pytest.skip("No validation for given handler.")
    # Given
    given_query_string = {
        "q": "string",
    }
    given_headers = {
        "content-type": "application/json",
        "Host": "localhost",
    }
    given_data = {"param1": "string"}
    response_getter.return_value = {"data": 1}
    expected_status_code = 400
    expected_response_data = {"errors": [{"message": mock.ANY}]}
    # When
    response = await client.post(
        "/browse/12/",
        params=given_query_string,
        json=given_data,
        headers=given_headers,
    )
    response_data = await response.json()
    # Then
    assert response.status == expected_status_code
    assert response_data == expected_response_data