From 146351131b6bc70b4ce09d7454bcf31638da9d20 Mon Sep 17 00:00:00 2001 From: nobe4 Date: Sun, 19 May 2024 16:13:01 +0200 Subject: [PATCH 1/2] test: create case showcasing that a 205 breaks DoWithContext A 205 must not return any content, so DoWithContext should handle this case and not fail when trying to parse the JSON. Ref: https://httpwg.org/specs/rfc9110.html#status.205 --- pkg/api/rest_client_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pkg/api/rest_client_test.go b/pkg/api/rest_client_test.go index edbb9d2..c36d0c6 100644 --- a/pkg/api/rest_client_test.go +++ b/pkg/api/rest_client_test.go @@ -303,6 +303,24 @@ func TestRESTClientPatch(t *testing.T) { assert.True(t, gock.IsDone(), printPendingMocks(gock.Pending())) } +func TestRESTClientPatchStatus205(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 dcd25bb2a91f7398c1a024ae49c1fd62539fa5da Mon Sep 17 00:00:00 2001 From: nobe4 Date: Sun, 19 May 2024 16:13:59 +0200 Subject: [PATCH 2/2] fix: implement the fix that resolve the 205 miss-handling Similar to the check for 204. --- pkg/api/rest_client.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/api/rest_client.go b/pkg/api/rest_client.go index 2d91f70..fe1d3a9 100644 --- a/pkg/api/rest_client.go +++ b/pkg/api/rest_client.go @@ -98,6 +98,11 @@ func (c *RESTClient) DoWithContext(ctx context.Context, method string, path stri if resp.StatusCode == http.StatusNoContent { return nil } + + if resp.StatusCode == http.StatusResetContent { + return nil + } + defer resp.Body.Close() b, err := io.ReadAll(resp.Body)