49 lines
1.3 KiB
Go
49 lines
1.3 KiB
Go
package ari
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"ai-operator/internal/config"
|
|
)
|
|
|
|
func TestHealthCheck(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
user, pass, ok := r.BasicAuth()
|
|
if ok && user == "ai_operator" && pass == "secret" {
|
|
w.WriteHeader(http.StatusOK)
|
|
_, _ = w.Write([]byte(`{"ok":true}`))
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient(config.AsteriskConfig{ARIURL: server.URL, ARIUser: "ai_operator", ARIPassword: "secret"})
|
|
status := client.HealthCheck(context.Background())
|
|
if !status.AuthenticatedOK {
|
|
t.Fatal("authenticated request should be OK")
|
|
}
|
|
if !status.UnauthenticatedReturns401 {
|
|
t.Fatal("unauthenticated request should return 401")
|
|
}
|
|
}
|
|
|
|
func TestHealthCheckBadCredentials(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient(config.AsteriskConfig{ARIURL: server.URL, ARIUser: "bad", ARIPassword: "bad"})
|
|
status := client.HealthCheck(context.Background())
|
|
if status.AuthenticatedOK {
|
|
t.Fatal("authenticated request should fail")
|
|
}
|
|
if status.Error == "" {
|
|
t.Fatal("expected helpful health error")
|
|
}
|
|
}
|