- webhook.go: HMAC-SHA256 WebhookSigner matching PHP WebhookSignature — sign/verify, X-Webhook-Signature / X-Webhook-Timestamp headers, VerifyRequest middleware helper, 5-minute default tolerance, secret generator (RFC §6) - sunset.go: ApiSunsetWith(date, replacement, opts...) + WithSunsetNoticeURL; ApiSunset now emits API-Suggested-Replacement when replacement set; RouteDescription.NoticeURL surfaces API-Deprecation-Notice-URL (RFC §8) - options.go + api.go + transport.go: WithWebSocket(gin.HandlerFunc) alongside existing WithWSHandler(http.Handler); gin form wins when both supplied (RFC §2.2) - openapi.go: apiSuggestedReplacement + apiDeprecationNoticeURL as reusable header components; NoticeURL on a RouteDescription flips operation deprecated flag and emits response header doc - cmd/api/*.go: migrated from Cobra (cli.NewCommand, StringFlag) to new path-based CLI API (c.Command + core.Options.String/Int/Bool); replaces the 1,422-line Cobra test suite with _Good/_Bad/_Ugly triads on the new surface - webhook_test.go + sunset_test.go + websocket_test.go: full coverage Co-Authored-By: Virgil <virgil@lethean.io>
200 lines
6.5 KiB
Go
200 lines
6.5 KiB
Go
// SPDX-License-Identifier: EUPL-1.2
|
|
|
|
package api_test
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
api "dappco.re/go/core/api"
|
|
)
|
|
|
|
type sunsetStubGroup struct{}
|
|
|
|
func (sunsetStubGroup) Name() string { return "legacy" }
|
|
func (sunsetStubGroup) BasePath() string { return "/legacy" }
|
|
func (sunsetStubGroup) RegisterRoutes(rg *gin.RouterGroup) {
|
|
rg.GET("/status", func(c *gin.Context) {
|
|
c.JSON(http.StatusOK, api.OK("ok"))
|
|
})
|
|
}
|
|
|
|
type sunsetLinkStubGroup struct{}
|
|
|
|
func (sunsetLinkStubGroup) Name() string { return "legacy-link" }
|
|
func (sunsetLinkStubGroup) BasePath() string { return "/legacy-link" }
|
|
func (sunsetLinkStubGroup) RegisterRoutes(rg *gin.RouterGroup) {
|
|
rg.GET("/status", func(c *gin.Context) {
|
|
c.Header("Link", "<https://example.com/docs>; rel=\"help\"")
|
|
c.JSON(http.StatusOK, api.OK("ok"))
|
|
})
|
|
}
|
|
|
|
type sunsetHeaderStubGroup struct{}
|
|
|
|
func (sunsetHeaderStubGroup) Name() string { return "legacy-headers" }
|
|
func (sunsetHeaderStubGroup) BasePath() string { return "/legacy-headers" }
|
|
func (sunsetHeaderStubGroup) RegisterRoutes(rg *gin.RouterGroup) {
|
|
rg.GET("/status", func(c *gin.Context) {
|
|
c.Header("Deprecation", "false")
|
|
c.Header("Sunset", "Wed, 01 Jan 2025 00:00:00 GMT")
|
|
c.Header("X-API-Warn", "Existing warning")
|
|
c.Header("Link", "<https://example.com/docs>; rel=\"help\"")
|
|
c.JSON(http.StatusOK, api.OK("ok"))
|
|
})
|
|
}
|
|
|
|
func TestWithSunset_Good_AddsDeprecationHeaders(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
e, err := api.New(api.WithSunset("2025-06-01", "/api/v2/status"))
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
e.Register(sunsetStubGroup{})
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest(http.MethodGet, "/legacy/status", nil)
|
|
e.Handler().ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d", w.Code)
|
|
}
|
|
if got := w.Header().Get("Deprecation"); got != "true" {
|
|
t.Fatalf("expected Deprecation=true, got %q", got)
|
|
}
|
|
if got := w.Header().Get("Sunset"); got != "Sun, 01 Jun 2025 00:00:00 GMT" {
|
|
t.Fatalf("expected formatted Sunset header, got %q", got)
|
|
}
|
|
if got := w.Header().Get("Link"); got != "</api/v2/status>; rel=\"successor-version\"" {
|
|
t.Fatalf("expected successor Link header, got %q", got)
|
|
}
|
|
if got := w.Header().Get("API-Suggested-Replacement"); got != "/api/v2/status" {
|
|
t.Fatalf("expected API-Suggested-Replacement to mirror replacement URL, got %q", got)
|
|
}
|
|
if got := w.Header().Get("X-API-Warn"); got != "This endpoint is deprecated and will be removed on 2025-06-01." {
|
|
t.Fatalf("expected deprecation warning, got %q", got)
|
|
}
|
|
}
|
|
|
|
// TestApiSunsetWith_Good_AddsNoticeURLHeader exercises ApiSunsetWith with the
|
|
// WithSunsetNoticeURL option to verify the spec §8 notice header is emitted.
|
|
func TestApiSunsetWith_Good_AddsNoticeURLHeader(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
mw := api.ApiSunsetWith(
|
|
"2026-04-30",
|
|
"POST /api/v2/billing/invoices",
|
|
api.WithSunsetNoticeURL("https://docs.api.dappco.re/deprecation/billing"),
|
|
)
|
|
|
|
r := gin.New()
|
|
r.Use(mw)
|
|
r.GET("/billing", func(c *gin.Context) { c.JSON(http.StatusOK, api.OK("ok")) })
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest(http.MethodGet, "/billing", nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
if got := w.Header().Get("API-Deprecation-Notice-URL"); got != "https://docs.api.dappco.re/deprecation/billing" {
|
|
t.Fatalf("expected API-Deprecation-Notice-URL header, got %q", got)
|
|
}
|
|
if got := w.Header().Get("API-Suggested-Replacement"); got != "POST /api/v2/billing/invoices" {
|
|
t.Fatalf("expected API-Suggested-Replacement to mirror replacement, got %q", got)
|
|
}
|
|
}
|
|
|
|
// TestApiSunsetWith_Bad_OmitsEmptyOptionalHeaders ensures empty option values
|
|
// do not emit blank headers, keeping the response surface clean.
|
|
func TestApiSunsetWith_Bad_OmitsEmptyOptionalHeaders(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
mw := api.ApiSunsetWith("", "", api.WithSunsetNoticeURL(" "))
|
|
|
|
r := gin.New()
|
|
r.Use(mw)
|
|
r.GET("/x", func(c *gin.Context) { c.JSON(http.StatusOK, api.OK("ok")) })
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest(http.MethodGet, "/x", nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
if got := w.Header().Get("Sunset"); got != "" {
|
|
t.Fatalf("expected no Sunset header for empty date, got %q", got)
|
|
}
|
|
if got := w.Header().Get("Link"); got != "" {
|
|
t.Fatalf("expected no Link header for empty replacement, got %q", got)
|
|
}
|
|
if got := w.Header().Get("API-Suggested-Replacement"); got != "" {
|
|
t.Fatalf("expected no API-Suggested-Replacement for empty replacement, got %q", got)
|
|
}
|
|
if got := w.Header().Get("API-Deprecation-Notice-URL"); got != "" {
|
|
t.Fatalf("expected no API-Deprecation-Notice-URL for blank URL, got %q", got)
|
|
}
|
|
if got := w.Header().Get("Deprecation"); got != "true" {
|
|
t.Fatalf("expected Deprecation=true even with no metadata, got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestWithSunset_Good_PreservesExistingLinkHeaders(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
e, err := api.New(api.WithSunset("2025-06-01", "/api/v2/status"))
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
e.Register(sunsetLinkStubGroup{})
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest(http.MethodGet, "/legacy-link/status", nil)
|
|
e.Handler().ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d", w.Code)
|
|
}
|
|
|
|
links := w.Header().Values("Link")
|
|
if len(links) != 2 {
|
|
t.Fatalf("expected 2 Link header values, got %v", links)
|
|
}
|
|
if links[0] != "<https://example.com/docs>; rel=\"help\"" {
|
|
t.Fatalf("expected existing Link header to be preserved first, got %q", links[0])
|
|
}
|
|
if links[1] != "</api/v2/status>; rel=\"successor-version\"" {
|
|
t.Fatalf("expected successor Link header to be appended, got %q", links[1])
|
|
}
|
|
}
|
|
|
|
func TestWithSunset_Good_PreservesExistingDeprecationHeaders(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
e, err := api.New(api.WithSunset("2025-06-01", "/api/v2/status"))
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
e.Register(sunsetHeaderStubGroup{})
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest(http.MethodGet, "/legacy-headers/status", nil)
|
|
e.Handler().ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d", w.Code)
|
|
}
|
|
|
|
if got := w.Header().Values("Deprecation"); len(got) != 2 {
|
|
t.Fatalf("expected 2 Deprecation header values, got %v", got)
|
|
}
|
|
if got := w.Header().Values("Sunset"); len(got) != 2 {
|
|
t.Fatalf("expected 2 Sunset header values, got %v", got)
|
|
}
|
|
if got := w.Header().Values("X-API-Warn"); len(got) != 2 {
|
|
t.Fatalf("expected 2 X-API-Warn header values, got %v", got)
|
|
}
|
|
if got := w.Header().Values("Link"); len(got) != 2 {
|
|
t.Fatalf("expected 2 Link header values, got %v", got)
|
|
}
|
|
}
|