-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
trace_test.go
66 lines (53 loc) · 1.51 KB
/
trace_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
package rest
import (
"io"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestTraceNoID(t *testing.T) {
ts := httptest.NewServer(Trace(getTestHandlerBlah()))
defer ts.Close()
res, err := http.Get(ts.URL + "/something")
assert.NoError(t, err)
if res != nil {
defer func() { _ = res.Body.Close() }()
}
b, err := io.ReadAll(res.Body)
assert.NoError(t, err)
assert.Equal(t, 200, res.StatusCode)
assert.Equal(t, "blah", string(b))
traceHeader := res.Header.Get("X-Request-ID")
t.Logf("headers - %+v", res.Header)
assert.True(t, traceHeader != "", "non-empty header")
}
func TestTraceWithID(t *testing.T) {
handler := func() http.HandlerFunc {
fn := func(rw http.ResponseWriter, req *http.Request) {
traceID := GetTraceID(req)
assert.Equal(t, "123456", traceID)
_, _ = rw.Write([]byte("blah"))
}
return fn
}
ts := httptest.NewServer(Trace(handler()))
defer ts.Close()
client := http.Client{Timeout: 5 * time.Second}
req, err := http.NewRequest("GET", ts.URL+"/something", http.NoBody)
assert.NoError(t, err)
req.Header.Add("X-Request-Id", "123456")
res, err := client.Do(req)
assert.NoError(t, err)
if res != nil {
defer func() { _ = res.Body.Close() }()
}
b, err := io.ReadAll(res.Body)
assert.NoError(t, err)
assert.Equal(t, 200, res.StatusCode)
assert.Equal(t, "blah", string(b))
traceHeader := res.Header.Get("X-Request-ID")
t.Logf("headers - %+v", res.Header)
assert.Equal(t, "123456", traceHeader, "passing original trace-id")
}