check / check (push) Failing after 1m56s
A new server test sends requests with an Origin header through the server's routes and expects Access-Control-Allow-Origin, set to the configured origin, on both image routes, a preflight OPTIONS request included, and no such header on the login and URL generator pages. It fails for now: the CORS middleware still wraps every route. The encrypted image path the maintenance tests use is now a named constant, shared with the new test; what they check is unchanged. Model: opus-5-5
65 lines
1.8 KiB
Go
65 lines
1.8 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
// TestCORSOnlyOnImageRoutes verifies that the image routes answer with the
|
|
// configured access_control_allow_origin, a preflight request included, and
|
|
// that the login and URL generator pages send no Access-Control-Allow-Origin,
|
|
// so no other site can read them. /metrics is left out: its middleware
|
|
// registers with the process-wide Prometheus registry, which only one test
|
|
// in this package can do.
|
|
func TestCORSOnlyOnImageRoutes(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
const appOrigin = "https://app.example.com"
|
|
|
|
s := newTestServer(t)
|
|
s.config.AccessControlAllowOrigin = appOrigin
|
|
s.SetupRoutes()
|
|
|
|
requests := []struct {
|
|
method string
|
|
path string
|
|
want string
|
|
}{
|
|
{http.MethodGet, unsignedImagePath, appOrigin},
|
|
{http.MethodHead, unsignedImagePath, appOrigin},
|
|
{http.MethodOptions, unsignedImagePath, appOrigin},
|
|
{http.MethodGet, encryptedImagePath, appOrigin},
|
|
{http.MethodGet, "/", ""},
|
|
{http.MethodOptions, "/", ""},
|
|
{http.MethodPost, "/generate", ""},
|
|
{http.MethodGet, "/logout", ""},
|
|
}
|
|
|
|
for _, tc := range requests {
|
|
t.Run(tc.method+" "+tc.path, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
req := httptest.NewRequestWithContext(
|
|
t.Context(), tc.method, tc.path, nil)
|
|
req.Header.Set("Origin", appOrigin)
|
|
|
|
// An OPTIONS request naming the method it asks about is the
|
|
// preflight a browser sends before some cross-origin requests.
|
|
if tc.method == http.MethodOptions {
|
|
req.Header.Set("Access-Control-Request-Method", http.MethodGet)
|
|
}
|
|
|
|
rec := httptest.NewRecorder()
|
|
s.ServeHTTP(rec, req)
|
|
t.Logf("status %d", rec.Code)
|
|
|
|
got := rec.Header().Get("Access-Control-Allow-Origin")
|
|
if got != tc.want {
|
|
t.Errorf("Access-Control-Allow-Origin = %q, want %q",
|
|
got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|