#!/bin/bash
# Test Bearer Token Authentication with Caching
# Colors
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
BASE_URL="http://localhost:8002"
# Replace with your actual token from Laravel login
TOKEN="11836|UAc9YiEKc9zO9MvNHKQqY9WwdkxW7qQyw3mqyNK5"
echo -e "${BLUE}๐งช Testing Bearer Token Authentication${NC}"
echo "========================================"
echo ""
# Test 1: Health Check
echo -e "${YELLOW}1๏ธโฃ Health Check${NC}"
response=$(curl -s "$BASE_URL/health")
if echo "$response" | grep -q "healthy"; then
echo -e "${GREEN}โ Server is healthy${NC}"
else
echo -e "${RED}โ Server health check failed${NC}"
echo "Response: $response"
fi
echo ""
# Test 2: Cache Stats (before any requests)
echo -e "${YELLOW}2๏ธโฃ Initial Cache Stats${NC}"
curl -s "$BASE_URL/cache/stats" | python3 -m json.tool
echo ""
# Test 3: First chat request (should be cache MISS)
echo -e "${YELLOW}3๏ธโฃ First Chat Request (Cache MISS expected)${NC}"
echo "Sending: 'What can I do?'"
start_time=$(date +%s%3N)
response=$(curl -s -X POST "$BASE_URL/chat" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d '{"message": "What can I do?"}')
end_time=$(date +%s%3N)
duration=$((end_time - start_time))
if echo "$response" | grep -q '"success": true'; then
echo -e "${GREEN}โ Chat request successful${NC}"
echo "Duration: ${duration}ms"
echo "$response" | python3 -m json.tool | head -n 20
else
echo -e "${RED}โ Chat request failed${NC}"
echo "$response" | python3 -m json.tool
fi
echo ""
# Test 4: Cache stats after first request
echo -e "${YELLOW}4๏ธโฃ Cache Stats After First Request${NC}"
echo "Expected: 1 entry, 0 hits, 1 miss"
curl -s "$BASE_URL/cache/stats" | python3 -m json.tool
echo ""
# Test 5: Second chat request (should be cache HIT)
echo -e "${YELLOW}5๏ธโฃ Second Chat Request (Cache HIT expected)${NC}"
echo "Sending: 'Show my contracts'"
start_time=$(date +%s%3N)
response=$(curl -s -X POST "$BASE_URL/chat" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d '{"message": "Show my contracts"}')
end_time=$(date +%s%3N)
duration=$((end_time - start_time))
if echo "$response" | grep -q '"success": true'; then
echo -e "${GREEN}โ Chat request successful (should be faster!)${NC}"
echo "Duration: ${duration}ms"
echo "$response" | python3 -m json.tool | head -n 20
else
echo -e "${RED}โ Chat request failed${NC}"
echo "$response" | python3 -m json.tool
fi
echo ""
# Test 6: Cache stats after second request
echo -e "${YELLOW}6๏ธโฃ Cache Stats After Second Request${NC}"
echo "Expected: 1 entry, 1 hit, 1 miss (50% hit rate)"
curl -s "$BASE_URL/cache/stats" | python3 -m json.tool
echo ""
# Test 7: Third chat request (cache HIT again)
echo -e "${YELLOW}7๏ธโฃ Third Chat Request (Cache HIT expected)${NC}"
echo "Sending: 'List my organizations'"
start_time=$(date +%s%3N)
response=$(curl -s -X POST "$BASE_URL/chat" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d '{"message": "List my organizations"}')
end_time=$(date +%s%3N)
duration=$((end_time - start_time))
if echo "$response" | grep -q '"success": true'; then
echo -e "${GREEN}โ Chat request successful${NC}"
echo "Duration: ${duration}ms"
else
echo -e "${RED}โ Chat request failed${NC}"
echo "$response" | python3 -m json.tool
fi
echo ""
# Test 8: Final cache stats
echo -e "${YELLOW}8๏ธโฃ Final Cache Stats${NC}"
echo "Expected: 1 entry, 2 hits, 1 miss (66.7% hit rate)"
curl -s "$BASE_URL/cache/stats" | python3 -m json.tool
echo ""
# Test 9: Missing Authorization header
echo -e "${YELLOW}9๏ธโฃ Test Missing Authorization (Should Fail)${NC}"
response=$(curl -s -X POST "$BASE_URL/chat" \
-H "Content-Type: application/json" \
-d '{"message": "test"}')
if echo "$response" | grep -q "Missing or invalid Authorization"; then
echo -e "${GREEN}โ Correctly rejected request without token${NC}"
else
echo -e "${RED}โ Should have rejected request${NC}"
fi
echo ""
# Test 10: Invalid token
echo -e "${YELLOW}๐ Test Invalid Token (Should Fail)${NC}"
response=$(curl -s -X POST "$BASE_URL/chat" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer invalid_token_123" \
-d '{"message": "test"}')
if echo "$response" | grep -q "Invalid or expired token"; then
echo -e "${GREEN}โ Correctly rejected invalid token${NC}"
else
echo -e "${RED}โ Should have rejected invalid token${NC}"
fi
echo ""
echo "========================================"
echo -e "${BLUE}โ
Bearer Token Authentication Tests Complete!${NC}"
echo ""
echo -e "${YELLOW}๐ก Key Observations:${NC}"
echo " - First request: Slower (calls Laravel API)"
echo " - Subsequent requests: Faster (uses cache)"
echo " - Cache hit rate should improve with more requests"
echo " - Invalid tokens are properly rejected"
echo ""
echo -e "${YELLOW}๐ To monitor cache performance:${NC}"
echo " curl http://localhost:8002/cache/stats"
echo ""
echo -e "${YELLOW}๐ง To use in your React app:${NC}"
echo " See BEARER_TOKEN_AUTH.md for integration examples"