-
Notifications
You must be signed in to change notification settings - Fork 6
/
yldme_test.py
executable file
·88 lines (67 loc) · 2.68 KB
/
yldme_test.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
#!/usr/bin/env python3
import os
import re
import subprocess
import tempfile
import time
import unittest
import requests
import yldme
# Constants
TEST_PORT = 9999
TEST_URL = 'http://localhost:' + str(TEST_PORT)
# Test Cases
class YldMeHandlerTestCase(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.config_dir = tempfile.TemporaryDirectory()
cls.process = subprocess.Popen([
'./yldme.py',
'--port=' + str(TEST_PORT),
'--config_dir=' + cls.config_dir.name,
], stderr=subprocess.DEVNULL)
time.sleep(1)
@classmethod
def tearDownClass(cls):
cls.process.terminate()
cls.config_dir.cleanup()
def test_get_index(self):
response = requests.get(TEST_URL)
self.assertTrue(response.status_code == 200)
self.assertTrue('/paste' in response.text)
self.assertTrue('/url' in response.text)
def test_post_url(self, method=requests.post):
response = method(TEST_URL + '/url', data = 'https://yld.me')
self.assertTrue(response.status_code == 200)
shorturl = response.text.strip()
self.assertTrue(re.match('https://yld.me/[a-zA-Z0-9]{1}', shorturl))
shorturl = TEST_URL + '/' + os.path.basename(shorturl)
response = requests.get(shorturl, allow_redirects=False)
self.assertTrue(response.status_code == 302)
self.assertTrue(response.headers['Location'] == 'https://yld.me')
def test_put_url(self):
self.test_post_url(requests.put)
def test_post_paste_batch(self, method=requests.post):
data = 'Sic Mundus Creatus Est'
response = method(TEST_URL + '/paste', data=data)
self.assertTrue(response.status_code == 200)
shorturl = response.text.strip()
self.assertTrue(re.match('https://yld.me/[a-zA-Z0-9]{1}', shorturl))
shorturl = TEST_URL + '/' + os.path.basename(shorturl)
response = requests.get(shorturl)
self.assertTrue(response.status_code == 200)
for word in data.split():
self.assertTrue(word in response.text)
rawurl = TEST_URL + '/raw/' + os.path.basename(shorturl)
response = requests.get(rawurl)
self.assertTrue(response.status_code == 200)
self.assertTrue(response.text == data)
rawurl = TEST_URL + '/raw/' + os.path.basename(shorturl) + '.txt'
response = requests.get(rawurl)
self.assertTrue(response.status_code == 200)
self.assertTrue(response.text == data)
def test_put_paste_url(self):
self.test_post_paste_batch(requests.put)
# Main Execution
if __name__ == '__main__':
unittest.main()