/
handler_test.go
51 lines (42 loc) · 1015 Bytes
/
handler_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
package main
import (
"github.com/d-smith/docker-build/customctx"
"github.com/d-smith/docker-build/customctx/reqid"
"github.com/d-smith/docker-build/customctx/timing"
"golang.org/x/net/context"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestWithXRequestID(t *testing.T) {
h := &customctx.ContextAdapter{
Ctx: context.Background(),
Handler: timing.TimerMiddleware(reqid.RequestIdMiddleware(customctx.ContextHandlerFunc(handler))),
}
ts := httptest.NewServer(h)
defer ts.Close()
req, err := http.NewRequest("GET", ts.URL, nil)
if err != nil {
log.Fatal(err.Error())
}
req.Header.Set("X-Request-ID", "request-id")
client := http.Client{}
var resp *http.Response
for i := 0; i < 1; i++ {
resp, err = client.Do(req)
if err != nil {
log.Fatal(err.Error())
}
}
body, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
log.Fatal(err.Error())
}
if !strings.Contains(string(body), "request-id") {
t.Fail()
}
}