-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_tesseract.py
206 lines (151 loc) · 6.08 KB
/
test_tesseract.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import pytest
from api import API
from middleware import Middleware
FILE_DIR = "css"
FILE_NAME = "main.css"
FILE_CONTENTS = "body {background-color: red}"
# helpers
def _create_static(static_dir):
asset = static_dir.mkdir(FILE_DIR).join(FILE_NAME)
asset.write(FILE_CONTENTS)
return asset
def test_basic_routing_addition(api):
@api.route("/home")
def home(req, resp):
resp.text = "YOLO"
def test_route_overlap_throws_exception(api):
@api.route("/home")
def home(req, resp):
resp.text = "YOLO"
with pytest.raises(AssertionError):
@api.route("/home")
def home2(req, resp):
resp.text = "YOLO"
def test_tesseract_test_client_can_send_requests(api, client):
RESPONSE_TEXT = "THIS IS COOL"
@api.route("/hey")
def cool(req, resp):
resp.text = RESPONSE_TEXT
assert client.get("http://testserver/hey").text == RESPONSE_TEXT
def test_parameterized_route(api, client):
@api.route("/{name}")
def hello(req, resp, name):
resp.text = f"hey {name}"
assert client.get("http://testserver/pankaj").text == "hey pankaj"
assert client.get("http://testserver/kumar").text == "hey kumar"
def test_default_404_response(client):
response = client.get("http://testserver/doesnotexist")
assert response.status_code == 404
assert response.text == "Not found."
def test_class_based_handler_get(api, client):
response_text = "this is a get request"
@api.route("/book")
class BooksResource:
def get(self, req, resp):
resp.text = response_text
assert client.get("http://testserver/book").text == response_text
def test_class_based_handler_post(api, client):
response_text = "this is a post request"
@api.route("/book")
class BooksResource:
def post(self, req, resp):
resp.text = response_text
assert client.post("http://testserver/book").text == response_text
def test_class_based_handler_not_allowed_method(api, client):
@api.route("/book")
class BooksResource:
def post(self, req, resp):
resp.text = "yolo"
with pytest.raises(AttributeError):
client.get("http://testserver/book")
def test_alternative_django_based_route(api, client):
response_text = "Alternative way to add a route"
def home(req, resp):
resp.text = response_text
api.add_route("/alternative", home)
assert client.get("http://testserver/alternative").text == response_text
def test_template(api, client):
@api.route("/html")
def html_handler(req, resp):
resp.body = api.template("index.html", context={"title": "Some Title", "name": "Some Name"}).encode()
response = client.get("http://testserver/html")
assert "text/html" in response.headers["Content-Type"]
assert "Some Title" in response.text
assert "Some Name" in response.text
def test_custom_exception_handler(api, client):
def on_exception(req, resp, exc):
resp.text = "AttributeErrorHappened"
api.add_exception_handler(on_exception)
@api.route("/")
def index(req, resp):
raise AttributeError()
response = client.get("http://testserver/")
assert response.text == "AttributeErrorHappened"
def test_404_is_returned_for_nonexistent_static_file(client):
assert client.get(f"http://testserver/main.css)").status_code == 404
def test_assets_are_served(tmpdir_factory):
static_dir = tmpdir_factory.mktemp("static")
_create_static(static_dir)
api = API(static_dir=str(static_dir))
client = api.test_session()
response = client.get(f"http://testserver/static/{FILE_DIR}/{FILE_NAME}")
assert response.status_code == 200
assert response.text == FILE_CONTENTS
def test_middleware_methods_are_called(api, client):
process_request_called = False
process_response_called = False
class CallMiddlewareMethods(Middleware):
def __init__(self, app):
super().__init__(app)
def process_request(self, req):
nonlocal process_request_called
process_request_called = True
def process_response(self, req, resp):
nonlocal process_response_called
process_response_called = True
api.add_middleware(CallMiddlewareMethods)
@api.route("/")
def index(req, res):
res.text = "YOLO"
client.get('http://testserver/')
assert process_request_called is True
assert process_response_called is True
def test_allowed_methods_for_function_based_handlers(api, client):
@api.route("/home", allowed_methods=["post"])
def home(req, resp):
resp.text = "Hello"
with pytest.raises(AttributeError):
client.get("http://testserver/home")
assert client.post("http://testserver/home").text == "Hello"
def test_json_response_helper(api, client):
@api.route("/json")
def json_handler(req, resp):
resp.json = {"name": "tesseract"}
response = client.get("http://testserver/json")
json_body = response.json()
assert response.headers["Content-Type"] == "application/json"
assert json_body["name"] == "tesseract"
def test_html_response_helper(api, client):
@api.route("/html")
def html_handler(req, resp):
resp.html = api.template("index.html", context={"title": "Best Title", "name": "Best Name"})
response = client.get("http://testserver/html")
assert "text/html" in response.headers["Content-Type"]
assert "Best Title" in response.text
assert "Best Name" in response.text
def test_text_response_helper(api, client):
response_text = "Just Plain Text"
@api.route("/text")
def text_handler(req, resp):
resp.text = response_text
response = client.get("http://testserver/text")
assert "text/plain" in response.headers["Content-Type"]
assert response.text == response_text
def test_manually_setting_body(api, client):
@api.route("/body")
def text_handler(req, resp):
resp.body = b"Byte Body"
resp.content_type = "text/plain"
response = client.get("http://testserver/body")
assert "text/plain" in response.headers["Content-Type"]
assert response.text == "Byte Body"