|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import re |
| 5 | +import subprocess |
| 6 | +import tempfile |
| 7 | +import time |
| 8 | +import unittest |
| 9 | + |
| 10 | +import requests |
| 11 | +import yldme |
| 12 | + |
| 13 | +# Constants |
| 14 | + |
| 15 | +TEST_PORT = 9999 |
| 16 | +TEST_URL = 'http://localhost:' + str(TEST_PORT) |
| 17 | + |
| 18 | +# Test Cases |
| 19 | + |
| 20 | +class YldMeHandlerTestCase(unittest.TestCase): |
| 21 | + |
| 22 | + @classmethod |
| 23 | + def setUpClass(cls): |
| 24 | + cls.config_dir = tempfile.TemporaryDirectory() |
| 25 | + cls.process = subprocess.Popen([ |
| 26 | + './yldme.py', |
| 27 | + '--port=' + str(TEST_PORT), |
| 28 | + '--config_dir=' + cls.config_dir.name, |
| 29 | + ], stderr=subprocess.DEVNULL) |
| 30 | + time.sleep(1) |
| 31 | + |
| 32 | + @classmethod |
| 33 | + def tearDownClass(cls): |
| 34 | + cls.process.terminate() |
| 35 | + cls.config_dir.cleanup() |
| 36 | + |
| 37 | + def test_get_index(self): |
| 38 | + response = requests.get(TEST_URL) |
| 39 | + self.assertTrue(response.status_code == 200) |
| 40 | + self.assertTrue('/paste' in response.text) |
| 41 | + self.assertTrue('/url' in response.text) |
| 42 | + |
| 43 | + def test_post_url(self, method=requests.post): |
| 44 | + response = method(TEST_URL + '/url', data = 'https://yld.me') |
| 45 | + self.assertTrue(response.status_code == 200) |
| 46 | + |
| 47 | + shorturl = response.text.strip() |
| 48 | + self.assertTrue(re.match('https://yld.me/[a-zA-Z0-9]{1}', shorturl)) |
| 49 | + |
| 50 | + shorturl = TEST_URL + '/' + os.path.basename(shorturl) |
| 51 | + response = requests.get(shorturl, allow_redirects=False) |
| 52 | + self.assertTrue(response.status_code == 302) |
| 53 | + self.assertTrue(response.headers['Location'] == 'https://yld.me') |
| 54 | + |
| 55 | + def test_put_url(self): |
| 56 | + self.test_post_url(requests.put) |
| 57 | + |
| 58 | + def test_post_paste_batch(self, method=requests.post): |
| 59 | + data = 'Sic Mundus Creatus Est' |
| 60 | + response = method(TEST_URL + '/paste', data=data) |
| 61 | + self.assertTrue(response.status_code == 200) |
| 62 | + |
| 63 | + shorturl = response.text.strip() |
| 64 | + self.assertTrue(re.match('https://yld.me/[a-zA-Z0-9]{1}', shorturl)) |
| 65 | + |
| 66 | + shorturl = TEST_URL + '/' + os.path.basename(shorturl) |
| 67 | + response = requests.get(shorturl) |
| 68 | + self.assertTrue(response.status_code == 200) |
| 69 | + for word in data.split(): |
| 70 | + self.assertTrue(word in response.text) |
| 71 | + |
| 72 | + rawurl = TEST_URL + '/raw/' + os.path.basename(shorturl) |
| 73 | + response = requests.get(rawurl) |
| 74 | + self.assertTrue(response.status_code == 200) |
| 75 | + self.assertTrue(response.text == data) |
| 76 | + |
| 77 | + rawurl = TEST_URL + '/raw/' + os.path.basename(shorturl) + '.txt' |
| 78 | + response = requests.get(rawurl) |
| 79 | + self.assertTrue(response.status_code == 200) |
| 80 | + self.assertTrue(response.text == data) |
| 81 | + |
| 82 | + def test_put_paste_url(self): |
| 83 | + self.test_post_paste_batch(requests.put) |
| 84 | + |
| 85 | +# Main Execution |
| 86 | + |
| 87 | +if __name__ == '__main__': |
| 88 | + unittest.main() |
0 commit comments