Skip to content

Commit

Permalink
Initialise Headers map to avoid runtime panic when purging. (#267)
Browse files Browse the repository at this point in the history
* Initialise Headers map to avoid runtime panic when purging.

* tweak test comment

* Use project idioms.
  • Loading branch information
Integralist authored Mar 25, 2021
1 parent ab8f494 commit 59b80a5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
9 changes: 6 additions & 3 deletions fastly/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ func (c *Client) Purge(i *PurgeInput) (*Purge, error) {
return nil, ErrMissingURL
}

ro := new(RequestOptions)
ro.Parallel = true
ro := &RequestOptions{
Parallel: true,
}
if i.Soft {
ro.Headers["Fastly-Soft-Purge"] = "1"
ro.Headers = map[string]string{
"Fastly-Soft-Purge": "1",
}
}

resp, err := c.Post("purge/"+i.URL, ro)
Expand Down
24 changes: 23 additions & 1 deletion fastly/purge_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
package fastly

import "testing"
import (
"testing"
)

// TestClient_Purge validates no runtime panics are raised by the Purge method.
//
// Specifically, we're ensuring that the setting of the `Soft` key to `true`
// (which will require assigning a header to the RequestOptions.Headers field)
// doesn't cause `panic: assignment to entry in nil map`.
func TestClient_Purge(t *testing.T) {
t.Parallel()

client := DefaultClient()
url := "http://gofastly.fastly-testing.com/foo/bar"

_, err := client.Purge(&PurgeInput{
URL: url,
Soft: true,
})
if err == nil {
t.Fatalf("we've accidentally purged a real URL: %s", url)
}
}

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

var err error
var purge *Purge
record(t, "purges/purge_by_key", func(c *Client) {
Expand Down

0 comments on commit 59b80a5

Please sign in to comment.