Skip to content

Commit 684e058

Browse files
f-dgAnaethelion
andauthored
Reties With not empty URL Path (#657)
* Reties With not empty URL Path transport builds an incorrect request path if the address is configured with a path, for example: ``` eCnf := elasticsearch.Config{ Addresses: []string{ "https://server.com/somepath" }, } ``` it happens when retries is enabled and the API returns an error, transports adds to the request "/somepath" on each retry instead of building a new path. * Add comment to reset of original path * Update test to check url after retry * update variable name following linter recommendations --------- Co-authored-by: Laurent Saint-Félix <[email protected]>
1 parent ea78361 commit 684e058

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

estransport/estransport.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,8 @@ func (c *Client) Perform(req *http.Request) (*http.Response, error) {
327327
}
328328
}
329329
}
330-
330+
331+
originalPath := req.URL.Path
331332
for i := 0; i <= c.maxRetries; i++ {
332333
var (
333334
conn *Connection
@@ -447,6 +448,10 @@ func (c *Client) Perform(req *http.Request) (*http.Response, error) {
447448
break
448449
}
449450
}
451+
452+
// Re-init the path of the request to its original state
453+
// This will be re-enriched by the connection upon retry
454+
req.URL.Path = originalPath
450455
}
451456

452457
// TODO(karmi): Wrap error

estransport/estransport_internal_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,10 @@ func TestTransportPerformRetries(t *testing.T) {
657657

658658
t.Run("Reset request body during retry", func(t *testing.T) {
659659
var bodies []string
660-
u, _ := url.Parse("https://foo.com/bar")
660+
esURL := "https://foo.com/bar"
661+
endpoint := "/abc"
662+
663+
u, _ := url.Parse(esURL)
661664
tp, _ := New(Config{
662665
URLs: []*url.URL{u},
663666
Transport: &mockTransp{
@@ -666,6 +669,10 @@ func TestTransportPerformRetries(t *testing.T) {
666669
if err != nil {
667670
panic(err)
668671
}
672+
expectedURL := strings.Join([]string{esURL, endpoint}, "")
673+
if !strings.EqualFold(req.URL.String(), expectedURL) {
674+
t.Fatalf("expected request url to be %s, got: %s", expectedURL, req.URL.String())
675+
}
669676
bodies = append(bodies, string(body))
670677
return &http.Response{Status: "MOCK", StatusCode: 502}, nil
671678
},

0 commit comments

Comments
 (0)