-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_http_client.py
More file actions
75 lines (66 loc) · 2.01 KB
/
test_http_client.py
File metadata and controls
75 lines (66 loc) · 2.01 KB
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
import pytest
import os
import json
from socket_function import get_do
TEST_FILE = "test_output.html"
@pytest.fixture(autouse=True)
def cleanup_file():
yield
if os.path.exists(TEST_FILE):
os.remove(TEST_FILE)
def test_get_request_to_httpbin():
url = "https://httpbin.org/html"
cookies = get_do(
url=url,
headers={},
body="",
method="GET",
timeout=10,
file_name=TEST_FILE,
cookie={}
)
assert os.path.exists(TEST_FILE) # проверка на то, что файл создан
with open(TEST_FILE, "r", encoding="utf-8") as f:
content = f.read()
assert "<h1>Herman Melville - Moby-Dick</h1>" in content
assert "</html>" in content
def test_real_chunked_request():
url = "https://httpbin.org/stream/1"
cookies = get_do(
url=url,
headers={},
body="",
method="GET",
timeout=10,
file_name=TEST_FILE,
cookie={}
)
assert os.path.exists(TEST_FILE)
with open(TEST_FILE, 'r', encoding='utf-8') as f:
content = f.read()
try:
data = json.loads(TEST_FILE)
assert "id" in data
assert "url" in data
assert data["url"] == "https://httpbin.org/stream/1"
except json.JSONDecodeError:
pytest.fail("Не удалось распарсить JSON из ответа, chunked-декодер работает неверно.")
def test_real_redirect_handling():
url = "https://httpbin.org/redirect/1"
cookies = get_do(
url=url,
headers={},
body="",
method="GET",
timeout=10,
file_name=TEST_FILE,
cookie={}
)
assert os.path.exists(TEST_FILE)
with open(TEST_FILE, "r", encoding="utf-8") as f:
content = f.read()
try:
data = json.loads(content)
assert data["url"] == "https://httpbin.org/get"
except json.JSONDecodeError:
pytest.fail("Ответ после редиректа не является валидным JSON.")