This repository has been archived on 2026-09-13. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files

56 lines
1.5 KiB
Go

package httpapi
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestRouterExposesOnlySpecifiedMethods(t *testing.T) {
endpoint := http.HandlerFunc(func(response http.ResponseWriter, _ *http.Request) {
response.WriteHeader(http.StatusNoContent)
})
router, err := NewRouter(Endpoints{
Metadata: endpoint,
Token: endpoint,
JWKS: endpoint,
Health: endpoint,
Ready: endpoint,
})
if err != nil {
t.Fatalf("NewRouter() error = %v", err)
}
tests := []struct {
method string
path string
status int
}{
{http.MethodGet, "/.well-known/oauth-authorization-server", http.StatusNoContent},
{http.MethodPost, "/oauth2/token", http.StatusNoContent},
{http.MethodGet, "/oauth2/jwks", http.StatusNoContent},
{http.MethodGet, "/healthz", http.StatusNoContent},
{http.MethodGet, "/readyz", http.StatusNoContent},
{http.MethodGet, "/oauth2/token", http.StatusMethodNotAllowed},
{http.MethodPost, "/oauth2/jwks", http.StatusMethodNotAllowed},
{http.MethodGet, "/unknown", http.StatusNotFound},
}
for _, test := range tests {
t.Run(test.method+" "+test.path, func(t *testing.T) {
request := httptest.NewRequest(test.method, test.path, nil)
response := httptest.NewRecorder()
router.ServeHTTP(response, request)
if response.Code != test.status {
t.Fatalf("status = %d, want %d", response.Code, test.status)
}
})
}
}
func TestRouterRequiresEveryEndpoint(t *testing.T) {
if _, err := NewRouter(Endpoints{}); err == nil {
t.Fatal("NewRouter() error = nil, want missing endpoint error")
}
}