Skip to content

Commit

Permalink
Merge pull request #610 from meilisearch/fix/code-coverage
Browse files Browse the repository at this point in the history
add coverage for added code
  • Loading branch information
Strift authored Feb 18, 2025
2 parents 5cdae38 + 889299c commit 0336c0c
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 0336c0c

Please sign in to comment.