| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- #!/bin/bash
- # Test script for Gemini File API endpoints
- # This script tests all four file operations: upload, get, list, and delete
- set -e
- # Configuration
- BASE_URL="${BASE_URL:-http://localhost:3000}"
- API_TOKEN="${API_TOKEN:-your-api-token-here}"
- # Colors for output
- GREEN='\033[0;32m'
- RED='\033[0;31m'
- YELLOW='\033[1;33m'
- NC='\033[0m' # No Color
- # Helper functions
- print_success() {
- echo -e "${GREEN}✓ $1${NC}"
- }
- print_error() {
- echo -e "${RED}✗ $1${NC}"
- }
- print_info() {
- echo -e "${YELLOW}ℹ $1${NC}"
- }
- # Check if API token is set
- if [ "$API_TOKEN" = "your-api-token-here" ]; then
- print_error "Please set API_TOKEN environment variable"
- echo "Usage: API_TOKEN=your-token BASE_URL=http://localhost:3000 ./test_gemini_file_api.sh"
- exit 1
- fi
- echo "=========================================="
- echo "Testing Gemini File API Endpoints"
- echo "=========================================="
- echo ""
- # Create a test file
- TEST_FILE="/tmp/test_gemini_upload.txt"
- echo "This is a test file for Gemini File API upload" > "$TEST_FILE"
- print_info "Created test file: $TEST_FILE"
- echo ""
- # Test 1: Upload File
- echo "Test 1: Upload File"
- echo "--------------------"
- print_info "Uploading file to $BASE_URL/upload/v1beta/files"
- UPLOAD_RESPONSE=$(curl -s -w "\n%{http_code}" -X POST "$BASE_URL/upload/v1beta/files" \
- -H "Authorization: Bearer $API_TOKEN" \
- -F "file=@$TEST_FILE" \
- -F "display_name=Test Document")
- HTTP_CODE=$(echo "$UPLOAD_RESPONSE" | tail -n1)
- RESPONSE_BODY=$(echo "$UPLOAD_RESPONSE" | sed '$d')
- if [ "$HTTP_CODE" = "200" ]; then
- print_success "File uploaded successfully (HTTP $HTTP_CODE)"
- Response: $RESPONSE_BODY"
- # Extract file name from response
- FILE_NAME=$(echo "$RESPONSE_BODY" | grep -o '"name":"[^"]*"' | cut -d'"' -f4)
- if [ -n "$FILE_NAME" ]; then
- print_info "File name: $FILE_NAME"
- fi
- else
- print_error "File upload failed (HTTP $HTTP_CODE)"
- echo "Response: $RESPONSE_BODY"
- exit 1
- fi
- echo ""
- # Test 2: List Files
- echo "Test 2: List Files"
- echo "------------------"
- print_info "Listing files from $BASE_URL/v1beta/files"
- LIST_RESPONSE=$(curl -s -w "\n%{http_code}" -X GET "$BASE_URL/v1beta/files" \
- -H "Authorization: Bearer $API_TOKEN")
- HTTP_CODE=$(echo "$LIST_RESPONSE" | tail -n1)
- RESPONSE_BODY=$(echo "$LIST_RESPONSE" | sed '$d')
- if [ "$HTTP_CODE" = "200" ]; then
- print_success "Files listed successfully (HTTP $HTTP_CODE)"
- echo "Response: $RESPONSE_BODY"
- else
- print_error "File listing failed (HTTP $HTTP_CODE)"
- echo "Response: $RESPONSE_BODY"
- fi
- echo ""
- # Test 3: Get File Metadata (if we have a file name)
- if [ -n "$FILE_NAME" ]; then
- echo "Test 3: Get File Metadata"
- echo "-------------------------"
- print_info "Getting metadata for $FILE_NAME"
- GET_RESPONSE=$(curl -s -w "\n%{http_code}" -X GET "$BASE_URL/v1beta/$FILE_NAME" \
- -H "Authorization: Bearer $API_TOKEN")
- HTTP_CODE=$(echo "$GET_RESPONSE" | tail -n1)
- RESPONSE_BODY=$(echo "$GET_RESPONSE" | sed '$d')
- if [ "$HTTP_CODE" = "200" ]; then
- print_success "File metadata retrieved successfully (HTTP $HTTP_CODE)"
- echo "Response: $RESPONSE_BODY"
- else
- print_error "File metadata retrieval failed (HTTP $HTTP_CODE)"
- echo "Response: $RESPONSE_BODY"
- fi
- echo ""
- # Test 4: Delete File
- echo "Test 4: Delete File"
- echo "-------------------"
- print_info "Deleting file $FILE_NAME"
- DELETE_RESPONSE=$(curl -s -w "\n%{http_code}" -X DELETE "$BASE_URL/v1beta/$FILE_NAME" \
- -H "Authorization: Bearer $API_TOKEN")
- HTTP_CODE=$(echo "$DELETE_RESPONSE" | tail -n1)
- RESPONSE_BODY=$(echo "$DELETE_RESPONSE" | sed '$d')
- if [ "$HTTP_CODE" = "200" ] || [ "$HTTP_CODE" = "204" ]; then
- print_success "File deleted successfully (HTTP $HTTP_CODE)"
- echo "Response: $RESPONSE_BODY"
- else
- print_error "File deletion failed (HTTP $HTTP_CODE)"
- echo "Response: $RESPONSE_BODY"
- fi
- echo ""
- fi
- # Cleanup
- rm -f "$TEST_FILE"
- print_info "Cleaned up test file"
- echo ""
- echo "=========================================="
- echo "Test Summary"
- echo "=========================================="
- print_success "All tests completed!"
- echo ""
- echo "Note: If you see authentication errors, make sure:"
- echo " 1. The server is running"
- echo " 2. Your API token is valid"
- echo " 3. The token has access to a Gemini channel"
- echo " 4. The Gemini API key is properly configured in the channel"
|