-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_api.py
78 lines (51 loc) · 1.91 KB
/
test_api.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
import pytest
from api import API
def test_basic_route_adding(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_bumbo_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/matthew").text == "hey matthew"
assert client.get("http://testserver/ashley").text == "hey ashley"
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 BookResource:
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 BookResource:
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 BookResource:
def post(self, req, resp):
resp.text = "yolo"
with pytest.raises(AttributeError):
client.get("http://testserver/book")