conveyor-belt-new/test/test-controller-api.py
2025-03-27 22:29:43 +08:00

51 lines
1.7 KiB
Python

import requests
import json
# 设置服务器地址和端口
BASE_URL = "http://127.0.0.1:1"
def test_process_get(conveyor_belt_id):
"""
测试 /api/processGet 接口
"""
url = f"{BASE_URL}/api/processGet"
params = {"id": conveyor_belt_id}
print(f"Testing GET {url} with params: {params}")
try:
response = requests.get(url, params=params)
print(f"Response Status Code: {response.status_code}")
print(f"Response Body: {response.json()}")
except Exception as e:
print(f"Error during GET request: {e}")
def test_process_post(conveyor_belt_id, action, speed):
"""
测试 /api/processPost 接口
"""
url = f"{BASE_URL}/api/processPost"
headers = {"Content-Type": "application/json"}
payload = {
"id": conveyor_belt_id,
"action": action,
"speed": speed
}
print(f"Testing POST {url} with payload: {json.dumps(payload, indent=4)}")
try:
response = requests.post(url, headers=headers, json=payload)
print(f"Response Status Code: {response.status_code}")
print(f"Response Body: {response.json()}")
except Exception as e:
print(f"Error during POST request: {e}")
if __name__ == "__main__":
# 测试 /api/processGet
print("=== Testing /api/processGet ===")
test_process_get(conveyor_belt_id=108) # 替换为实际的传送带 ID
# 测试 /api/processPost
print("\n=== Testing /api/processPost ===")
test_process_post(conveyor_belt_id=108, action=1, speed=100) # 启动传送带
test_process_post(conveyor_belt_id=108, action=0, speed=0) # 停止传送带
test_process_post(conveyor_belt_id=108, action=2, speed=50) # 无效操作测试