Skip to content

Commit

Permalink
Fix order when reading custom headers in resources.GetRemote
Browse files Browse the repository at this point in the history
Fixes #10616
  • Loading branch information
bep committed Jan 16, 2023
1 parent 6e9fa9e commit b5d4850
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 12 deletions.
31 changes: 19 additions & 12 deletions resources/resource_factories/create/remote.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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",
Expand Down
35 changes: 35 additions & 0 deletions resources/resource_factories/create/remote_test.go
Expand Up @@ -20,6 +20,8 @@ import (
)

func TestDecodeRemoteOptions(t *testing.T) {
t.Parallel()

c := qt.New(t)

for _, test := range []struct {
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit b5d4850

Please sign in to comment.