forked from xmidt-org/golang-money
-
Notifications
You must be signed in to change notification settings - Fork 1
/
spanner_test.go
61 lines (52 loc) · 1.57 KB
/
spanner_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
package money
import (
"context"
"net/http"
"net/http/httptest"
"testing"
)
func TestNewHTTPSpanner(t *testing.T) {
t.Run("DI", testNewHTTPSpannerNil)
t.Run("Start", testStart)
t.Run("DecorationNoMoneyContext", testDecorate)
t.Run("DecorationMoneyContext", testDecorateWithMoney)
}
func testNewHTTPSpannerNil(t *testing.T) {
var spanner *HTTPSpanner
if spanner.Decorate("test", nil) != nil {
t.Error("Decoration should leave handler unchanged")
}
}
func testStart(t *testing.T) {
var spanner = NewHTTPSpanner()
if spanner.Start(context.Background(), Span{}) == nil {
t.Error("was expecting a non-nil response")
}
}
func testDecorate(t *testing.T) {
var spanner = NewHTTPSpanner()
handler := http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
_, ok := TrackerFromContext(r.Context())
if ok {
t.Error("Tracker should not have been injected")
}
})
decorated := spanner.Decorate("test", handler)
decorated.ServeHTTP(nil, httptest.NewRequest("GET", "localhost:9090/test", nil))
}
func testDecorateWithMoney(t *testing.T) {
var spanner = NewHTTPSpanner()
handler := http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
_, ok := TrackerFromContext(r.Context())
if !ok {
t.Error("Expected tracker to be present")
}
})
decorated := spanner.Decorate("test", handler)
inputRequest := httptest.NewRequest("GET", "localhost:9090/test", nil)
inputRequest.Header.Add(MoneyHeader, "trace-id=abc;parent-id=1;span-id=1")
decorated.ServeHTTP(nil, inputRequest)
}
//create a test that simply finishes the tracker that was started