|
1 | 1 | package typesense |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "context" |
| 6 | + "io" |
5 | 7 | "net/http" |
6 | 8 | "net/http/httptest" |
7 | 9 | "testing" |
@@ -392,3 +394,51 @@ func TestApiCallCanAbortRequest(t *testing.T) { |
392 | 394 | assert.Nil(t, res) |
393 | 395 | assert.Equal(t, requestURLHistory, serverURLs[:3]) |
394 | 396 | } |
| 397 | + |
| 398 | +func TestApiCallRetryWithRequestBody(t *testing.T) { |
| 399 | + |
| 400 | + requestURLHistory := make([]string, 0, 2) |
| 401 | + |
| 402 | + servers, serverURLs := instantiateServers([]serverHandler{ |
| 403 | + func(w http.ResponseWriter, r *http.Request) { |
| 404 | + appendHistory(&requestURLHistory, r) |
| 405 | + w.WriteHeader(501) |
| 406 | + }, |
| 407 | + func(w http.ResponseWriter, r *http.Request) { |
| 408 | + appendHistory(&requestURLHistory, r) |
| 409 | + data := r.Body |
| 410 | + bodyBytes, _ := io.ReadAll(data) |
| 411 | + assert.Equal(t, string(bodyBytes), "body data") |
| 412 | + w.WriteHeader(201) |
| 413 | + }, |
| 414 | + func(w http.ResponseWriter, r *http.Request) { |
| 415 | + appendHistory(&requestURLHistory, r) |
| 416 | + data := r.Body |
| 417 | + bodyBytes, _ := io.ReadAll(data) |
| 418 | + assert.Equal(t, string(bodyBytes), "body data") |
| 419 | + w.WriteHeader(203) |
| 420 | + }, |
| 421 | + }) |
| 422 | + for _, server := range servers { |
| 423 | + defer server.Close() |
| 424 | + } |
| 425 | + |
| 426 | + apiCall := newAPICall( |
| 427 | + &ClientConfig{ |
| 428 | + Nodes: serverURLs, |
| 429 | + ConnectionTimeout: 5 * time.Second, |
| 430 | + }, |
| 431 | + ) |
| 432 | + req, err := http.NewRequest(http.MethodPost, "http://example.com", bytes.NewBuffer([]byte("body data"))) |
| 433 | + assert.NoError(t, err) |
| 434 | + |
| 435 | + res, err := apiCall.Do(req) |
| 436 | + assert.NoError(t, err) |
| 437 | + assert.Equal(t, 201, res.StatusCode) |
| 438 | + |
| 439 | + res2, err2 := apiCall.Do(req) |
| 440 | + assert.NoError(t, err2) |
| 441 | + assert.Equal(t, 203, res2.StatusCode) |
| 442 | + |
| 443 | + assert.Equal(t, serverURLs, requestURLHistory) |
| 444 | +} |
0 commit comments