forked from CJackHwang/AIstudioProxyAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.sh
More file actions
executable file
·159 lines (131 loc) · 3.98 KB
/
test_api.sh
File metadata and controls
executable file
·159 lines (131 loc) · 3.98 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
148
149
150
151
152
153
154
155
156
157
158
159
#!/bin/bash
# AIstudioProxyAPI Test Script
# Quick test script to verify the API is working correctly
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
SERVER_PORT=${1:-2048}
BASE_URL="http://127.0.0.1:$SERVER_PORT"
print_header() {
echo -e "${BLUE}================================${NC}"
echo -e "${BLUE} AI Studio Proxy API Tests${NC}"
echo -e "${BLUE}================================${NC}"
echo "Testing API at: $BASE_URL"
echo ""
}
print_test() {
echo -e "${YELLOW}[TEST]${NC} $1"
}
print_success() {
echo -e "${GREEN}[✅ PASS]${NC} $1"
}
print_error() {
echo -e "${RED}[❌ FAIL]${NC} $1"
}
# Test function
test_endpoint() {
local endpoint="$1"
local description="$2"
local expected_status="${3:-200}"
print_test "Testing $description..."
local response=$(curl -s -w "%{http_code}" "$BASE_URL$endpoint" -o /tmp/api_response.json)
local status_code="${response: -3}"
if [ "$status_code" = "$expected_status" ]; then
print_success "$description (Status: $status_code)"
return 0
else
print_error "$description (Expected: $expected_status, Got: $status_code)"
return 1
fi
}
# Test chat completion
test_chat_completion() {
print_test "Testing chat completion (non-streaming)..."
local response=$(curl -s -w "%{http_code}" -X POST "$BASE_URL/v1/chat/completions" \
-H 'Content-Type: application/json' \
-d '{
"model": "gemini-2.5-pro-preview-06-05",
"messages": [{"role": "user", "content": "Say hello in one word"}],
"stream": false,
"max_output_tokens": 10
}' \
-o /tmp/chat_response.json \
--max-time 30)
local status_code="${response: -3}"
if [ "$status_code" = "200" ]; then
# Check if response contains expected fields
if jq -e '.choices[0].message.content' /tmp/chat_response.json > /dev/null 2>&1; then
local content=$(jq -r '.choices[0].message.content' /tmp/chat_response.json)
print_success "Chat completion (Response: \"$content\")"
return 0
else
print_error "Chat completion (Invalid response format)"
return 1
fi
else
print_error "Chat completion (Status: $status_code)"
return 1
fi
}
# Main test suite
main() {
print_header
local failed_tests=0
local total_tests=0
# Test 1: Health check
((total_tests++))
if ! test_endpoint "/health" "Health check"; then
((failed_tests++))
fi
# Test 2: API info
((total_tests++))
if ! test_endpoint "/api/info" "API info"; then
((failed_tests++))
fi
# Test 3: Models list
((total_tests++))
if ! test_endpoint "/v1/models" "Models list"; then
((failed_tests++))
fi
# Test 4: Chat completion
((total_tests++))
if ! test_chat_completion; then
((failed_tests++))
fi
# Summary
echo ""
echo -e "${BLUE}================================${NC}"
echo -e "${BLUE} Test Results Summary${NC}"
echo -e "${BLUE}================================${NC}"
local passed_tests=$((total_tests - failed_tests))
echo "Total tests: $total_tests"
echo -e "Passed: ${GREEN}$passed_tests${NC}"
echo -e "Failed: ${RED}$failed_tests${NC}"
if [ $failed_tests -eq 0 ]; then
echo ""
print_success "All tests passed! 🎉"
echo ""
echo "API is ready for use:"
echo " Base URL: $BASE_URL/v1"
echo " Health: $BASE_URL/health"
echo " Models: $BASE_URL/v1/models"
return 0
else
echo ""
print_error "Some tests failed. Check the server logs."
return 1
fi
}
# Cleanup function
cleanup() {
rm -f /tmp/api_response.json /tmp/chat_response.json
}
# Set trap for cleanup
trap cleanup EXIT
# Run main function
main "$@"