-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtest_http_requests.py
139 lines (105 loc) · 3.51 KB
/
test_http_requests.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
from __future__ import annotations
import json
from typing import Any
import pytest
import responses
from requests import HTTPError, Session
from tests.data.test_defaults import DEFAULT_TOKEN
from todoist_api_python.endpoints import BASE_URL, TASKS_ENDPOINT
from todoist_api_python.http_requests import delete, get, post
DEFAULT_URL = f"{BASE_URL}/{TASKS_ENDPOINT}"
@responses.activate
def test_get_with_params(default_task_response: dict[str, Any]) -> None:
params = {"param1": "value1", "param2": "value2"}
responses.add(
responses.GET,
DEFAULT_URL,
json=default_task_response,
status=200,
)
response = get(Session(), DEFAULT_URL, DEFAULT_TOKEN, params)
assert len(responses.calls) == 1
assert (
responses.calls[0].request.url == f"{DEFAULT_URL}?param1=value1¶m2=value2"
)
assert (
responses.calls[0].request.headers["Authorization"] == f"Bearer {DEFAULT_TOKEN}"
)
assert response == default_task_response
@responses.activate
def test_get_raise_for_status() -> None:
responses.add(
responses.GET,
DEFAULT_URL,
json="<error description>",
status=500,
)
with pytest.raises(HTTPError) as error_info:
get(Session(), DEFAULT_URL, DEFAULT_TOKEN)
assert error_info.value.response.content == b'"<error description>"'
@responses.activate
def test_post_with_data(default_task_response: dict[str, Any]) -> None:
request_id = "12345"
data = {"param1": "value1", "param2": "value2", "request_id": request_id}
responses.add(
responses.POST,
DEFAULT_URL,
json=default_task_response,
status=200,
)
response = post(Session(), DEFAULT_URL, DEFAULT_TOKEN, data)
assert len(responses.calls) == 1
assert responses.calls[0].request.url == DEFAULT_URL
assert (
responses.calls[0].request.headers["Authorization"] == f"Bearer {DEFAULT_TOKEN}"
)
assert responses.calls[0].request.headers["X-Request-Id"] == request_id
assert (
responses.calls[0].request.headers["Content-Type"]
== "application/json; charset=utf-8"
)
assert responses.calls[0].request.body == json.dumps(data)
assert response == default_task_response
@responses.activate
def test_post_return_ok_when_no_response_body() -> None:
responses.add(
responses.POST,
DEFAULT_URL,
status=204,
)
result = post(Session(), DEFAULT_URL, DEFAULT_TOKEN)
assert result is True
@responses.activate
def test_post_raise_for_status() -> None:
responses.add(
responses.POST,
DEFAULT_URL,
status=500,
)
with pytest.raises(HTTPError):
post(Session(), DEFAULT_URL, DEFAULT_TOKEN)
@responses.activate
def test_delete_with_request_id() -> None:
request_id = "12345"
responses.add(
responses.DELETE,
DEFAULT_URL,
status=204,
)
result = delete(Session(), DEFAULT_URL, DEFAULT_TOKEN, {"request_id": request_id})
assert len(responses.calls) == 1
assert responses.calls[0].request.url == DEFAULT_URL
assert (
responses.calls[0].request.headers["Authorization"] == f"Bearer {DEFAULT_TOKEN}"
)
assert responses.calls[0].request.headers["X-Request-Id"] == request_id
assert result is True
@responses.activate
def test_delete_raise_for_status() -> None:
responses.add(
responses.DELETE,
DEFAULT_URL,
status=500,
)
with pytest.raises(HTTPError):
delete(Session(), DEFAULT_URL, DEFAULT_TOKEN)