fix: Correct typo in FAILED_TESTS_SUMMARY variable and enhance HTTP status code handling in API tests
This commit is contained in:
@@ -79,7 +79,7 @@ TEST_START_TIME=$(date +%s)
|
|||||||
AUTH_TOKEN=""
|
AUTH_TOKEN=""
|
||||||
SERVER_PID=""
|
SERVER_PID=""
|
||||||
TEST_RESULTS=()
|
TEST_RESULTS=()
|
||||||
FALED_TESTS_SUMMARY=()
|
FAILED_TESTS_SUMMARY=()
|
||||||
PASS_COUNT=0
|
PASS_COUNT=0
|
||||||
FAIL_COUNT=0
|
FAIL_COUNT=0
|
||||||
TEST_TESTIMONIAL_ID=""
|
TEST_TESTIMONIAL_ID=""
|
||||||
@@ -135,10 +135,20 @@ test_api_endpoint() {
|
|||||||
|
|
||||||
local start_req_time=$(date +%s%3N)
|
local start_req_time=$(date +%s%3N)
|
||||||
|
|
||||||
response=$(curl -s -w "\n%{http_code}" -X "$method" "${headers[@]}" -d "$body" "$BASE_URL$endpoint")
|
# Use a more compatible approach for Windows/Git Bash
|
||||||
|
local temp_file=$(mktemp)
|
||||||
|
local status_file=$(mktemp)
|
||||||
|
|
||||||
http_status=$(echo "$response" | tail -n1)
|
# Make single request and capture both body and status using response headers
|
||||||
response_body=$(echo "$response" | sed '$d')
|
curl -s -X "$method" "${headers[@]}" -d "$body" "$BASE_URL$endpoint" \
|
||||||
|
-D "$status_file" -o "$temp_file"
|
||||||
|
|
||||||
|
response_body=$(cat "$temp_file")
|
||||||
|
|
||||||
|
# Extract HTTP status code from headers file
|
||||||
|
http_status=$(head -n 1 "$status_file" | cut -d' ' -f2)
|
||||||
|
|
||||||
|
rm -f "$temp_file" "$status_file"
|
||||||
|
|
||||||
local end_req_time=$(date +%s%3N)
|
local end_req_time=$(date +%s%3N)
|
||||||
local duration=$((end_req_time - start_req_time))
|
local duration=$((end_req_time - start_req_time))
|
||||||
@@ -146,16 +156,21 @@ test_api_endpoint() {
|
|||||||
local status="FAIL"
|
local status="FAIL"
|
||||||
local error_msg=""
|
local error_msg=""
|
||||||
|
|
||||||
if [ "$http_status" -eq "$expected_status" ]; then
|
# Check if http_status is numeric
|
||||||
|
if [[ "$http_status" =~ ^[0-9]+$ ]] && [ "$http_status" -eq "$expected_status" ]; then
|
||||||
status="PASS"
|
status="PASS"
|
||||||
((PASS_COUNT++))
|
((PASS_COUNT++))
|
||||||
write_test_log "SUCCESS" "✓ $test_name - Sukses (Status: $http_status, Waktu: ${duration}ms)"
|
write_test_log "SUCCESS" "✓ $test_name - Sukses (Status: $http_status, Waktu: ${duration}ms)"
|
||||||
else
|
else
|
||||||
status="FAIL"
|
status="FAIL"
|
||||||
((FAIL_COUNT++))
|
((FAIL_COUNT++))
|
||||||
write_test_log "ERROR" " Request Body: $body"
|
write_test_log "ERROR" " Request Body: $body"
|
||||||
write_test_log "ERROR" " Response Body: $response_body"
|
write_test_log "ERROR" " Response Body: $response_body"
|
||||||
|
if [[ ! "$http_status" =~ ^[0-9]+$ ]]; then
|
||||||
|
error_msg="Failed to get valid HTTP status code (got: $http_status)"
|
||||||
|
else
|
||||||
error_msg="Status yang diharapkan $expected_status, tetapi mendapat $http_status."
|
error_msg="Status yang diharapkan $expected_status, tetapi mendapat $http_status."
|
||||||
|
fi
|
||||||
write_test_log "ERROR" "✗ $test_name - Gagal: $error_msg"
|
write_test_log "ERROR" "✗ $test_name - Gagal: $error_msg"
|
||||||
FAILED_TESTS_SUMMARY+=("✗ $test_name - $error_msg")
|
FAILED_TESTS_SUMMARY+=("✗ $test_name - $error_msg")
|
||||||
fi
|
fi
|
||||||
@@ -194,14 +209,24 @@ get_auth_token() {
|
|||||||
local headers=(-H "Content-Type: application/json")
|
local headers=(-H "Content-Type: application/json")
|
||||||
local start_req_time=$(date +%s%3N)
|
local start_req_time=$(date +%s%3N)
|
||||||
|
|
||||||
response=$(curl -s -w "\n%{http_code}" -X "POST" "${headers[@]}" -d "$login_data" "$BASE_URL/v1/auth/login")
|
# Use a more compatible approach for Windows/Git Bash
|
||||||
|
local temp_file=$(mktemp)
|
||||||
|
local status_file=$(mktemp)
|
||||||
|
|
||||||
local http_status=$(echo "$response" | tail -n1)
|
# Make single request and capture both body and status using response headers
|
||||||
local response_body=$(echo "$response" | sed '$d')
|
curl -s -X "POST" "${headers[@]}" -d "$login_data" "$BASE_URL/v1/auth/login" \
|
||||||
|
-D "$status_file" -o "$temp_file"
|
||||||
|
|
||||||
|
local response_body=$(cat "$temp_file")
|
||||||
|
|
||||||
|
# Extract HTTP status code from headers file
|
||||||
|
local http_status=$(head -n 1 "$status_file" | cut -d' ' -f2)
|
||||||
|
|
||||||
|
rm -f "$temp_file" "$status_file"
|
||||||
local end_req_time=$(date +%s%3N)
|
local end_req_time=$(date +%s%3N)
|
||||||
local duration=$((end_req_time - start_req_time))
|
local duration=$((end_req_time - start_req_time))
|
||||||
|
|
||||||
if [ "$http_status" -eq 200 ]; then
|
if [[ "$http_status" =~ ^[0-9]+$ ]] && [ "$http_status" -eq 200 ]; then
|
||||||
if echo "$response_body" | jq . > /dev/null 2>&1; then
|
if echo "$response_body" | jq . > /dev/null 2>&1; then
|
||||||
AUTH_TOKEN=$(echo "$response_body" | jq -r '.data.token.access_token // empty')
|
AUTH_TOKEN=$(echo "$response_body" | jq -r '.data.token.access_token // empty')
|
||||||
if [[ -n "$AUTH_TOKEN" && "$AUTH_TOKEN" != "null" ]]; then
|
if [[ -n "$AUTH_TOKEN" && "$AUTH_TOKEN" != "null" ]]; then
|
||||||
@@ -226,10 +251,14 @@ get_auth_token() {
|
|||||||
|
|
||||||
local status="PASS"
|
local status="PASS"
|
||||||
local error_msg=""
|
local error_msg=""
|
||||||
if [ "$http_status" -ne 200 ] || [[ -z "$AUTH_TOKEN" ]]; then
|
if [[ ! "$http_status" =~ ^[0-9]+$ ]] || [ "$http_status" -ne 200 ] || [[ -z "$AUTH_TOKEN" ]]; then
|
||||||
status="FAIL"
|
status="FAIL"
|
||||||
|
if [[ ! "$http_status" =~ ^[0-9]+$ ]]; then
|
||||||
|
error_msg="Failed to get valid HTTP status code (got: $http_status)"
|
||||||
|
else
|
||||||
error_msg="Authentication failed"
|
error_msg="Authentication failed"
|
||||||
fi
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
result_json=$(jq -n --arg name "User Authentication" --arg ep "/v1/auth/login" --arg meth "POST" \
|
result_json=$(jq -n --arg name "User Authentication" --arg ep "/v1/auth/login" --arg meth "POST" \
|
||||||
--arg stat "$status" --arg code "$http_status" --arg dur "$duration" \
|
--arg stat "$status" --arg code "$http_status" --arg dur "$duration" \
|
||||||
@@ -254,19 +283,26 @@ test_all_users_login_performance() {
|
|||||||
|
|
||||||
local start_time=$(date +%s%3N)
|
local start_time=$(date +%s%3N)
|
||||||
|
|
||||||
response=$(curl -s -w "\n%{http_code}" -X "POST" \
|
# Use a more compatible approach for Windows/Git Bash
|
||||||
-H "Content-Type: application/json" \
|
local temp_file=$(mktemp)
|
||||||
-d "$login_data" \
|
local status_file=$(mktemp)
|
||||||
"$BASE_URL/v1/auth/login")
|
|
||||||
|
|
||||||
local http_status=$(echo "$response" | tail -n1)
|
# Make single request and capture both body and status using response headers
|
||||||
local response_body=$(echo "$response" | sed '$d')
|
curl -s -X "POST" -H "Content-Type: application/json" -d "$login_data" "$BASE_URL/v1/auth/login" \
|
||||||
|
-D "$status_file" -o "$temp_file"
|
||||||
|
|
||||||
|
local response_body=$(cat "$temp_file")
|
||||||
|
|
||||||
|
# Extract HTTP status code from headers file
|
||||||
|
local http_status=$(head -n 1 "$status_file" | cut -d' ' -f2)
|
||||||
|
|
||||||
|
rm -f "$temp_file" "$status_file"
|
||||||
local end_time=$(date +%s%3N)
|
local end_time=$(date +%s%3N)
|
||||||
local duration=$((end_time - start_time))
|
local duration=$((end_time - start_time))
|
||||||
|
|
||||||
total_login_time=$((total_login_time + duration))
|
total_login_time=$((total_login_time + duration))
|
||||||
|
|
||||||
if [ "$http_status" -eq 200 ]; then
|
if [[ "$http_status" =~ ^[0-9]+$ ]] && [ "$http_status" -eq 200 ]; then
|
||||||
if echo "$response_body" | jq -e '.data.token.access_token' > /dev/null 2>&1; then
|
if echo "$response_body" | jq -e '.data.token.access_token' > /dev/null 2>&1; then
|
||||||
((successful_logins++))
|
((successful_logins++))
|
||||||
((PASS_COUNT++))
|
((PASS_COUNT++))
|
||||||
@@ -322,34 +358,47 @@ test_with_user() {
|
|||||||
|
|
||||||
local login_data
|
local login_data
|
||||||
login_data=$(jq -n --arg email "$email" --arg pass "$TEST_PASSWORD" '{email: $email, password: $pass}')
|
login_data=$(jq -n --arg email "$email" --arg pass "$TEST_PASSWORD" '{email: $email, password: $pass}')
|
||||||
|
|
||||||
local start_time=$(date +%s%3N)
|
local start_time=$(date +%s%3N)
|
||||||
|
|
||||||
response=$(curl -s -w "\n%{http_code}" -X "POST" \
|
# Use a more compatible approach for Windows/Git Bash
|
||||||
-H "Content-Type: application/json" \
|
local temp_file=$(mktemp)
|
||||||
-d "$login_data" \
|
local status_file=$(mktemp)
|
||||||
"$BASE_URL/v1/auth/login")
|
|
||||||
|
|
||||||
local http_status=$(echo "$response" | tail -n1)
|
# Make single request and capture both body and status using response headers
|
||||||
local response_body=$(echo "$response" | sed '$d')
|
curl -s -X "POST" -H "Content-Type: application/json" -d "$login_data" "$BASE_URL/v1/auth/login" \
|
||||||
|
-D "$status_file" -o "$temp_file"
|
||||||
|
|
||||||
|
local response_body=$(cat "$temp_file")
|
||||||
|
|
||||||
|
# Extract HTTP status code from headers file
|
||||||
|
local http_status=$(head -n 1 "$status_file" | cut -d' ' -f2)
|
||||||
|
|
||||||
|
rm -f "$temp_file" "$status_file"
|
||||||
local end_time=$(date +%s%3N)
|
local end_time=$(date +%s%3N)
|
||||||
local duration=$((end_time - start_time))
|
local duration=$((end_time - start_time))
|
||||||
|
|
||||||
if [ "$http_status" -eq 200 ]; then
|
if [[ "$http_status" =~ ^[0-9]+$ ]] && [ "$http_status" -eq 200 ]; then
|
||||||
if echo "$response_body" | jq -e '.data.token.access_token' > /dev/null 2>&1; then
|
if echo "$response_body" | jq -e '.data.token.access_token' > /dev/null 2>&1; then
|
||||||
local user_auth_token=$(echo "$response_body" | jq -r '.data.token.access_token')
|
local user_auth_token=$(echo "$response_body" | jq -r '.data.token.access_token')
|
||||||
write_test_log "SUCCESS" "✓ Login $fullname berhasil - ${duration}ms"
|
write_test_log "SUCCESS" "✓ Login $fullname berhasil - ${duration}ms"
|
||||||
|
|
||||||
local me_response
|
local me_response
|
||||||
me_response=$(curl -s -w "\n%{http_code}" -X "GET" \
|
# Use a more compatible approach for Windows/Git Bash
|
||||||
-H "Content-Type: application/json" \
|
local temp_file=$(mktemp)
|
||||||
-H "Authorization: Bearer $user_auth_token" \
|
local status_file=$(mktemp)
|
||||||
"$BASE_URL/v1/users/me")
|
|
||||||
|
|
||||||
local me_status=$(echo "$me_response" | tail -n1)
|
# Make single request and capture both body and status using response headers
|
||||||
local me_body=$(echo "$me_response" | sed '$d')
|
curl -s -X "GET" -H "Content-Type: application/json" -H "Authorization: Bearer $user_auth_token" "$BASE_URL/v1/users/me" \
|
||||||
|
-D "$status_file" -o "$temp_file"
|
||||||
|
|
||||||
if [ "$me_status" -eq 200 ]; then
|
local me_body=$(cat "$temp_file")
|
||||||
|
|
||||||
|
# Extract HTTP status code from headers file
|
||||||
|
local me_status=$(head -n 1 "$status_file" | cut -d' ' -f2)
|
||||||
|
|
||||||
|
rm -f "$temp_file" "$status_file"
|
||||||
|
|
||||||
|
if [[ "$me_status" =~ ^[0-9]+$ ]] && [ "$me_status" -eq 200 ]; then
|
||||||
((PASS_COUNT++))
|
((PASS_COUNT++))
|
||||||
write_test_log "SUCCESS" "✓ Get profile $fullname berhasil"
|
write_test_log "SUCCESS" "✓ Get profile $fullname berhasil"
|
||||||
|
|
||||||
@@ -396,17 +445,24 @@ test_comprehensive_with_user() {
|
|||||||
|
|
||||||
local start_time=$(date +%s%3N)
|
local start_time=$(date +%s%3N)
|
||||||
|
|
||||||
response=$(curl -s -w "\n%{http_code}" -X "POST" \
|
# Use a more compatible approach for Windows/Git Bash
|
||||||
-H "Content-Type: application/json" \
|
local temp_file=$(mktemp)
|
||||||
-d "$login_data" \
|
local status_file=$(mktemp)
|
||||||
"$BASE_URL/v1/auth/login")
|
|
||||||
|
|
||||||
local http_status=$(echo "$response" | tail -n1)
|
# Make single request and capture both body and status using response headers
|
||||||
local response_body=$(echo "$response" | sed '$d')
|
curl -s -X "POST" -H "Content-Type: application/json" -d "$login_data" "$BASE_URL/v1/auth/login" \
|
||||||
|
-D "$status_file" -o "$temp_file"
|
||||||
|
|
||||||
|
local response_body=$(cat "$temp_file")
|
||||||
|
|
||||||
|
# Extract HTTP status code from headers file
|
||||||
|
local http_status=$(head -n 1 "$status_file" | cut -d' ' -f2)
|
||||||
|
|
||||||
|
rm -f "$temp_file" "$status_file"
|
||||||
local end_time=$(date +%s%3N)
|
local end_time=$(date +%s%3N)
|
||||||
local duration=$((end_time - start_time))
|
local duration=$((end_time - start_time))
|
||||||
|
|
||||||
if [ "$http_status" -eq 200 ]; then
|
if [[ "$http_status" =~ ^[0-9]+$ ]] && [ "$http_status" -eq 200 ]; then
|
||||||
if echo "$response_body" | jq -e '.data.token.access_token' > /dev/null 2>&1; then
|
if echo "$response_body" | jq -e '.data.token.access_token' > /dev/null 2>&1; then
|
||||||
user_token=$(echo "$response_body" | jq -r '.data.token.access_token')
|
user_token=$(echo "$response_body" | jq -r '.data.token.access_token')
|
||||||
write_test_log "SUCCESS" "✓ Login $fullname berhasil - ${duration}ms"
|
write_test_log "SUCCESS" "✓ Login $fullname berhasil - ${duration}ms"
|
||||||
|
|||||||
Reference in New Issue
Block a user