99 lines
3.7 KiB
Go
99 lines
3.7 KiB
Go
package httpserver
|
|
|
|
import (
|
|
"Rezina-Backend/internal/domain"
|
|
"Rezina-Backend/internal/web_server/handlers"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
func TestCORSPreflightHeaderCasing(t *testing.T) {
|
|
for _, origin := range []string{"https://admin.rezina.yaltopia.com", "https://rezina.yaltopia.com"} {
|
|
for _, casing := range []struct {
|
|
name, origin, method, headers string
|
|
}{
|
|
{"canonical", "Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers"},
|
|
{"lowercase", "origin", "access-control-request-method", "access-control-request-headers"},
|
|
{"mixed", "oRiGiN", "aCcEsS-CoNtRoL-ReQuEsT-MeThOd", "aCcEsS-CoNtRoL-ReQuEsT-HeAdErS"},
|
|
} {
|
|
t.Run(origin+"/"+casing.name, func(t *testing.T) {
|
|
server := newFiberApp()
|
|
a := audienceTestApp(domain.UserStatusActive)
|
|
a.initAdminRoutes(server.Group("/api/v1/admin", apiAudience("admin")), &handlers.Handler{})
|
|
request := httptest.NewRequest(http.MethodOptions, "/api/v1/admin/team/login", nil)
|
|
// Assign directly: Header.Set canonicalizes keys and would conceal the production bug.
|
|
request.Header[casing.origin] = []string{origin}
|
|
request.Header[casing.method] = []string{http.MethodPost}
|
|
request.Header[casing.headers] = []string{"content-type,authorization,accept"}
|
|
response, err := server.Test(request)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer response.Body.Close()
|
|
if response.StatusCode != http.StatusNoContent {
|
|
t.Fatalf("status=%d want=204", response.StatusCode)
|
|
}
|
|
if got := response.Header.Get("Access-Control-Allow-Origin"); got != "*" {
|
|
t.Errorf("allow origin=%q want=*", got)
|
|
}
|
|
if got := response.Header.Get("Access-Control-Allow-Methods"); !strings.Contains(got, "POST") {
|
|
t.Errorf("POST missing from allowed methods: %q", got)
|
|
}
|
|
for _, header := range []string{"content-type", "authorization", "accept"} {
|
|
if got := strings.ToLower(response.Header.Get("Access-Control-Allow-Headers")); !strings.Contains(got, header) {
|
|
t.Errorf("%s missing from allowed headers: %q", header, got)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCORSActualRequestHeaderCasing(t *testing.T) {
|
|
for _, casing := range []struct{ name, origin, authorization string }{
|
|
{"canonical", "Origin", "Authorization"},
|
|
{"lowercase", "origin", "authorization"},
|
|
{"mixed", "oRiGiN", "aUtHoRiZaTiOn"},
|
|
} {
|
|
for _, authenticated := range []bool{false, true} {
|
|
name := casing.name + "/unauthenticated"
|
|
if authenticated {
|
|
name = casing.name + "/authenticated"
|
|
}
|
|
t.Run(name, func(t *testing.T) {
|
|
server := newFiberApp()
|
|
a := audienceTestApp(domain.UserStatusActive)
|
|
server.Get("/api/v1/admin/courses", a.adminAuthMiddleware, func(c *fiber.Ctx) error {
|
|
c.Set("Content-Disposition", `attachment; filename="courses.csv"`)
|
|
return c.SendString("courses")
|
|
})
|
|
request := httptest.NewRequest(http.MethodGet, "/api/v1/admin/courses", nil)
|
|
request.Header[casing.origin] = []string{"https://admin.rezina.yaltopia.com"}
|
|
wantStatus := http.StatusUnauthorized
|
|
if authenticated {
|
|
request.Header[casing.authorization] = []string{"Bearer " + audienceToken(t, a, domain.RoleAdmin)}
|
|
wantStatus = http.StatusOK
|
|
}
|
|
response, err := server.Test(request)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer response.Body.Close()
|
|
if response.StatusCode != wantStatus {
|
|
t.Errorf("status=%d want=%d", response.StatusCode, wantStatus)
|
|
}
|
|
if got := response.Header.Get("Access-Control-Allow-Origin"); got != "*" {
|
|
t.Errorf("allow origin=%q want=*", got)
|
|
}
|
|
if got := response.Header.Get("Access-Control-Expose-Headers"); got != "Content-Disposition" {
|
|
t.Errorf("exposed headers=%q want=Content-Disposition", got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|