Files
webzine/scripts/test-endpoints.sh

224 lines
11 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# =============================================================================
# test-endpoints.sh
# Teste tous les endpoints du Webzine et signale ceux qui dépassent le seuil.
#
# Usage :
# ./scripts/test-endpoints.sh [URL_BASE] [MAX_MS]
#
# Exemples :
# ./scripts/test-endpoints.sh # défauts : localhost:5038, 1000ms
# ./scripts/test-endpoints.sh http://localhost:5038 500
# =============================================================================
set -euo pipefail
BASE_URL="${1:-http://localhost:5038}"
MAX_MS="${2:-1000}"
RAPPORT_FICHIER="/tmp/webzine_rapport_endpoints.txt"
ECHECS=0
TOTAL=0
# ── Couleurs ──────────────────────────────────────────────────────────────────
ROUGE='\033[0;31m'
JAUNE='\033[1;33m'
VERT='\033[0;32m'
CYAN='\033[0;36m'
GRAS='\033[1m'
RESET='\033[0m'
# ── Fonctions utilitaires ──────────────────────────────────────────────────────
log() { echo -e "$*"; }
succes() { log "${VERT}[OK]${RESET} $*"; }
echec() { log "${ROUGE}[ÉCHEC]${RESET} $*"; ECHECS=$((ECHECS + 1)); }
lent() { log "${JAUNE}[LENT]${RESET} $*"; ECHECS=$((ECHECS + 1)); }
info() { log "${CYAN}$*${RESET}"; }
# ── verifier_endpoint ─────────────────────────────────────────────────────────
# Arguments :
# $1 Méthode HTTP (GET | POST)
# $2 URL (absolue)
# $3 Libellé (texte lisible)
# $4 Corps (optionnel, pour POST)
# $5 Content-Type (optionnel, défaut : application/x-www-form-urlencoded)
# ─────────────────────────────────────────────────────────────────────────────
verifier_endpoint() {
local METHODE="${1:-GET}"
local URL="$2"
local LIBELLE="$3"
local CORPS="${4:-}"
local TYPE_CONTENU="${5:-application/x-www-form-urlencoded}"
TOTAL=$((TOTAL + 1))
# Exécution de la requête selon la méthode HTTP
if [ "$METHODE" = "POST" ] && [ -n "$CORPS" ]; then
REPONSE=$(curl -s -o /dev/null \
-w "%{http_code}|%{time_total}" \
-X POST \
-H "Content-Type: $TYPE_CONTENU" \
-d "$CORPS" \
--max-time 10 \
"$URL" 2>&1) || REPONSE="000|9.999"
else
REPONSE=$(curl -s -o /dev/null \
-w "%{http_code}|%{time_total}" \
-X GET \
--max-time 10 \
--location \
"$URL" 2>&1) || REPONSE="000|9.999"
fi
# Extraction du code HTTP et du temps de réponse
CODE_HTTP=$(echo "$REPONSE" | cut -d'|' -f1)
TEMPS_TOTAL=$(echo "$REPONSE" | cut -d'|' -f2)
# Conversion en millisecondes entières — awk évite les problèmes de locale décimale
TEMPS_MS=$(awk "BEGIN {printf \"%.0f\", $TEMPS_TOTAL * 1000}")
# Évaluation du résultat : erreur serveur, dépassement de seuil ou succès
if [ "${CODE_HTTP:-0}" -ge 500 ] 2>/dev/null; then
echec "$LIBELLE → HTTP $CODE_HTTP (${TEMPS_MS}ms)"
echo "[ÉCHEC] $LIBELLE → HTTP $CODE_HTTP (${TEMPS_MS}ms)" >> "$RAPPORT_FICHIER"
elif [ "${TEMPS_MS:-99999}" -gt "$MAX_MS" ] 2>/dev/null; then
lent "$LIBELLE${TEMPS_MS}ms dépasse le seuil de ${MAX_MS}ms"
echo "[LENT] $LIBELLE${TEMPS_MS}ms (limite : ${MAX_MS}ms)" >> "$RAPPORT_FICHIER"
else
succes "$LIBELLE${TEMPS_MS}ms"
echo "[OK] $LIBELLE${TEMPS_MS}ms" >> "$RAPPORT_FICHIER"
fi
}
# =============================================================================
# PROGRAMME PRINCIPAL
# =============================================================================
# Initialisation du fichier de rapport
> "$RAPPORT_FICHIER"
cat >> "$RAPPORT_FICHIER" <<EOF
=== Rapport de performance des endpoints Webzine ===
URL de base : $BASE_URL
Seuil : ${MAX_MS}ms
Date : $(date -u +"%Y-%m-%dT%H:%M:%SZ")
EOF
log ""
info "╔══════════════════════════════════════════════════════════╗"
info "║ Webzine Test de performance des endpoints ║"
info "╚══════════════════════════════════════════════════════════╝"
info " URL de base : $BASE_URL"
info " Seuil : ${MAX_MS}ms"
log ""
# ── SECTION PUBLIQUE ──────────────────────────────────────────────────────────
info "── Endpoints publics ─────────────────────────────────────"
verifier_endpoint GET "$BASE_URL/" "GET / (Accueil)"
verifier_endpoint GET "$BASE_URL/Accueil" "GET /Accueil"
verifier_endpoint GET "$BASE_URL/Contact" "GET /Contact"
log ""
info "── Titre Détails ───────────────────────────────────────"
# Test des pages de détail pour les 5 premiers titres
for ID in 1 2 3 4 5; do
verifier_endpoint GET "$BASE_URL/titre/$ID" "GET /titre/$ID"
done
log ""
info "── Titre Par style ─────────────────────────────────────"
# Test du filtrage par style musical
STYLES=("Rock" "Pop" "Rap" "Jazz" "Metal" "Electronic" "Hip-Hop" "Soul" "Funk")
for STYLE in "${STYLES[@]}"; do
ENCODE=$(python3 -c "import urllib.parse; print(urllib.parse.quote('$STYLE'))" 2>/dev/null || echo "$STYLE")
verifier_endpoint GET "$BASE_URL/titres/style/$ENCODE" "GET /titres/style/$STYLE"
done
log ""
info "── Artiste ───────────────────────────────────────────────"
# Test des pages artiste avec des noms en kebab-case
ARTISTES=("fatal-bazooka" "daft-punk" "justice" "kraftwerk")
for ARTISTE in "${ARTISTES[@]}"; do
verifier_endpoint GET "$BASE_URL/artiste/$ARTISTE" "GET /artiste/$ARTISTE"
done
log ""
info "── Recherche (POST) ──────────────────────────────────────"
# Test de la recherche plein texte par mots-clés
MOTS=("rock" "jazz" "pop" "metal")
for MOT in "${MOTS[@]}"; do
verifier_endpoint POST "$BASE_URL/recherche" \
"POST /recherche (mot=$MOT)" \
"mot=$MOT"
done
log ""
# ── SECTION ADMINISTRATION ────────────────────────────────────────────────────
info "── Administration Tableau de bord ──────────────────────"
verifier_endpoint GET "$BASE_URL/Administration/Dashboard" "GET /Administration/Dashboard"
log ""
info "── Administration Artiste ──────────────────────────────"
verifier_endpoint GET "$BASE_URL/Administration/Artistes" "GET /Administration/Artistes (liste)"
verifier_endpoint GET "$BASE_URL/Administration/Artiste/Create" "GET /Administration/Artiste/Create"
verifier_endpoint GET "$BASE_URL/Administration/Artiste/Edit/1" "GET /Administration/Artiste/Edit/1"
verifier_endpoint GET "$BASE_URL/Administration/Artiste/Delete/1" "GET /Administration/Artiste/Delete/1"
log ""
info "── Administration Commentaire ──────────────────────────"
verifier_endpoint GET "$BASE_URL/Administration/Commentaires" "GET /Administration/Commentaires (liste)"
verifier_endpoint GET "$BASE_URL/Administration/Commentaire/Delete/1" "GET /Administration/Commentaire/Delete/1"
log ""
info "── Administration Style ────────────────────────────────"
verifier_endpoint GET "$BASE_URL/Administration/Styles" "GET /Administration/Styles (liste)"
verifier_endpoint GET "$BASE_URL/Administration/Style/Create" "GET /Administration/Style/Create"
verifier_endpoint GET "$BASE_URL/Administration/Style/Edit/1" "GET /Administration/Style/Edit/1"
verifier_endpoint GET "$BASE_URL/Administration/Style/Delete/1" "GET /Administration/Style/Delete/1"
log ""
info "── Administration Titre ────────────────────────────────"
verifier_endpoint GET "$BASE_URL/Administration/Titres" "GET /Administration/Titres (liste)"
verifier_endpoint GET "$BASE_URL/Administration/Titre/Create" "GET /Administration/Titre/Create"
verifier_endpoint GET "$BASE_URL/Administration/Titre/Edit/1" "GET /Administration/Titre/Edit/1"
verifier_endpoint GET "$BASE_URL/Administration/Titre/Delete/1" "GET /Administration/Titre/Delete/1"
# ── RÉCAPITULATIF ─────────────────────────────────────────────────────────────
REUSSIS=$((TOTAL - ECHECS))
log ""
info "╔══════════════════════════════════════════════════════════╗"
info "║ Résultats ║"
info "╚══════════════════════════════════════════════════════════╝"
log " Total : ${TOTAL}"
log " ${VERT}Réussis${RESET} : ${REUSSIS}"
if [ "$ECHECS" -gt 0 ]; then
log " ${ROUGE}Échecs${RESET} : ${ECHECS}"
log ""
log "${ROUGE}${GRAS}❌ ENDPOINTS EN ÉCHEC :${RESET}"
# Affichage de tous les endpoints ayant échoué ou dépassé le seuil
grep -E "^\[(ÉCHEC|LENT)\]" "$RAPPORT_FICHIER" | while IFS= read -r ligne; do
log " ${ROUGE}${RESET} $ligne"
done
log ""
log "${ROUGE}La PR doit être rejetée. Corrigez les endpoints ci-dessus.${RESET}"
else
log " ${VERT}Échecs${RESET} : 0"
log ""
log "${VERT}${GRAS}✅ Tous les endpoints respectent le seuil de ${MAX_MS}ms.${RESET}"
fi
log ""
log "Rapport complet enregistré dans : ${RAPPORT_FICHIER}"
log ""
# Écriture du récapitulatif dans le rapport
cat >> "$RAPPORT_FICHIER" <<EOF
=== Récapitulatif ===
Total : $TOTAL
Réussis : $REUSSIS
Échecs : $ECHECS
EOF