-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtests.py
103 lines (82 loc) · 3.45 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
import json
import requests
import unittest
class TestApp(unittest.TestCase):
url = 'http://localhost:8080/'
def test_json_request(self):
data = {}
with open('testcases/sample.html', encoding="utf-8") as html:
data["contents"] = html.read()
headers = {
'Content-Type': 'application/json',
}
response = requests.post(url=self.url, data=json.dumps(data), headers=headers)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers['content-type'], 'application/pdf')
def test_with_null_json_request(self):
data = {
}
headers = {
'Content-Type': 'application/json',
}
response = requests.post(url=self.url, data=json.dumps(data), headers=headers)
self.assertEqual(response.status_code, 400)
def test_with_non_html_input(self):
data = {
'contents': 'random non html string... test test test',
}
headers = {
'Content-Type': 'application/json',
}
response = requests.post(url=self.url, data=json.dumps(data), headers=headers)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers['content-type'], 'application/pdf')
def test_file_input(self):
file_contents = ""
with open('testcases/sample.html', 'rb') as f:
file_contents = f.read()
response = requests.post(url=self.url, files={'file': file_contents})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers['content-type'], 'application/pdf')
def test_neither_json_not_file_input(self):
data = {
}
response = requests.post(url=self.url, data=data)
self.assertEqual(response.status_code, 400)
def test_blank_file_input(self):
# blank pdf will be the output
file_contents = ""
with open('testcases/blank.html', 'rb') as f:
file_contents = f.read()
response = requests.post(url=self.url, files={'file': file_contents})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers['content-type'], 'application/pdf')
def test_content_with_blank_html(self):
data = {
'contents': '<html lang="en"><head><meta charset="UTF-8"><title>Title</title></head><body></body></html>',
}
headers = {
'Content-Type': 'application/json',
}
response = requests.post(url=self.url, data=json.dumps(data), headers=headers)
self.assertEqual(response.status_code, 200)
def test_html_with_unicode_char(self):
data = {}
with open('testcases/unicode.html', encoding="utf-8") as f:
data["contents"] = f.read()
headers = {'Content-Type': 'application/json'}
response = requests.post(url=self.url, data=json.dumps(data), headers=headers)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers['content-type'], 'application/pdf')
def test_get_endpoint(self):
response = requests.get(
url='http://localhost:8080/ping',
)
self.assertEqual(response.status_code, 200)
def test_get_endpoint_with_invalid_url(self):
response = requests.get(
url='http://localhost:8080/random',
)
self.assertEqual(response.status_code, 405)
if __name__ == '__main__':
unittest.main()