-
Notifications
You must be signed in to change notification settings - Fork 627
/
Copy pathestransport_integration_test.go
100 lines (77 loc) · 2.15 KB
/
estransport_integration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Licensed to Elasticsearch B.V. under one or more agreements.
// Elasticsearch B.V. licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
// +build integration
package estransport_test
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/elastic/go-elasticsearch/v6/estransport"
"github.com/elastic/go-elasticsearch/v6/esutil"
)
var (
_ = fmt.Print
)
func TestTransportRetries(t *testing.T) {
var counter int
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
counter++
body, _ := ioutil.ReadAll(r.Body)
fmt.Println("req.Body:", string(body))
http.Error(w, "FAKE 502", http.StatusBadGateway)
}))
serverURL, _ := url.Parse(server.URL)
transport, _ := estransport.New(estransport.Config{URLs: []*url.URL{serverURL}})
bodies := []io.Reader{
strings.NewReader(`FAKE`),
esutil.NewJSONReader(`FAKE`),
}
for _, body := range bodies {
t.Run(fmt.Sprintf("Reset the %T request body", body), func(t *testing.T) {
counter = 0
req, err := http.NewRequest("GET", "/", body)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
res, err := transport.Perform(req)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
body, _ := ioutil.ReadAll(res.Body)
fmt.Println("> GET", req.URL)
fmt.Printf("< %s (tries: %d)\n", bytes.TrimSpace(body), counter)
if counter != 3 {
t.Errorf("Unexpected number of retries, want=3, got=%d", counter)
}
})
}
}
func TestTransportHeaders(t *testing.T) {
u, _ := url.Parse("http://localhost:9200")
hdr := http.Header{}
hdr.Set("Accept", "application/yaml")
tp, _ := estransport.New(estransport.Config{
URLs: []*url.URL{u},
Header: hdr,
})
req, _ := http.NewRequest("GET", "/", nil)
res, err := tp.Perform(req)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
if !bytes.HasPrefix(body, []byte("---")) {
t.Errorf("Unexpected response body:\n%s", body)
}
}