-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathtest_https.py
More file actions
93 lines (69 loc) · 2.56 KB
/
test_https.py
File metadata and controls
93 lines (69 loc) · 2.56 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import json
import os
import tempfile
from urllib.request import urlopen
import pytest
import requests
from mocket import Mocket, Mocketizer, mocketize
from mocket.mockhttp import Entry # noqa - test retrocompatibility
@pytest.fixture
def url_to_mock():
return "https://httpbin.org/ip"
@pytest.fixture
def response():
return {
"integer": 1,
"string": "asd",
"boolean": False,
}
@mocketize
def test_json(response):
url_to_mock = "https://testme.org/json"
Entry.single_register(
Entry.GET,
url_to_mock,
body=json.dumps(response),
headers={"content-type": "application/json"},
)
mocked_response = requests.get(url_to_mock).json()
assert response == mocked_response
mocked_response = json.loads(urlopen(url_to_mock).read().decode("utf-8"))
assert response == mocked_response
@pytest.mark.skipif('os.getenv("SKIP_TRUE_HTTP", False)')
@pytest.mark.xfail(reason="Service down or blocking GitHub actions IPs")
def test_truesendall_with_recording_https(url_to_mock):
with tempfile.TemporaryDirectory() as temp_dir, Mocketizer(
truesocket_recording_dir=temp_dir
):
requests.get(url_to_mock, headers={"Accept": "application/json"})
resp = requests.get(url_to_mock, headers={"Accept": "application/json"})
assert resp.status_code == 200
dump_filename = os.path.join(
Mocket.get_truesocket_recording_dir(),
Mocket.get_namespace() + ".json",
)
with open(dump_filename) as f:
responses = json.load(f)
assert len(responses["httpbin.org"]["443"].keys()) == 1
@pytest.mark.skipif('os.getenv("SKIP_TRUE_HTTP", False)')
@pytest.mark.xfail(reason="Service down or blocking GitHub actions IPs")
def test_truesendall_after_mocket_session(url_to_mock):
Mocket.enable()
Mocket.disable()
resp = requests.get(url_to_mock)
assert resp.status_code == 200
@pytest.mark.skipif('os.getenv("SKIP_TRUE_HTTP", False)')
@pytest.mark.xfail(reason="Service down or blocking GitHub actions IPs")
def test_real_request_session(url_to_mock):
session = requests.Session()
url_to_compare = "http://httpbin.org/headers"
with Mocketizer():
assert len(session.get(url_to_mock).content) < len(
session.get(url_to_compare).content
)
@mocketize
def test_raise_exception_from_single_register():
url = "https://github.com/fluidicon.png"
Entry.single_register(Entry.GET, url, exception=OSError())
with pytest.raises(requests.exceptions.ConnectionError):
requests.get(url)