-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_usage.py
More file actions
148 lines (130 loc) · 4.19 KB
/
Copy pathexample_usage.py
File metadata and controls
148 lines (130 loc) · 4.19 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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
使用示例
演示如何使用HTTP客户端和服务端
"""
from client import HTTPClient
import time
import json
def print_request_response(case_id, method, path, headers, json_data, response):
"""打印完整的请求和响应信息"""
print("=" * 80)
print("REQUEST")
print("=" * 80)
print(f"Method: {method.upper()}")
print(f"URL: http://localhost:32941{path}")
print(f"\nRequest Headers:")
request_headers = headers.copy() if headers else {}
request_headers['X-Case-ID'] = case_id
for key, value in request_headers.items():
print(f" {key}: {value}")
if json_data:
print(f"\nRequest Body:")
print(json.dumps(json_data, indent=2, ensure_ascii=False))
else:
print(f"\nRequest Body: (empty)")
print("\n" + "=" * 80)
print("RESPONSE")
print("=" * 80)
print(f"Status Code: {response.status_code}")
print(f"\nResponse Headers:")
for key, value in response.headers.items():
print(f" {key}: {value}")
print(f"\nResponse Body:")
try:
response_json = response.json()
print(json.dumps(response_json, indent=2, ensure_ascii=False))
except (ValueError, json.JSONDecodeError):
print(response.text)
print("=" * 80)
def example_usage():
"""示例用法"""
# 创建客户端
client = HTTPClient(base_url='http://localhost:32941')
print("=" * 60)
print("HTTP客户端使用示例")
print("=" * 60)
# 示例1: 获取用户列表
print("\n[示例1] 获取用户列表 (case1)")
print("-" * 60)
try:
headers = {'Accept': 'application/json'}
response = client.send_request(
case_id='case1',
method='GET',
path='/api/users',
headers=headers
)
print_request_response('case1', 'GET', '/api/users', headers, None, response)
except Exception as e:
print(f"错误: {e}")
# 示例2: 创建用户
print("\n[示例2] 创建用户 (case2)")
print("-" * 60)
try:
headers = {'Content-Type': 'application/json'}
json_data = {
'name': 'Charlie',
'email': 'charlie@example.com'
}
response = client.send_request(
case_id='case2',
method='POST',
path='/api/users',
headers=headers,
json_data=json_data
)
print_request_response('case2', 'POST', '/api/users', headers, json_data, response)
except Exception as e:
print(f"错误: {e}")
# 示例3: 获取用户详情
print("\n[示例3] 获取用户详情 (case3)")
print("-" * 60)
try:
headers = {'Accept': 'application/json'}
response = client.send_request(
case_id='case3',
method='GET',
path='/api/users/1',
headers=headers
)
print_request_response('case3', 'GET', '/api/users/1', headers, None, response)
except Exception as e:
print(f"错误: {e}")
# 示例4: 用户不存在
print("\n[示例4] 用户不存在 (case4)")
print("-" * 60)
try:
headers = {'Accept': 'application/json'}
response = client.send_request(
case_id='case4',
method='GET',
path='/api/users/999',
headers=headers
)
print_request_response('case4', 'GET', '/api/users/999', headers, None, response)
except Exception as e:
print(f"错误: {e}")
# 示例5: 文本响应
print("\n[示例5] 文本响应 (case5)")
print("-" * 60)
try:
headers = {}
response = client.send_request(
case_id='case5',
method='GET',
path='/api/status',
headers=headers
)
print_request_response('case5', 'GET', '/api/status', headers, None, response)
except Exception as e:
print(f"错误: {e}")
print("\n" + "=" * 60)
print("示例完成!")
print("=" * 60)
if __name__ == '__main__':
print("\n注意: 请确保服务端已启动 (python server.py)")
print("等待3秒后开始测试...\n")
time.sleep(3)
example_usage()