Fix order when reading custom headers in resources.GetRemote - hugo - [fork] hugo port for 9front
HTML git clone git@git.drkhsh.at/hugo.git
DIR Log
DIR Files
DIR Refs
DIR Submodules
DIR README
DIR LICENSE
---
DIR commit b5d485060fc5f6222e730e5f59f7946b4602c7c6
DIR parent 6e9fa9e0fd15f2e373873e9ddfc888879c15e28b
HTML Author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Date: Mon, 16 Jan 2023 11:00:55 +0100
Fix order when reading custom headers in resources.GetRemote
Fixes #10616
Diffstat:
M resources/resource_factories/creat… | 31 +++++++++++++++++++------------
M resources/resource_factories/creat… | 35 +++++++++++++++++++++++++++++++
2 files changed, 54 insertions(+), 12 deletions(-)
---
DIR diff --git 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",
DIR diff --git 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")