Skip to content

Commit caba752

Browse files
test(cors): cover empty-token continue branches (patch coverage)
cors_preflight_allowlist.go lines 70+88 are defensive 'skip empty token' branches that the BUG-API-066/067 regression tests don't hit. Add two tiny tests that pass comma-separated input with stray double-commas so diff-cover lands at 100% on the new file. auth.go lines 519-520 (the AuthErrorExpiredToken assignment for optionalAuth strict mode) are already covered by the existing TestOptionalAuthStrict_ExpiredToken_Returns401 — verified locally: 'go tool cover' shows hit count 1 on both. Verified locally: middleware suite passes, line-coverage on the two new branches confirmed via 'go tool cover -func'. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent af57ed2 commit caba752

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package middleware_test
2+
3+
// cors_preflight_coverage_test.go — patch-coverage backfill for the two
4+
// empty-token `continue` branches in cors_preflight_allowlist.go
5+
// (lines 70 and 88) that the main BUG-API-066/067 test pair didn't hit.
6+
//
7+
// Both branches are reached when the comma-separated input contains an
8+
// empty token between two non-empty values — a real browser would never
9+
// emit that, but defensive parsing tolerates it. Without these tests the
10+
// patch-coverage gate stays at 95% on this file.
11+
12+
import (
13+
"net/http"
14+
"net/http/httptest"
15+
"testing"
16+
17+
"github.com/gofiber/fiber/v2"
18+
"github.com/stretchr/testify/require"
19+
20+
"instant.dev/internal/middleware"
21+
)
22+
23+
// TestPreflightAllowlist_SkipsEmptyHeaderToken exercises the inner
24+
// `if name == "" { continue }` at cors_preflight_allowlist.go:70. The
25+
// preflight asks for `Authorization,, Content-Type` (note the double
26+
// comma producing an empty middle token) which must NOT 403 — the empty
27+
// token is skipped and both real headers pass.
28+
func TestPreflightAllowlist_SkipsEmptyHeaderToken(t *testing.T) {
29+
app := fiber.New()
30+
app.Use(middleware.PreflightAllowlist(
31+
"GET,POST,OPTIONS",
32+
"Content-Type,Authorization",
33+
))
34+
app.Get("/x", func(c *fiber.Ctx) error { return c.JSON(fiber.Map{"ok": true}) })
35+
36+
req := httptest.NewRequest(http.MethodOptions, "/x", nil)
37+
req.Header.Set("Origin", "https://instanode.dev")
38+
req.Header.Set("Access-Control-Request-Method", "GET")
39+
req.Header.Set("Access-Control-Request-Headers", "Authorization,, Content-Type")
40+
41+
resp, err := app.Test(req, 5000)
42+
require.NoError(t, err)
43+
defer resp.Body.Close()
44+
45+
require.NotEqual(t, http.StatusForbidden, resp.StatusCode,
46+
"empty middle token in AC-Request-Headers must be skipped, not rejected")
47+
}
48+
49+
// TestPreflightAllowlist_SkipsEmptyAllowlistToken exercises the inner
50+
// `if t == "" { continue }` of commaSet at cors_preflight_allowlist.go:88.
51+
// Constructed by passing an allowMethods string with a double-comma so
52+
// commaSet must skip the empty entry.
53+
func TestPreflightAllowlist_SkipsEmptyAllowlistToken(t *testing.T) {
54+
app := fiber.New()
55+
// Note the double comma in the methods list — commaSet must skip it
56+
// so "GET" still registers as allowed.
57+
app.Use(middleware.PreflightAllowlist(
58+
"GET,,POST,OPTIONS",
59+
"Content-Type",
60+
))
61+
app.Get("/x", func(c *fiber.Ctx) error { return c.JSON(fiber.Map{"ok": true}) })
62+
63+
req := httptest.NewRequest(http.MethodOptions, "/x", nil)
64+
req.Header.Set("Origin", "https://instanode.dev")
65+
req.Header.Set("Access-Control-Request-Method", "GET")
66+
67+
resp, err := app.Test(req, 5000)
68+
require.NoError(t, err)
69+
defer resp.Body.Close()
70+
71+
require.NotEqual(t, http.StatusForbidden, resp.StatusCode,
72+
"GET must remain allowed even when allowMethods has a stray empty token")
73+
}

0 commit comments

Comments
 (0)