-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_app.py
214 lines (138 loc) · 5.58 KB
/
test_app.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
207
208
import pytest
from pylord.middleware import Middleware
def test_basic_rout_adding(app):
@app.route("/home")
def home(req, resp):
resp.text = "hi babe"
def test_duplicate_routes_throws_exception(app):
@app.route("/home")
def home(req, resp):
resp.text = "hi babe"
with pytest.raises(AssertionError):
@app.route("/home")
def home2(req, resp):
resp.text = "hi babe2"
def test_requests_can_be_sent_by_client(app, test_client):
@app.route("/home")
def home(req, resp):
resp.text = "hi babe"
response = test_client.get("http://testserver/home")
assert response.text == "hi babe"
def test_parameterized_routes(app, test_client):
@app.route("/hello/{name}")
def generating(request, response, name):
response.text = f"hello {name}"
assert test_client.get("http://testserver/hello/kamoliddin").text == "hello kamoliddin"
assert test_client.get("http://testserver/hello/dagon").text == "hello dagon"
def test_default_response(test_client):
response = test_client.get("http://testserver/kkkkkkkk")
assert response.text == "Not Found"
assert response.status_code == 404
def test_class_based_get(app, test_client):
@app.route("/book")
class Books:
def get(self, req, resp):
resp.text = 'this is get method'
assert test_client.get("http://testserver/book").text == "this is get method"
def test_class_based_post(app, test_client):
@app.route("/book")
class Books:
def post(self, req, resp):
resp.text = 'endpoint to create a book'
assert test_client.post("http://testserver/book").text == "endpoint to create a book"
def test_class_based_not_allowed(app, test_client):
@app.route("/book")
class Books:
def post(self, req, resp):
resp.text = 'endpoint to create a book'
response = test_client.get("http://testserver/book")
assert response.text == "Method not allowed"
assert response.status_code == 405
def test_alternative_rout_adding(app, test_client):
def new_handler(req, resp):
resp.text = "New Handler"
app.add_route("/new_handler", new_handler)
assert test_client.get("http://testserver/new_handler").text == "New Handler"
def test_template_handler(app, test_client):
@app.route("/template")
def template(req, resp):
resp.body = app.template(
"home.html",
context={
"new_title": "Best_title",
"new_body": "Best_body"
}
)
response = test_client.get("http://testserver/template")
assert "Best_body" in response.text
assert "new_title" in response.text
assert "text/html" in response.headers["Content-Type"]
def test_custom_exception_handler(app, test_client):
def on_exception(req, resp, exc):
resp.text = "something bad happened"
app.add_exception_handler(on_exception)
@app.route("/exception")
def exception_throwing_handler(req, resp):
raise AttributeError("some exception")
response = test_client.get("http://testserver/exception")
assert response.text == "something bad happened"
def test_non_existent_static_files(test_client):
assert test_client.get("http://testserver/nonexitent.css").status_code == 404
def test_serving_static_file(test_client):
response = test_client.get("http://testserver/static/test.css")
assert response.text == "body {background-color: lightblue;}"
def test_middleware(app, test_client):
process_request_called = False
process_response_called = False
class SimpleMiddleWare(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
app.add_middleware(SimpleMiddleWare)
@app.route("/home")
def index(req, resp):
resp.text = "from handler"
test_client.get("http://testserver/home")
assert process_request_called is True
assert process_response_called is True
def test_allowed_method_for_function_based_handlers(app, test_client):
@app.route("/home", allowed_methods=["post"])
def home(req, res):
resp.text = "hi this is home page"
resp = test_client.get("http://testserver/home")
assert resp.status_code == 405
assert resp.text == "Method not allowed"
def test_json_response_helper(app, test_client):
@app.route("/json")
def json_handler(req, resp):
resp.json = {"name": "pylord"}
resp = test_client.get("http://testserver/json")
resp_data = resp.json()
assert resp.headers["Content-Type"] == "application/json"
assert resp_data["name"] == "pylord"
def test_text_response_handler(app, test_client):
@app.route("/text")
def text_handler(req, resp):
resp.text = "plain text"
response = test_client.get("http://testserver/text")
assert "text/plain" in response.headers["Content-Type"]
assert response.text == "plain text"
def test_html_helper(app, test_client):
@app.route("/html")
def html_handler(req, resp):
resp.html = app.template(
"home.html",
context={
"new_title": "Best_title",
"new_body": "Best_body"
}
)
response = test_client.get("http://testserver/html")
assert "text/html" in response.headers["Content-Type"]
assert "new_title" in response.text
assert "Best_body" in response.text