Fix order when reading custom headers in resources.GetRemote

Fixes #10616
This commit is contained in:
Bjørn Erik Pedersen 2023-01-16 11:00:55 +01:00
parent 6e9fa9e0fd
commit b5d485060f
2 changed files with 54 additions and 12 deletions

View file

@ -91,15 +91,10 @@ func (c *Client) FromRemote(uri string, optionsm map[string]any) (resource.Resou
return nil, err
}
req, err := http.NewRequest(options.Method, uri, options.BodyReader())
req, err := options.NewRequest(uri)
if err != nil {
return nil, fmt.Errorf("failed to create request for resource %s: %w", uri, err)
}
addDefaultHeaders(req)
if options.Headers != nil {
addUserProvidedHeaders(options.Headers, req)
}
res, err := c.httpClient.Do(req)
if err != nil {
@ -207,12 +202,7 @@ func calculateResourceID(uri string, optionsm map[string]any) string {
return helpers.HashString(uri, optionsm)
}
func addDefaultHeaders(req *http.Request, accepts ...string) {
for _, accept := range accepts {
if !hasHeaderValue(req.Header, "Accept", accept) {
req.Header.Add("Accept", accept)
}
}
func addDefaultHeaders(req *http.Request) {
if !hasHeaderKey(req.Header, "User-Agent") {
req.Header.Add("User-Agent", "Hugo Static Site Generator")
}
@ -264,6 +254,23 @@ func (o fromRemoteOptions) BodyReader() io.Reader {
return bytes.NewBuffer(o.Body)
}
func (o fromRemoteOptions) NewRequest(url string) (*http.Request, error) {
req, err := http.NewRequest(o.Method, url, o.BodyReader())
if err != nil {
return nil, err
}
// First add any user provided headers.
if o.Headers != nil {
addUserProvidedHeaders(o.Headers, req)
}
// Then add default headers not provided by the user.
addDefaultHeaders(req)
return req, nil
}
func decodeRemoteOptions(optionsm map[string]any) (fromRemoteOptions, error) {
options := fromRemoteOptions{
Method: "GET",

View file

@ -20,6 +20,8 @@ import (
)
func TestDecodeRemoteOptions(t *testing.T) {
t.Parallel()
c := qt.New(t)
for _, test := range []struct {
@ -81,10 +83,43 @@ func TestDecodeRemoteOptions(t *testing.T) {
})
}
}
func TestOptionsNewRequest(t *testing.T) {
t.Parallel()
c := qt.New(t)
opts := fromRemoteOptions{
Method: "GET",
Body: []byte("foo"),
}
req, err := opts.NewRequest("https://example.com/api")
c.Assert(err, qt.IsNil)
c.Assert(req.Method, qt.Equals, "GET")
c.Assert(req.Header["User-Agent"], qt.DeepEquals, []string{"Hugo Static Site Generator"})
opts = fromRemoteOptions{
Method: "GET",
Body: []byte("foo"),
Headers: map[string]any{
"User-Agent": "foo",
},
}
req, err = opts.NewRequest("https://example.com/api")
c.Assert(err, qt.IsNil)
c.Assert(req.Method, qt.Equals, "GET")
c.Assert(req.Header["User-Agent"], qt.DeepEquals, []string{"foo"})
}
func TestCalculateResourceID(t *testing.T) {
t.Parallel()
c := qt.New(t)
c.Assert(calculateResourceID("foo", nil), qt.Equals, "5917621528921068675")