Skip to content

Commit 6e3bb87

Browse files
Manas Srivastavaclaude
andcommitted
test(presign): cover the scheme-fallback branch in rewritePresignHost
Diff-cover round 2 flagged storage_presign.go lines 352-353 (the `if scheme == "" { scheme = signed.Scheme }` branch) as untested. The prior subtest passed "http://..." which set Scheme to "http", missing the fallback. New subtest uses a protocol-relative "//cdn.instanode.dev" so url.Parse returns Scheme=="" and the branch fires. signed URL was https, so the rewritten output must inherit https — pinned. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 9cf899c commit 6e3bb87

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

internal/handlers/storage_presign_test.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,25 @@ func TestRewritePresignHost_CanonicalHostSubstitution(t *testing.T) {
131131
assert.Equal(t, "", out)
132132
})
133133

134-
t.Run("publicURL inherits signed scheme when scheme missing", func(t *testing.T) {
135-
// Test the scheme-fallback branch where publicURL is parsed as a
136-
// host-only string (no scheme). url.Parse on a bare host returns
137-
// {Scheme:"" Host:""} (it lands in Opaque) — so synthesize a
138-
// scheme-less URL by passing "//host/path".
134+
t.Run("publicURL with explicit scheme overrides signed scheme", func(t *testing.T) {
135+
// publicURL with explicit scheme — verify that scheme wins.
139136
signed, err := url.Parse(signedRaw)
140137
assert.NoError(t, err)
141-
// publicURL with scheme but bare host — verify scheme override path.
142138
out, ok := rewritePresignHost(signed, "http://s3.instanode.dev")
143139
assert.True(t, ok)
144140
assert.True(t, strings.HasPrefix(out, "http://s3.instanode.dev/"))
145141
})
142+
143+
t.Run("publicURL with no scheme inherits signed scheme", func(t *testing.T) {
144+
// Protocol-relative URL: url.Parse on "//host/path" returns
145+
// {Scheme:"" Host:"host"}. Exercises the
146+
// `if scheme == "" { scheme = signed.Scheme }` branch.
147+
signed, err := url.Parse(signedRaw)
148+
assert.NoError(t, err)
149+
out, ok := rewritePresignHost(signed, "//cdn.instanode.dev")
150+
assert.True(t, ok, "protocol-relative publicURL must still rewrite")
151+
// signed had scheme "https" — must be preserved on rewrite.
152+
assert.True(t, strings.HasPrefix(out, "https://cdn.instanode.dev/"),
153+
"scheme must fall back to the signed URL's scheme; got %q", out)
154+
})
146155
}

0 commit comments

Comments
 (0)