-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_test.py
43 lines (37 loc) · 1.35 KB
/
test_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
import unittest
import json
from http.server import HTTPServer
from threading import Thread
import http.client
from test import RequestHandler
class TestServer(unittest.TestCase):
@classmethod
def setUpClass(cls):
# Start the server in a separate thread
cls.server = HTTPServer(('localhost', 8080), RequestHandler)
cls.thread = Thread(target=cls.server.serve_forever)
cls.thread.start()
@classmethod
def tearDownClass(cls):
# Shutdown the server
cls.server.shutdown()
cls.thread.join()
def test_post_prompt(self):
# Create a connection to the server
conn = http.client.HTTPConnection('localhost', 8080)
# Define the prompt
prompt = "Tell me a fun fact about space."
# Create the JSON payload
payload = json.dumps({'prompt': prompt})
# Send a POST request
conn.request('POST', '/', payload, headers={'Content-Type': 'application/json'})
# Get the response
response = conn.getresponse()
# Read the response content
response_content = response.read().decode('utf-8')
# Check if the response status is 200
self.assertEqual(response.status, 200)
# Check if the response content is not empty
self.assertTrue(len(response_content) > 0)
if __name__ == '__main__':
unittest.main()