From 889299c3b924666faa56c3bc6683e73eb31f7b2f Mon Sep 17 00:00:00 2001 From: Strift Date: Tue, 18 Feb 2025 15:18:15 +0800 Subject: [PATCH] add coverage for added code --- client_test.go | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/client_test.go b/client_test.go index 3e4f40a7..5bc5a774 100644 --- a/client_test.go +++ b/client_test.go @@ -5,13 +5,15 @@ import ( "context" "encoding/json" "errors" - "github.com/stretchr/testify/require" + "fmt" "io" "net/http" "net/http/httptest" "strings" "testing" "time" + + "github.com/stretchr/testify/require" ) // Mock structures for testing @@ -106,6 +108,30 @@ func TestExecuteRequest(t *testing.T) { } else if r.URL.Path == "/io-reader" { w.WriteHeader(http.StatusOK) _, _ = w.Write([]byte(`{"message":"io reader"}`)) + } else if r.URL.Path == "/io-reader-encoding" { + ce := r.Header.Get("Content-Encoding") + if ce == "" { + w.WriteHeader(http.StatusBadRequest) + _, _ = w.Write([]byte("missing Content-Encoding header")) + return + } + + // Read the raw request body + body, err := io.ReadAll(r.Body) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + _, _ = w.Write([]byte(fmt.Sprintf("failed to read body: %v", err))) + return + } + + // For io.Reader, the client sends the raw data without encoding + // So we just need to compare it directly + if string(body) == "test data" { + w.WriteHeader(http.StatusOK) + } else { + w.WriteHeader(http.StatusBadRequest) + _, _ = w.Write([]byte(fmt.Sprintf("got: %s, want: test data", string(body)))) + } } else if r.URL.Path == "/failed-retry" { w.WriteHeader(http.StatusBadGateway) } else if r.URL.Path == "/success-retry" { @@ -422,6 +448,19 @@ func TestExecuteRequest(t *testing.T) { withTimeout: true, wantErr: true, }, + { + name: "Test request encoding with io.Reader", + internalReq: &internalRequest{ + endpoint: "/io-reader-encoding", + method: http.MethodPost, + contentType: "text/plain", + withRequest: strings.NewReader("test data"), + acceptedStatusCodes: []int{http.StatusOK}, + }, + expectedResp: nil, + contentEncoding: GzipEncoding, + wantErr: false, + }, } for _, tt := range tests {