From 59b80a526b16a880495d83bcc04f1a0bab0bd748 Mon Sep 17 00:00:00 2001 From: Mark McDonnell Date: Thu, 25 Mar 2021 16:34:32 +0000 Subject: [PATCH] Initialise Headers map to avoid runtime panic when purging. (#267) * Initialise Headers map to avoid runtime panic when purging. * tweak test comment * Use project idioms. --- fastly/purge.go | 9 ++++++--- fastly/purge_test.go | 24 +++++++++++++++++++++++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/fastly/purge.go b/fastly/purge.go index 1243626a1..063417846 100644 --- a/fastly/purge.go +++ b/fastly/purge.go @@ -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) diff --git a/fastly/purge_test.go b/fastly/purge_test.go index 7e9fdb75e..b4166a9e3 100644 --- a/fastly/purge_test.go +++ b/fastly/purge_test.go @@ -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) {