-
Notifications
You must be signed in to change notification settings - Fork 6
/
middleware_test.go
58 lines (46 loc) · 1.29 KB
/
middleware_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
package tracing
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"context"
opentracing "github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
func TestMiddleware(t *testing.T) {
assert := assert.New(t)
ts := httptest.NewServer(HTTPMiddleware("test-get", http.HandlerFunc(getTestHandler)))
defer ts.Close()
var u bytes.Buffer
u.WriteString(string(ts.URL))
res, err := http.Get(u.String())
assert.NoError(err)
if res != nil {
defer res.Body.Close()
}
_, err = ioutil.ReadAll(res.Body)
assert.NoError(err)
assert.Equal(res.StatusCode, 200, "Span should be in the context")
}
func getTestHandler(rw http.ResponseWriter, req *http.Request) {
if span := opentracing.SpanFromContext(req.Context()); span != nil {
fmt.Fprint(rw, "span ok")
}
http.Error(rw, "SPAN_NOT_FOUND", 404)
}
func TestGoKitEndpointMiddleware(t *testing.T) {
assert := assert.New(t)
endpoint := func(ctx context.Context, request interface{}) (response interface{}, err error) {
if span := opentracing.SpanFromContext(ctx); span == nil {
return nil, errors.New("span not found")
}
return nil, nil
}
endpoint = GotKitEndpointMiddleWare("span test")(endpoint)
_, err := endpoint(context.Background(), nil)
assert.NoError(err)
}