101 lines
2.8 KiB
Go
101 lines
2.8 KiB
Go
package middleware
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func TestResolveAllowedOrigin(t *testing.T) {
|
|
got := resolveAllowedOrigin("https://example.com", []string{"*"}, false)
|
|
if got != "*" {
|
|
t.Fatalf("wildcard without credentials should return *, got %s", got)
|
|
}
|
|
|
|
got = resolveAllowedOrigin("https://example.com", []string{"*"}, true)
|
|
if got != "https://example.com" {
|
|
t.Fatalf("wildcard with credentials should echo origin, got %s", got)
|
|
}
|
|
|
|
got = resolveAllowedOrigin("https://a.example.com", []string{"https://a.example.com", "https://b.example.com"}, false)
|
|
if got != "https://a.example.com" {
|
|
t.Fatalf("allow-list should return matched origin, got %s", got)
|
|
}
|
|
|
|
got = resolveAllowedOrigin("https://x.example.com", []string{"https://a.example.com"}, false)
|
|
if got != "" {
|
|
t.Fatalf("unmatched origin should be empty, got %s", got)
|
|
}
|
|
}
|
|
|
|
func TestRequestIDMiddleware(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
r := gin.New()
|
|
r.Use(RequestIDMiddleware())
|
|
r.GET("/ping", func(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{"request_id": getRequestID(c)})
|
|
})
|
|
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/ping", nil)
|
|
req.Header.Set(requestIDHeader, "req-123")
|
|
r.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status want 200 got %d", w.Code)
|
|
}
|
|
if w.Header().Get(requestIDHeader) != "req-123" {
|
|
t.Fatalf("response request id want req-123 got %s", w.Header().Get(requestIDHeader))
|
|
}
|
|
var resp map[string]string
|
|
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
|
t.Fatalf("unmarshal response failed: %v", err)
|
|
}
|
|
if resp["request_id"] != "req-123" {
|
|
t.Fatalf("context request id want req-123 got %s", resp["request_id"])
|
|
}
|
|
|
|
w2 := httptest.NewRecorder()
|
|
req2 := httptest.NewRequest(http.MethodGet, "/ping", nil)
|
|
r.ServeHTTP(w2, req2)
|
|
generated := w2.Header().Get(requestIDHeader)
|
|
if generated == "" {
|
|
t.Fatalf("generated request id should not be empty")
|
|
}
|
|
if resp := strings.TrimSpace(generated); resp == "" {
|
|
t.Fatalf("generated request id should not be blank")
|
|
}
|
|
}
|
|
|
|
func TestJWTAuthMiddlewareMissingSecret(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
r := gin.New()
|
|
r.Use(JWTAuthMiddleware("", nil))
|
|
r.GET("/admin/ping", func(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{"ok": true})
|
|
})
|
|
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/admin/ping", nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status want 200 got %d", w.Code)
|
|
}
|
|
var resp struct {
|
|
StatusCode int `json:"status_code"`
|
|
}
|
|
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
|
t.Fatalf("unmarshal response failed: %v", err)
|
|
}
|
|
if resp.StatusCode != 401 {
|
|
t.Fatalf("status_code want 401 got %d", resp.StatusCode)
|
|
}
|
|
}
|