-
-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathtest_requests_validation.py
171 lines (156 loc) · 5.67 KB
/
test_requests_validation.py
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
from types import GeneratorType
import pytest
import requests
import responses
from openapi_core import V31RequestUnmarshaller
from openapi_core import V31ResponseUnmarshaller
from openapi_core import V31WebhookRequestUnmarshaller
from openapi_core import V31WebhookResponseUnmarshaller
from openapi_core.contrib.requests import RequestsOpenAPIRequest
from openapi_core.contrib.requests import RequestsOpenAPIResponse
from openapi_core.contrib.requests import RequestsOpenAPIWebhookRequest
from openapi_core.datatypes import Parameters
from openapi_core.datatypes import RequestParameters
class TestRequestsOpenAPIValidation:
@pytest.fixture
def spec(self, factory):
specfile = "contrib/requests/data/v3.0/requests_factory.yaml"
return factory.spec_from_file(specfile)
@pytest.fixture
def request_unmarshaller(self, spec):
return V31RequestUnmarshaller(spec)
@pytest.fixture
def response_unmarshaller(self, spec):
return V31ResponseUnmarshaller(spec)
@pytest.fixture
def webhook_request_unmarshaller(self, spec):
return V31WebhookRequestUnmarshaller(spec)
@pytest.fixture
def webhook_response_unmarshaller(self, spec):
return V31WebhookResponseUnmarshaller(spec)
@responses.activate
def test_response_validator_path_pattern(self, response_unmarshaller):
responses.add(
responses.POST,
"http://localhost/browse/12/?q=string",
json={"data": "data"},
status=200,
match_querystring=True,
headers={"X-Rate-Limit": "12"},
)
request = requests.Request(
"POST",
"http://localhost/browse/12/",
params={"q": "string"},
headers={"content-type": "application/json"},
json={"param1": 1},
)
request_prepared = request.prepare()
session = requests.Session()
response = session.send(request_prepared)
openapi_request = RequestsOpenAPIRequest(request)
openapi_response = RequestsOpenAPIResponse(response)
result = response_unmarshaller.unmarshal(
openapi_request, openapi_response
)
assert not result.errors
def test_request_validator_path_pattern(self, request_unmarshaller):
request = requests.Request(
"POST",
"http://localhost/browse/12/",
params={"q": "string"},
headers={"content-type": "application/json"},
json={"param1": 1},
)
openapi_request = RequestsOpenAPIRequest(request)
result = request_unmarshaller.unmarshal(openapi_request)
assert not result.errors
def test_request_validator_encoded_chunks(self, request_unmarshaller):
request_chunks = [
b'{',
b'"param1": 1',
b'}',
]
def gen():
for chunk in request_chunks:
yield chunk
request = requests.Request(
"POST",
"http://localhost/browse/12/",
params={"q": "string"},
headers={"content-type": "application/json"},
data=gen(),
)
openapi_request = RequestsOpenAPIRequest(request)
result = request_unmarshaller.unmarshal(openapi_request)
assert not result.errors
assert result.body == {"param1": 1}
assert result.parameters == Parameters(
query={"q": "string"},
header={},
cookie={},
path={"id": 12},
)
assert isinstance(request.data, GeneratorType)
assert list(request.data) == request_chunks
def test_request_validator_prepared_request(self, request_unmarshaller):
request = requests.Request(
"POST",
"http://localhost/browse/12/",
params={"q": "string"},
headers={"content-type": "application/json"},
json={"param1": 1},
)
request_prepared = request.prepare()
openapi_request = RequestsOpenAPIRequest(request_prepared)
result = request_unmarshaller.unmarshal(openapi_request)
assert not result.errors
def test_webhook_request_validator_path(
self, webhook_request_unmarshaller
):
request = requests.Request(
"POST",
"http://otherhost/callback/",
headers={
"content-type": "application/json",
"X-Rate-Limit": "12",
},
json={"id": 1},
)
openapi_webhook_request = RequestsOpenAPIWebhookRequest(
request, "resourceAdded"
)
result = webhook_request_unmarshaller.unmarshal(
openapi_webhook_request
)
assert not result.errors
@responses.activate
def test_webhook_response_validator_path(
self, webhook_response_unmarshaller
):
responses.add(
responses.POST,
"http://otherhost/callback/",
json={"data": "data"},
status=200,
)
request = requests.Request(
"POST",
"http://otherhost/callback/",
headers={
"content-type": "application/json",
"X-Rate-Limit": "12",
},
json={"id": 1},
)
request_prepared = request.prepare()
session = requests.Session()
response = session.send(request_prepared)
openapi_webhook_request = RequestsOpenAPIWebhookRequest(
request, "resourceAdded"
)
openapi_response = RequestsOpenAPIResponse(response)
result = webhook_response_unmarshaller.unmarshal(
openapi_webhook_request, openapi_response
)
assert not result.errors