From 092f37a6365589eef06ecff3e3a7c1dbd1487001 Mon Sep 17 00:00:00 2001 From: David Wertenteil Date: Wed, 3 May 2023 08:39:59 +0300 Subject: [PATCH 1/2] if the response is empty, return an empty string Signed-off-by: David Wertenteil --- core/cautils/getter/utils.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/core/cautils/getter/utils.go b/core/cautils/getter/utils.go index 6e12d21d..59d5dd8f 100644 --- a/core/cautils/getter/utils.go +++ b/core/cautils/getter/utils.go @@ -67,6 +67,12 @@ func errAuth(resp *http.Response) error { } func readString(rdr io.Reader, sizeHint int64) (string, error) { + + // if the response is empty, return an empty string + if sizeHint < 0 { + return "", nil + } + var b strings.Builder b.Grow(int(sizeHint)) From b805f22038eeb20e38b0172017b646521613ae37 Mon Sep 17 00:00:00 2001 From: David Wertenteil Date: Wed, 3 May 2023 08:58:12 +0300 Subject: [PATCH 2/2] add test Signed-off-by: David Wertenteil --- core/cautils/getter/utils_test.go | 54 +++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/core/cautils/getter/utils_test.go b/core/cautils/getter/utils_test.go index ece904a1..e40dd662 100644 --- a/core/cautils/getter/utils_test.go +++ b/core/cautils/getter/utils_test.go @@ -1,6 +1,7 @@ package getter import ( + "io" "testing" "github.com/stretchr/testify/require" @@ -43,3 +44,56 @@ func TestIsNativeFramework(t *testing.T) { require.Truef(t, isNativeFramework("nSa"), "expected nsa to be native (case insensitive)") require.Falsef(t, isNativeFramework("foo"), "expected framework to be custom") } + +func Test_readString(t *testing.T) { + type args struct { + rdr io.Reader + sizeHint int64 + } + tests := []struct { + name string + args args + want string + wantErr bool + }{ + { + name: "should return empty string if sizeHint is negative", + args: args{ + rdr: nil, + sizeHint: -1, + }, + want: "", + wantErr: false, + }, + { + name: "should return empty string if sizeHint is zero", + args: args{ + rdr: &io.LimitedReader{}, + sizeHint: 0, + }, + want: "", + wantErr: false, + }, + { + name: "should return empty string if sizeHint is positive", + args: args{ + rdr: &io.LimitedReader{}, + sizeHint: 1, + }, + want: "", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := readString(tt.args.rdr, tt.args.sizeHint) + if (err != nil) != tt.wantErr { + t.Errorf("readString() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("readString() = %v, want %v", got, tt.want) + } + }) + } +}