/
transport_test.go
118 lines (107 loc) · 3.41 KB
/
transport_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package opentracing
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/mocktracer"
"github.com/olivere/elastic/v7"
)
func TestTransport(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
username, password, ok := r.BasicAuth()
if !ok || username != "alice" || password != "secret" {
w.WriteHeader(http.StatusForbidden)
return
}
switch r.URL.Path {
case "/":
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, `{
"name" : "Qg28M36",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "rwHa7BBnRC2h8KoDfCbmuQ",
"version" : {
"number" : "6.3.2",
"build_flavor" : "oss",
"build_type" : "tar",
"build_hash" : "053779d",
"build_date" : "2018-07-20T05:20:23.451332Z",
"build_snapshot" : false,
"lucene_version" : "7.3.1",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}`)
return
default:
w.WriteHeader(http.StatusInternalServerError)
return
}
}))
defer ts.Close()
// Mock tracer
tracer := mocktracer.New()
opentracing.InitGlobalTracer(tracer)
// Setup a simple transport
tr := NewTransport()
httpClient := &http.Client{
Transport: tr,
}
// Create a simple Ping request via Elastic
client, err := elastic.NewClient(
elastic.SetURL(ts.URL),
elastic.SetHttpClient(httpClient),
elastic.SetHealthcheck(false),
elastic.SetSniff(false),
elastic.SetBasicAuth("alice", "secret"),
)
if err != nil {
t.Fatal(err)
}
res, code, err := client.Ping(ts.URL).Do(context.Background())
if err != nil {
t.Fatal(err)
}
if want, have := http.StatusOK, code; want != have {
t.Fatalf("want Status=%d, have %d", want, have)
}
if want, have := "You Know, for Search", res.TagLine; want != have {
t.Fatalf("want TagLine=%q, have %q", want, have)
}
// Check the data written into tracer
spans := tracer.FinishedSpans()
if want, have := 1, len(spans); want != have {
t.Fatalf("want %d finished spans, have %d", want, have)
}
span := spans[0]
if want, have := "PerformRequest", span.OperationName; want != have {
t.Fatalf("want Span.OperationName=%q, have %q", want, have)
}
if want, have := "github.com/olivere/elastic/v7", span.Tag("component"); want != have {
t.Fatalf("want component tag=%q, have %q", want, have)
}
httpURL, ok := span.Tag("http.url").(string)
if !ok || httpURL == "" {
t.Fatalf("want http.url tag=%q to be a non-empty string (found type %T)", "http.url", span.Tag("http.url"))
}
if want, have := ts.URL+"/", httpURL; want != have {
t.Fatalf("want http.url tag=%q, have %q", want, have)
}
if strings.Contains(httpURL, "alice") || strings.Contains(httpURL, "password") {
t.Fatalf("want http.url tag %q to not contain username and/or password: %s", "URL", span.Tag("http.url"))
}
if want, have := "GET", span.Tag("http.method"); want != have {
t.Fatalf("want http.method tag=%q, have %q", want, have)
}
if want, have := uint16(http.StatusOK), span.Tag("http.status_code"); want != have {
t.Fatalf("want http.status_code tag=%v (%T), have %v (%T)", want, want, have, have)
}
}