/
test_requestions.py
146 lines (124 loc) · 5.97 KB
/
test_requestions.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
# -*- coding: utf-8 -*-
import json
import unittest
import requests
import requestions
from requestions import httpetrified
class TestRequestSerialization(unittest.TestCase):
def test_write_request_simple(self):
request = requests.models.Request(url="http://httpbin.org/get")
srequest = requestions.write_request(request, return_string=False)
self.assertIn("method", srequest.keys())
self.assertIn("headers", srequest.keys())
self.assertIn("params", srequest.keys())
self.assertIn("url", srequest.keys())
def test_write_request_strings(self):
request = requests.models.Request(url="http://httpbin.org/get")
srequest = requestions.write_request(request)
parsed = json.loads(srequest)
self.assertIn("method", parsed.keys())
self.assertIn("headers", parsed.keys())
def test_write_request_headers(self):
given_headers = {"test_header": "test_value"}
request = requests.models.Request(url="http://httpbin.org/get", headers=given_headers)
srequest = requestions.write_request(request, return_string=False)
self.assertIn("headers", srequest.keys())
self.assertEqual(given_headers, srequest["headers"])
def test_write_request_url(self):
given_url = "http://httpbin.org/get"
request = requests.models.Request(url=given_url)
srequest = requestions.write_request(request, return_string=False)
self.assertIn("url", srequest.keys())
self.assertEqual(given_url, srequest["url"])
class TestRequestDeserialization(unittest.TestCase):
def test_read_request_url(self):
given_url = "http://httpbin.org/get"
srequest = json.dumps({"url": given_url})
request = requestions.read_request(srequest)
self.assertEqual(given_url, request.url)
class TestResponseSerialization(unittest.TestCase):
def test_write_response_simple(self):
given_url = "http://httpbin.org/get"
response = requests.models.Response()
response.url = given_url
sresponse = requestions.write_response(response, return_string=False)
self.assertIn("url", sresponse.keys())
self.assertEqual(given_url, sresponse["url"])
class TestResponseDeserialization(unittest.TestCase):
def test_read_response_simple(self):
given_url = "http://httpbin.org/get"
sresponse = json.dumps({"url": given_url})
response = requestions.read_response(sresponse)
self.assertEqual(given_url, response.url)
class TestSerialization(unittest.TestCase):
def test_requests_pedantically(self):
given_url = "http://httpbin.org/get"
given_method = "GET"
given_headers = {"User-Agent": "Charmander/1.0"}
given_cookies = {"__utm_soul_tracking_id": "A941ECF10959101"}
given_request = requests.models.Request(url=given_url, method=given_method, headers=given_headers, cookies=given_cookies)
given_srequest = requestions.write_request(given_request)
request = requestions.read_request(given_srequest)
self.assertEqual(request.url, given_request.url)
self.assertEqual(request.headers, given_headers)
self.assertEqual(request.method, given_method)
self.assertEqual(dict(request.cookies), given_cookies)
def test_responses_pedantically(self):
given_url = "http://httpbin.org/get"
given_headers = {"User-Agent": "Charmander/1.0"}
given_cookies = {"__utm_soul_tracking_id": "A941ECF10959101"}
given_status_code = 200
given_content = "foo123"
given_response = requests.models.Response()
given_response.url = given_url
given_response.headers = given_headers
given_response.cookies = given_cookies
given_response.status_code = given_status_code
given_response._content = given_content
given_sresponse = requestions.write_response(given_response)
response = requestions.read_response(given_sresponse)
self.assertEqual(response.url, given_response.url)
self.assertEqual(response.headers, given_headers)
self.assertEqual(response.status_code, given_status_code)
self.assertEqual(dict(response.cookies), given_cookies)
self.assertEqual(response.content, given_content)
class TestUtilities(unittest.TestCase):
def test_replace_body_with_data(self):
stuff = "mestanolone"
sample = {"body": stuff}
requestions.utils.replace_body_with_data(sample)
self.assertNotIn("body", sample.keys())
self.assertIn("data", sample.keys())
def test_delete_blank_keys_from(self):
blank_keys = ["one", "two", "three", "four"]
sample = {"body": "stuff",}
sample.update(dict([(key, None) for key in blank_keys]))
requestions.utils.delete_blank_keys_from(sample)
self.assertIn("body", sample.keys())
for key in blank_keys:
self.assertNotIn(key, sample.keys())
def test_convert_to_dict_basic(self):
basic_input = {}
output = requestions.utils.convert_to_dict(basic_input)
self.assertEqual(basic_input, output)
def test_convert_to_dict_with_insensitive(self):
basic_input = {
"key": "value",
"headers": requests.structures.CaseInsensitiveDict(data={"key": "value"}),
}
output = requestions.utils.convert_to_dict(basic_input)
self.assertTrue("key" in output)
self.assertTrue("headers" in output)
self.assertTrue("key" in output["headers"])
self.assertNotEqual(type(output["headers"]), requests.structures.CaseInsensitiveDict)
class TestHttpetrifiedDecorator(unittest.TestCase):
@httpetrified("tests/data/jsonip-response.json")
def test_basic_decorator(self):
response = requests.get("http://jsonip.com/")
try:
jsonstuff = response.json()
except Exception as exception:
jsonstuff = response.json
self.assertEqual(jsonstuff["ip"], "66.68.190.37")
if __name__ == "__main__":
unittest.main()