-
Notifications
You must be signed in to change notification settings - Fork 16
/
http_test.go
234 lines (216 loc) · 5.2 KB
/
http_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package webhook
import (
"bytes"
"context"
"errors"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
)
func TestHTTPRequestIDSimple(t *testing.T) {
req := newReqID()
if req == "unknown" {
t.Error("unknown request received")
}
r2 := newReqID()
if req == r2 {
t.Error("same request ID returned in successive calls", r2, req)
}
}
func TestHTTPRequestIDScale(t *testing.T) {
workers := 5
idsPerRoutine := 100
total := workers * idsPerRoutine
var l sync.Mutex
stash := make(map[string]bool, total)
wait := make(chan struct{})
var wg sync.WaitGroup
wg.Add(workers)
for i := 0; i < workers; i++ {
go func() {
defer wg.Done()
<-wait
for i := 0; i < idsPerRoutine; i++ {
r := newReqID()
l.Lock()
stash[r] = true
l.Unlock()
}
}()
}
close(wait)
wg.Wait()
if len(stash) != total {
t.Errorf("id collision for %d ids, got %d uniques", total, len(stash))
}
}
func TestHTTPContextLogger(t *testing.T) {
l := newlp()
config := Config{
LogFlags: LogTraceAthenz | LogVerboseMapping,
LogProvider: func(id string) Logger {
l.id = id
return l
},
}
var r http.Request
r2 := requestWithContext(&r, config)
if r2 == &r {
t.Fatal("request not modified for context")
}
if l.id == "" {
t.Fatal("logger was not requested")
}
logger := GetLogger(r2.Context())
logger.Printf("%s %s", "hello", "world")
logger.Println("goodbye", "world")
e := IsLogEnabled(r2.Context(), LogTraceAthenz)
if !e {
t.Error("bad trace athenz flag, want true")
}
e = IsLogEnabled(r2.Context(), LogTraceServer)
if e {
t.Error("bad trace server flag, want false")
}
lines := strings.Split(strings.TrimRight(l.b.String(), "\n"), "\n")
if len(lines) != 2 {
t.Fatal("invalid lines in buffer, want 2, got", len(lines))
}
expected := []string{"hello world", "goodbye world"}
for i, e := range expected {
if e != lines[i] {
t.Errorf("bad msg, want %q, got %q", e, lines[i])
}
}
}
func TestHTTPContextNoLogger(t *testing.T) {
c := context.Background()
logger := GetLogger(c)
if logger == nil {
t.Error("nil logger returned for invalid context")
}
e := IsLogEnabled(c, LogTraceServer)
if e {
t.Error("bad trace server flag, should always be false for invalid context")
}
}
func TestHTTPWrapHandler(t *testing.T) {
l := newlp()
handler := wrapHandler(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
}),
Config{
LogFlags: LogTraceServer,
LogProvider: func(id string) Logger {
l.id = id
return l
},
},
)
body := bytes.NewBufferString("hello world")
r := httptest.NewRequest("POST", "/foo", body)
r.Header.Set("Input-Foo", "Bar")
r.Header.Add("Input-Bar", "Beer")
r.Header.Add("Input-Bar", "Wine")
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
if w.Code != 404 {
t.Fatal("delegate handler was not called")
}
capture := l.b.String()
for _, s := range []string{"server request from", "POST /foo", "Input-Foo : Bar", "Input-Bar : [Beer Wine]", "end headers"} {
if !strings.Contains(capture, s) {
t.Errorf("log %q does not contain %q", capture, s)
}
}
}
func TestHTTPWrapHandlerNoTrace(t *testing.T) {
l := newlp()
handler := wrapHandler(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
}),
Config{
LogFlags: LogVerboseMapping,
LogProvider: func(id string) Logger {
l.id = id
return l
},
},
)
r := httptest.NewRequest("GET", "/foo", nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
capture := l.b.String()
if capture != "" {
t.Fatal("logs found when tracing not enabled", capture)
}
}
func TestHTTPWriteJSON(t *testing.T) {
l := newlp()
config := Config{
LogProvider: func(id string) Logger {
l.id = id
return l
},
}
r := httptest.NewRequest("GET", "/foo", nil)
r2 := requestWithContext(r, config)
data := struct {
Foo string
Bar string
}{"foo", "bar"}
var body bytes.Buffer
w := httptest.NewRecorder()
w.Body = &body
writeJSON(r2.Context(), w, data)
if len(w.HeaderMap["Content-Type"]) != 1 {
t.Fatal("no content-type set in header")
}
if w.HeaderMap["Content-Type"][0] != "application/json" {
t.Error("bad content type header", w.HeaderMap["Content-Type"][0])
}
expected := `{"Foo":"foo","Bar":"bar"}`
if expected != body.String() {
t.Errorf("bad body, want '%s' got '%s'", expected, body.String())
}
if l.b.String() != "" {
t.Errorf("nothing should be printed to logger, found '%s", l.b.String())
}
}
type node struct {
}
func (n *node) MarshalJSON() ([]byte, error) {
return nil, errors.New("FOOBAR")
}
func TestHTTPWriteBadJSON(t *testing.T) {
l := newlp()
config := Config{
LogProvider: func(id string) Logger {
l.id = id
return l
},
}
r := httptest.NewRequest("GET", "/foo", nil)
r2 := requestWithContext(r, config)
n := &node{}
var body bytes.Buffer
w := httptest.NewRecorder()
w.Body = &body
writeJSON(r2.Context(), w, n)
es := l.b.String()
for _, e := range []string{"internal serialization error", "FOOBAR"} {
if !strings.Contains(es, e) {
t.Errorf("Error string '%s' did not contain '%s'", es, e)
}
}
if w.Code != 500 {
t.Errorf("500 error was not set")
}
es = "internal serialization error\n"
if body.String() != es {
t.Errorf("bad external message want '%q' got '%q", es, body.String())
}
}