resources: Add responseHeaders option to resources.GetRemote

* These response headers will be included in `.Data.Headers` if found.
* The header name matching is case insensitive.
* `Data.Headers` is of type `map[string][]string`
* In most cases there will be only one value per header key, but e.g. `Set-Cookie` commonly has multiple values.

Fixes #12521
This commit is contained in:
Bjørn Erik Pedersen
2025-01-23 10:05:44 +01:00
parent 51bb2fedab
commit 68586c891c
2 changed files with 57 additions and 10 deletions

View File

@@ -56,7 +56,42 @@ func TestGetRemoteHead(t *testing.T) {
b.AssertFileContent("public/index.html",
"Head Content: .",
"Head Data: map[ContentLength:18210 ContentType:image/png Status:200 OK StatusCode:200 TransferEncoding:[]]",
"Head Data: map[ContentLength:18210 ContentType:image/png Headers:map[] Status:200 OK StatusCode:200 TransferEncoding:[]]",
)
}
func TestGetRemoteResponseHeaders(t *testing.T) {
files := `
-- config.toml --
[security]
[security.http]
methods = ['(?i)GET|POST|HEAD']
urls = ['.*gohugo\.io.*']
-- layouts/index.html --
{{ $url := "https://gohugo.io/img/hugo.png" }}
{{ $opts := dict "method" "head" "responseHeaders" (slice "X-Frame-Options" "Server") }}
{{ with try (resources.GetRemote $url $opts) }}
{{ with .Err }}
{{ errorf "Unable to get remote resource: %s" . }}
{{ else with .Value }}
Response Headers: {{ .Data.Headers }}
{{ else }}
{{ errorf "Unable to get remote resource: %s" $url }}
{{ end }}
{{ end }}
`
b := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{
T: t,
TxtarString: files,
},
)
b.Build()
b.AssertFileContent("public/index.html",
"Response Headers: map[Server:[Netlify] X-Frame-Options:[DENY]]",
)
}