-
Notifications
You must be signed in to change notification settings - Fork 0
/
resty_test.go
144 lines (128 loc) · 3.82 KB
/
resty_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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package xopresty_test
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/xoplog/xop-go"
"github.com/xoplog/xop-go/xopmiddle"
"github.com/xoplog/xop-go/xopnum"
"github.com/xoplog/xop-go/xoprecorder"
"github.com/xoplog/xop-go/xoptest"
"github.com/xoplog/xopresty-go"
"github.com/muir/resty"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type exampleRequest struct {
Name string
Count int
}
type exampleResult struct {
Score float64
Comment string
}
var cases = []struct {
name string
clientMod func(*resty.Client) *resty.Client
requestMod func(*resty.Request) *resty.Request
handler func(t *testing.T, log *xop.Logger, w http.ResponseWriter, r *http.Request)
restyOpts []xopresty.ClientOpt
expectError bool
expectedText []string
}{
{
name: "with debugging and tracing",
clientMod: func(c *resty.Client) *resty.Client {
return c.SetDebug(true)
},
requestMod: func(r *resty.Request) *resty.Request {
return r.EnableTrace()
},
},
{
name: "without debugging, without tracing",
},
{
name: "with model",
requestMod: func(r *resty.Request) *resty.Request {
var res exampleResult
return r.
SetBody(exampleRequest{
Name: "Joe",
Count: 38,
}).SetResult(&res).
SetHeader("Content-Type", "application/json").
SetHeader("Accept", "application/json")
},
handler: func(t *testing.T, log *xop.Logger, w http.ResponseWriter, r *http.Request) {
enc, err := json.Marshal(exampleResult{
Score: 3.8,
Comment: "good progress",
})
assert.NoError(t, err, "marshal")
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(enc)
log.Debug().Msg("sent response")
},
expectedText: []string{
`T1.1.1 MODEL:request {"Name":"Joe","Count":38}`,
`T1.1.1 MODEL:response {"Score":3.8,"Comment":"good progress"}`,
},
},
}
func TestXopResty(t *testing.T) {
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
tLog := xoptest.New(t)
seed := xop.NewSeed(xop.WithBase(tLog))
log := seed.Request("client")
log.Info().Msg("i am the base log")
ctx := log.Sub().MinLevel(xopnum.TraceLevel).Logger().IntoContext(context.Background())
var called bool
inbound := xopmiddle.New(seed, func(r *http.Request) string {
return "handler:" + r.Method
})
ts := httptest.NewServer(inbound.HandlerFuncMiddleware()(func(w http.ResponseWriter, r *http.Request) {
called = true
log := xop.FromContextOrPanic(r.Context())
log.Info().Msg("in request handler")
if tc.handler == nil {
http.Error(w, "no handler", 500)
return
}
tc.handler(t, log, w, r)
}))
defer ts.Close()
log.Done()
c := xopresty.Client(resty.New())
if tc.clientMod != nil {
c = tc.clientMod(c)
}
r := c.R().SetContext(ctx)
if tc.requestMod != nil {
r = tc.requestMod(r)
}
_, err := r.Get(ts.URL)
requestSpan := tLog.Recorder().FindSpan(xoprecorder.NameEquals("GET handler:GET"))
require.NotNil(t, requestSpan, "requestSpan")
assert.NotEmpty(t, requestSpan.EndTime, "client request span completed")
if tc.expectError {
assert.Error(t, err, "expected error")
return
}
farSideSpan := tLog.Recorder().FindSpan(xoprecorder.ShortEquals("T1.2"))
require.NotNil(t, farSideSpan, "farSideSpan")
assert.NotEmpty(t, farSideSpan.EndTime, "server endpoint span completed")
assert.NoError(t, err, "Get")
assert.True(t, called, "handler called")
text := "T1.1.1 LINK:http.remote_trace " + farSideSpan.Bundle.Trace.String()
assert.Equalf(t, 1, tLog.Recorder().CountLines(xoprecorder.TextContains(text)), "count lines with '%s'", text)
for _, text := range tc.expectedText {
assert.Equalf(t, 1, tLog.Recorder().CountLines(xoprecorder.TextContains(text)), "count lines with '%s'", text)
}
})
}
}