-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
175 lines (136 loc) · 4.72 KB
/
tests.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
import requests
# from send_request import send_request
import config
import sys
sys.path.append("../")
sys.path.append("../lib")
def get_base_url() -> str:
"""
Return the base URL of the server
"""
return "http://localhost:{}/".format(config.SERVER_PORT)
def test_get() -> str:
req = requests.get(get_base_url())
if req.status_code != 200:
return "Bad status code."
if req.text != "hello world":
return "Bad content."
if req.headers["Content-Length"] != "11":
return "Bad Content-Length"
if req.headers["Content-Type"] != "text/html":
return "Bad Content-Length"
return ""
def test_get_dir_index() -> str:
req = requests.get(get_base_url() + "a")
if req.status_code != 200:
return "Bad status code."
if req.text != "hello":
return "Bad content."
return ""
def test_get_dir_not_allowed() -> str:
req = requests.get(get_base_url() + "virtual/a/")
# if (req.status_code != 403):
if req.status_code != 404:
return "Bad status code."
return ""
def test_wrong_post() -> str:
req = requests.post(get_base_url())
if req.status_code != 405:
return "Bad status code."
return ""
def test_too_big_request() -> str:
payload = "a" * 1025
req = requests.post(get_base_url() + "post/test", data=payload)
if req.status_code != 413:
return "Bad status code: {}, expected: {}".format(req.status_code, 413)
return ""
def test_custom_404() -> str:
req = requests.get(get_base_url() + "b")
if req.status_code != 404:
return "Bad status code"
if req.text != "custom404":
return "Bad error page"
return ""
def test_404() -> str:
req = requests.get(
get_base_url() + "doidjodoeijdosejfoejfseoifjseiofjsejfsejfesjfseofsejiseofj"
)
if req.status_code != 404:
return "Bad status code"
return ""
def test_autoindex() -> str:
req = requests.get(get_base_url() + "index/a/")
if req.status_code != 200:
return "Bad status code"
return ""
def test_two_puts() -> str:
req = requests.put(get_base_url() + "post/c", data="1")
if req.status_code != 201:
return "Bad status code on first creation: {}, expected: {}".format(
str(req.status_code), "201"
)
req = requests.put(get_base_url() + "post/c", data="2")
if req.status_code != 204:
return "Bad status code on update: {}, expected: {}".format(
str(req.status_code), "204"
)
return ""
def test_multiple_ports() -> str:
req = requests.get(get_base_url())
if req.text != "hello world":
return "Bad content on first port."
req = requests.get("http://localhost:{}/".format(config.SERVER_PORT2))
if req.text != "second file":
return "Bad content on second port."
return ""
def test_different_index() -> str:
req = requests.get(get_base_url())
if req.text != "hello world":
return "Bad first index."
req = requests.get(get_base_url() + "2/")
if req.text != "second file":
return "Bad second index."
return ""
def test_head() -> str:
req = requests.head(get_base_url())
if req.status_code != 200:
return "Bad status code: {}, expected: {}".format(str(req.status_code), "200")
if len(req.text) > 0:
return "Head returned some content."
return ""
def test_multiple_get() -> str:
for i in range(100):
req = requests.get(get_base_url())
if req.status_code != 200 or req.text != "hello world":
return "Bad request at {}th iteration.".format(i + 1)
return ""
def test_delete() -> str:
req = requests.put(get_base_url() + "post/test", data="test put and delete")
if req.status_code != 201:
return "Bad status on put request. {}".format(req.status_code)
location = req.headers["Location"]
if location != "/post/tmp/test":
return "Bad Location header: {}, expected: {}".format(
location, "/post/tmp/test"
)
print(location)
req = requests.get(get_base_url() + "post/tmp/test")
if req.status_code != 200 and req.text != "test put and delete":
return "Bad put request. {}".format(req.status_code)
req = requests.delete(get_base_url() + "post/tmp/test")
if req.status_code != 202 and req.status_code != 200:
return "Bad status code for DELETE."
req = requests.get(get_base_url() + location)
if req.status_code != 404:
return "File still exists after DELETE."
return ""
def test_delete_no_file() -> str:
req = requests.delete(get_base_url() + "post/gone")
if req.status_code != 404:
return "Bad status code for DELETE."
return ""
#######################
# CGI
# PUT avec chunk
# Multiple GET
# CONNECT