From 20b3d7269930d7f8e56237cd37fa1ce7281bf856 Mon Sep 17 00:00:00 2001 From: nobe4 Date: Sun, 19 May 2024 16:03:40 +0200 Subject: [PATCH 1/2] test: adds a tests that showcases #161 This shows that returning an empty body, with a non-error status code will break DoWithContext because it will try to unmarshal an invalid JSON string. --- pkg/api/rest_client_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pkg/api/rest_client_test.go b/pkg/api/rest_client_test.go index edbb9d2..f5d69f7 100644 --- a/pkg/api/rest_client_test.go +++ b/pkg/api/rest_client_test.go @@ -303,6 +303,25 @@ func TestRESTClientPatch(t *testing.T) { assert.True(t, gock.IsDone(), printPendingMocks(gock.Pending())) } +// Covers https://docs.github.com/en/rest/activity/notifications?apiVersion=2022-11-28#mark-a-thread-as-read +func TestRESTClientPatchNoResponseBody(t *testing.T) { + t.Cleanup(gock.Off) + gock.New("https://api.github.com"). + Patch("/some/path/here"). + BodyString(`{}`). + Reply(205). + BodyString("") + client, _ := NewRESTClient(ClientOptions{ + Host: "github.com", + AuthToken: "token", + Transport: http.DefaultTransport, + }) + r := bytes.NewReader([]byte(`{}`)) + err := client.Patch("some/path/here", r, nil) + assert.NoError(t, err) + assert.True(t, gock.IsDone(), printPendingMocks(gock.Pending())) +} + func TestRESTClientPost(t *testing.T) { t.Cleanup(gock.Off) gock.New("https://api.github.com"). From 702451a9b59dbda503bfd972aea0e06d9bf5acad Mon Sep 17 00:00:00 2001 From: nobe4 Date: Sun, 19 May 2024 16:06:10 +0200 Subject: [PATCH 2/2] fix: add check for empty response body This ensures that an empty response body, which is valid and present in some endpoints. --- pkg/api/rest_client.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pkg/api/rest_client.go b/pkg/api/rest_client.go index 2d91f70..61e1386 100644 --- a/pkg/api/rest_client.go +++ b/pkg/api/rest_client.go @@ -105,9 +105,11 @@ func (c *RESTClient) DoWithContext(ctx context.Context, method string, path stri return err } - err = json.Unmarshal(b, &response) - if err != nil { - return err + if len(b) > 0 { + err = json.Unmarshal(b, &response) + if err != nil { + return err + } } return nil