package middleware

import "testing"

func TestBearerToken(t *testing.T) {
	token, ok := bearerToken("Bearer abc.def")
	if !ok || token != "abc.def" {
		t.Fatalf("unexpected bearer token: %q %v", token, ok)
	}
}

func TestBearerTokenRejectsMissingOrEmpty(t *testing.T) {
	if _, ok := bearerToken(""); ok {
		t.Fatal("expected empty header to be rejected")
	}
	if _, ok := bearerToken("Basic abc"); ok {
		t.Fatal("expected non-bearer header to be rejected")
	}
	if _, ok := bearerToken("Bearer "); ok {
		t.Fatal("expected empty bearer token to be rejected")
	}
}

func TestImpersonationReadOnlyMethods(t *testing.T) {
	for _, method := range []string{"GET", "HEAD", "OPTIONS"} {
		if !isReadOnlyMethod(method) {
			t.Fatalf("%s should be read-only", method)
		}
	}
	for _, method := range []string{"POST", "PUT", "PATCH", "DELETE"} {
		if isReadOnlyMethod(method) {
			t.Fatalf("%s should be rejected for impersonation", method)
		}
	}
}
