From 8bbb45752a765fded5485e1cc41d586ca8505d8b Mon Sep 17 00:00:00 2001 From: Mark McDonnell Date: Thu, 25 Mar 2021 14:10:19 +0000 Subject: [PATCH 1/3] Initialise Headers map to avoid runtime panic when purging. --- fastly/purge.go | 1 + fastly/purge_test.go | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/fastly/purge.go b/fastly/purge.go index 1243626a1..a9d31d052 100644 --- a/fastly/purge.go +++ b/fastly/purge.go @@ -29,6 +29,7 @@ func (c *Client) Purge(i *PurgeInput) (*Purge, error) { ro := new(RequestOptions) ro.Parallel = true if i.Soft { + ro.Headers = make(map[string]string) ro.Headers["Fastly-Soft-Purge"] = "1" } diff --git a/fastly/purge_test.go b/fastly/purge_test.go index 7e9fdb75e..a0cd77086 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 struct) 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) { From 121839d69955d18af59af4da8e27ca9029846d04 Mon Sep 17 00:00:00 2001 From: Mark McDonnell Date: Thu, 25 Mar 2021 14:17:42 +0000 Subject: [PATCH 2/3] tweak test comment --- fastly/purge_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fastly/purge_test.go b/fastly/purge_test.go index a0cd77086..b4166a9e3 100644 --- a/fastly/purge_test.go +++ b/fastly/purge_test.go @@ -7,8 +7,8 @@ import ( // 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 struct) doesn't -// cause `panic: assignment to entry in nil map`. +// (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() From 2c34e63150103eb839816be925ea06a6c9d7dccd Mon Sep 17 00:00:00 2001 From: Mark McDonnell Date: Thu, 25 Mar 2021 16:33:20 +0000 Subject: [PATCH 3/3] Use project idioms. --- fastly/purge.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/fastly/purge.go b/fastly/purge.go index a9d31d052..063417846 100644 --- a/fastly/purge.go +++ b/fastly/purge.go @@ -26,11 +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 = make(map[string]string) - ro.Headers["Fastly-Soft-Purge"] = "1" + ro.Headers = map[string]string{ + "Fastly-Soft-Purge": "1", + } } resp, err := c.Post("purge/"+i.URL, ro)