-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_server.py
More file actions
57 lines (47 loc) · 1.47 KB
/
test_server.py
File metadata and controls
57 lines (47 loc) · 1.47 KB
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
import subprocess
import time
import requests
import pytest
PORT = 7777
BASE_URL = f"http://localhost:{PORT}"
@pytest.fixture(scope="module", autouse=True)
def server():
# Start the pastebin server
proc = subprocess.Popen(["node", "server.js"])
# Wait for the server to start
started = False
for _ in range(20):
try:
r = requests.get(BASE_URL, timeout=1)
if r.status_code == 200:
started = True
break
except requests.exceptions.RequestException:
time.sleep(0.5)
if not started:
proc.terminate()
pytest.fail("Server did not start in time")
yield
proc.terminate()
proc.wait()
def test_homepage():
r = requests.get(BASE_URL)
assert r.status_code == 200
assert "<html" in r.text.lower()
def test_add_and_retrieve_document():
# POST to /documents
payload = "This is a secure test document for pytest."
r = requests.post(f"{BASE_URL}/documents", data=payload)
assert r.status_code == 200
data = r.json()
assert "key" in data, "Response should have a key"
key = data["key"]
# GET /raw/{key}
r2 = requests.get(f"{BASE_URL}/raw/{key}")
assert r2.status_code == 200
assert r2.text == payload
def test_static_about_document():
# Check that static document about exists
r = requests.get(f"{BASE_URL}/raw/about")
assert r.status_code == 200
assert len(r.text) > 0