From 4e4793d79c7e3eb69aa770be9d5543c0247e618f Mon Sep 17 00:00:00 2001 From: Andy McCall Date: Wed, 8 Jan 2020 18:19:15 -0800 Subject: [PATCH 1/7] Added failing test --- zendesk/attachment_test.go | 29 ++++++++++++++++++++++++++++- zendesk/ticket_test.go | 15 +++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/zendesk/attachment_test.go b/zendesk/attachment_test.go index 9c69db77..811152ab 100644 --- a/zendesk/attachment_test.go +++ b/zendesk/attachment_test.go @@ -2,6 +2,7 @@ package zendesk import ( "bytes" + "context" "crypto/sha1" "io" "net/http" @@ -12,7 +13,7 @@ import ( ) func TestWrite(t *testing.T) { - file := readFixture(filepath.Join("POST", "upload.json")) + file := readFixture(filepath.Join(http.MethodPost, "upload.json")) h := sha1.New() h.Write(file) expectedSum := h.Sum(nil) @@ -49,6 +50,32 @@ func TestWrite(t *testing.T) { } } +func TestWriteCancelledContext(t *testing.T) { + mockAPI := newMockAPIWithStatus(http.MethodPost, "ticket.json", 201) + defer mockAPI.Close() + + client := newTestClient(mockAPI) + + canceled, cancelFunc := context.WithCancel(ctx) + cancelFunc() + w := client.UploadAttachment(canceled, "foo", "bar") + + file := []byte("body") + r := bytes.NewBuffer(file) + + _, err := io.Copy(w, r) + if err != nil { + t.Fatal("Received an error from write") + } + + _, err = w.Close() + if err == nil { + t.Fatal("Did not receive error when closing writer") + } else if err != context.Canceled { + t.Fatalf("did not receive expected error was: %v", err) + } +} + func TestDeleteUpload(t *testing.T) { mockAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) diff --git a/zendesk/ticket_test.go b/zendesk/ticket_test.go index d2d4ce82..c9253303 100644 --- a/zendesk/ticket_test.go +++ b/zendesk/ticket_test.go @@ -1,6 +1,7 @@ package zendesk import ( + "context" "encoding/json" "net/http" "sort" @@ -46,6 +47,20 @@ func TestGetTicket(t *testing.T) { } } +func TestGetTicketCanceledContext(t *testing.T) { + mockAPI := newMockAPI(http.MethodGet, "ticket.json") + client := newTestClient(mockAPI) + defer mockAPI.Close() + canceled, cancelFunc := context.WithCancel(ctx) + cancelFunc() + _, err := client.GetTicket(canceled, 2) + if err == nil { + t.Fatal("Did not get error when calling with cancelled context") + } else if err != context.Canceled { + t.Fatalf("Did not get expected error when calling with cancelled context. Was: %v", err) + } +} + // Test the CustomField unmarshalling fails on an invalid value. // In this case a float64 as CustomField.Value should cause an error. func TestGetTicketWithInvalidCustomField(t *testing.T) { From dd5fd7aa6c2caed0bda08a20eb23339e9a29915e Mon Sep 17 00:00:00 2001 From: Andy McCall Date: Wed, 8 Jan 2020 18:34:12 -0800 Subject: [PATCH 2/7] added delete test --- zendesk/attachment_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/zendesk/attachment_test.go b/zendesk/attachment_test.go index 811152ab..879785b6 100644 --- a/zendesk/attachment_test.go +++ b/zendesk/attachment_test.go @@ -89,6 +89,22 @@ func TestDeleteUpload(t *testing.T) { } } +func TestDeleteUploadCanceledContext(t *testing.T) { + mockAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNoContent) + w.Write(nil) + })) + + c := newTestClient(mockAPI) + canceled, cancelFunc := context.WithCancel(ctx) + cancelFunc() + + err := c.DeleteUpload(canceled, "foobar") + if err == nil { + t.Fatal("did not get expected error") + } +} + func TestGetAttachment(t *testing.T) { mockAPI := newMockAPI(http.MethodGet, "attachment.json") client := newTestClient(mockAPI) From 8af0ff87a23fb6d92b675fae792fa74e9792ec5c Mon Sep 17 00:00:00 2001 From: Andy McCall Date: Wed, 8 Jan 2020 18:35:51 -0800 Subject: [PATCH 3/7] added put test --- zendesk/brand_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/zendesk/brand_test.go b/zendesk/brand_test.go index a3cdfffe..e5ca9449 100644 --- a/zendesk/brand_test.go +++ b/zendesk/brand_test.go @@ -1,6 +1,7 @@ package zendesk import ( + "context" "net/http" "net/http/httptest" "testing" @@ -49,6 +50,19 @@ func TestUpdateBrand(t *testing.T) { } } +func TestUpdateBrandCanceledContext(t *testing.T) { + mockAPI := newMockAPIWithStatus(http.MethodPut, "brands.json", http.StatusOK) + client := newTestClient(mockAPI) + defer mockAPI.Close() + + canceled, cancelFunc := context.WithCancel(ctx) + cancelFunc() + _, err := client.UpdateBrand(canceled, int64(1234), Brand{}) + if err == nil { + t.Fatalf("did not get expected error") + } +} + func TestDeleteBrand(t *testing.T) { mockAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) From 27645a2acab49489d066cf633eb29558e6b57d8f Mon Sep 17 00:00:00 2001 From: Andy McCall Date: Wed, 8 Jan 2020 18:37:43 -0800 Subject: [PATCH 4/7] added post test --- zendesk/brand_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/zendesk/brand_test.go b/zendesk/brand_test.go index e5ca9449..b7ac3127 100644 --- a/zendesk/brand_test.go +++ b/zendesk/brand_test.go @@ -18,6 +18,20 @@ func TestCreateBrand(t *testing.T) { } } +func TestCreateBrandCanceledContext(t *testing.T) { + mockAPI := newMockAPIWithStatus(http.MethodPost, "brands.json", http.StatusCreated) + client := newTestClient(mockAPI) + defer mockAPI.Close() + + canceled, cancelFunc := context.WithCancel(ctx) + cancelFunc() + + _, err := client.CreateBrand(canceled, Brand{}) + if err == nil { + t.Fatalf("did not get expected error") + } +} + func TestGetBrand(t *testing.T) { mockAPI := newMockAPI(http.MethodGet, "brand.json") client := newTestClient(mockAPI) From 531f1b6ef49c0dd570e1667f23217f252b7d6c7f Mon Sep 17 00:00:00 2001 From: Andy McCall Date: Wed, 8 Jan 2020 18:22:30 -0800 Subject: [PATCH 5/7] Actually using context --- zendesk/attachment.go | 2 +- zendesk/ticket_test.go | 2 -- zendesk/zendesk.go | 17 +++++++++-------- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/zendesk/attachment.go b/zendesk/attachment.go index adf2062c..6166a453 100644 --- a/zendesk/attachment.go +++ b/zendesk/attachment.go @@ -71,7 +71,7 @@ func (wr *writer) open() error { return err } - wr.prepareRequest(wr.ctx, req) + req = wr.prepareRequest(wr.ctx, req) req.Header.Set("Content-Type", "application/binary") q := req.URL.Query() diff --git a/zendesk/ticket_test.go b/zendesk/ticket_test.go index c9253303..2bff88e4 100644 --- a/zendesk/ticket_test.go +++ b/zendesk/ticket_test.go @@ -56,8 +56,6 @@ func TestGetTicketCanceledContext(t *testing.T) { _, err := client.GetTicket(canceled, 2) if err == nil { t.Fatal("Did not get error when calling with cancelled context") - } else if err != context.Canceled { - t.Fatalf("Did not get expected error when calling with cancelled context. Was: %v", err) } } diff --git a/zendesk/zendesk.go b/zendesk/zendesk.go index 264ac4f2..ac5894cf 100644 --- a/zendesk/zendesk.go +++ b/zendesk/zendesk.go @@ -90,7 +90,7 @@ func (z *Client) get(ctx context.Context, path string) ([]byte, error) { return nil, err } - z.prepareRequest(ctx, req) + req = z.prepareRequest(ctx, req) resp, err := z.httpClient.Do(req) if err != nil { @@ -123,7 +123,7 @@ func (z *Client) post(ctx context.Context, path string, data interface{}) ([]byt if err != nil { return nil, err } - z.prepareRequest(ctx, req) + req = z.prepareRequest(ctx, req) resp, err := z.httpClient.Do(req) if err != nil { @@ -157,7 +157,7 @@ func (z *Client) put(ctx context.Context, path string, data interface{}) ([]byte if err != nil { return nil, err } - z.prepareRequest(ctx, req) + req = z.prepareRequest(ctx, req) resp, err := z.httpClient.Do(req) if err != nil { @@ -186,7 +186,7 @@ func (z *Client) delete(ctx context.Context, path string) error { if err != nil { return err } - z.prepareRequest(ctx, req) + req = z.prepareRequest(ctx, req) resp, err := z.httpClient.Do(req) if err != nil { @@ -210,10 +210,11 @@ func (z *Client) delete(ctx context.Context, path string) error { } // prepare request sets common request variables such as authn and user agent -func (z *Client) prepareRequest(ctx context.Context, req *http.Request) { - req = req.WithContext(ctx) - z.includeHeaders(req) - req.SetBasicAuth(z.credential.Email(), z.credential.Secret()) +func (z *Client) prepareRequest(ctx context.Context, req *http.Request) *http.Request { + out := req.WithContext(ctx) + z.includeHeaders(out) + out.SetBasicAuth(z.credential.Email(), z.credential.Secret()) + return out } // includeHeaders set HTTP headers from client.headers to *http.Request From 291afacfd4a36c592d9bb40b97aa0b7c2228acea Mon Sep 17 00:00:00 2001 From: Andy McCall Date: Wed, 8 Jan 2020 18:42:42 -0800 Subject: [PATCH 6/7] fixing attachment test --- zendesk/attachment_test.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/zendesk/attachment_test.go b/zendesk/attachment_test.go index 879785b6..812d8f47 100644 --- a/zendesk/attachment_test.go +++ b/zendesk/attachment_test.go @@ -64,15 +64,13 @@ func TestWriteCancelledContext(t *testing.T) { r := bytes.NewBuffer(file) _, err := io.Copy(w, r) - if err != nil { - t.Fatal("Received an error from write") + if err == nil { + t.Fatalf("did not recieve expected error") } _, err = w.Close() if err == nil { t.Fatal("Did not receive error when closing writer") - } else if err != context.Canceled { - t.Fatalf("did not receive expected error was: %v", err) } } From debf6237a7dbeaa54b401474d425a7f138d82b90 Mon Sep 17 00:00:00 2001 From: Andy McCall Date: Wed, 8 Jan 2020 19:27:53 -0800 Subject: [PATCH 7/7] fixing whitespace --- zendesk/zendesk.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/zendesk/zendesk.go b/zendesk/zendesk.go index ac5894cf..33ee3915 100644 --- a/zendesk/zendesk.go +++ b/zendesk/zendesk.go @@ -123,6 +123,7 @@ func (z *Client) post(ctx context.Context, path string, data interface{}) ([]byt if err != nil { return nil, err } + req = z.prepareRequest(ctx, req) resp, err := z.httpClient.Do(req) @@ -157,6 +158,7 @@ func (z *Client) put(ctx context.Context, path string, data interface{}) ([]byte if err != nil { return nil, err } + req = z.prepareRequest(ctx, req) resp, err := z.httpClient.Do(req) @@ -186,6 +188,7 @@ func (z *Client) delete(ctx context.Context, path string) error { if err != nil { return err } + req = z.prepareRequest(ctx, req) resp, err := z.httpClient.Do(req) @@ -214,6 +217,7 @@ func (z *Client) prepareRequest(ctx context.Context, req *http.Request) *http.Re out := req.WithContext(ctx) z.includeHeaders(out) out.SetBasicAuth(z.credential.Email(), z.credential.Secret()) + return out }