diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-01-16 11:00:55 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-01-16 14:44:15 +0100 |
commit | b5d485060fc5f6222e730e5f59f7946b4602c7c6 (patch) | |
tree | ada2265a8134b787f3ca25cd4ac554d12d67110e | |
parent | 6e9fa9e0fd15f2e373873e9ddfc888879c15e28b (diff) |
Fix order when reading custom headers in resources.GetRemote
Fixes #10616
-rw-r--r-- | resources/resource_factories/create/remote.go | 31 | ||||
-rw-r--r-- | resources/resource_factories/create/remote_test.go | 35 |
2 files changed, 54 insertions, 12 deletions
diff --git a/resources/resource_factories/create/remote.go b/resources/resource_factories/create/remote.go index 51199dc93..fa5533d7b 100644 --- a/resources/resource_factories/create/remote.go +++ b/resources/resource_factories/create/remote.go @@ -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", diff --git a/resources/resource_factories/create/remote_test.go b/resources/resource_factories/create/remote_test.go index c2a3b7b32..a443a0633 100644 --- a/resources/resource_factories/create/remote_test.go +++ b/resources/resource_factories/create/remote_test.go @@ -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") |