forked from movio/bramble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgateway_test.go
220 lines (197 loc) · 5.65 KB
/
gateway_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
package bramble
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// can only run one test at a time that takes over the logrus output
var logrusLock = sync.Mutex{}
func TestGatewayQuery(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var req struct {
Query string
}
json.NewDecoder(r.Body).Decode(&req)
if strings.Contains(req.Query, "service") {
// initial query to get schema
schema := `type Service {
name: String!
version: String!
schema: String!
}
type Query {
test: String
service: Service!
}`
encodedSchema, _ := json.Marshal(schema)
fmt.Fprintf(w, `{
"data": {
"service": {
"schema": %s,
"version": "1.0",
"name": "test-service"
}
}
}`, string(encodedSchema))
assert.Equal(t, "Bramble/dev (update)", r.Header.Get("User-Agent"))
} else {
w.Write([]byte(`{ "data": { "test": "Hello" }}`))
assert.Equal(t, "Bramble/dev (query)", r.Header.Get("User-Agent"))
}
}))
client := NewClient(WithUserAgent(GenerateUserAgent("query")))
executableSchema := NewExecutableSchema(nil, 50, client, NewService(server.URL))
err := executableSchema.UpdateSchema(true)
require.NoError(t, err)
gtw := NewGateway(executableSchema, []Plugin{})
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/query", strings.NewReader(`
{
"query": "query { test }"
}`))
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("Accept", "application/json; charset=utf-8")
gtw.Router(&Config{}).ServeHTTP(rec, req)
assert.Equal(t, http.StatusOK, rec.Code)
assert.JSONEq(t, `{"data": { "test": "Hello" }}`, rec.Body.String())
}
func TestRequestJSONBodyLogging(t *testing.T) {
logrusLock.Lock()
defer logrusLock.Unlock()
server := NewGateway(NewExecutableSchema(nil, 50, nil), nil).Router(&Config{})
body := map[string]interface{}{
"foo": "bar",
}
jr, jw := io.Pipe()
go func() {
enc := json.NewEncoder(jw)
enc.Encode(body)
jw.Close()
}()
defer jr.Close()
req := httptest.NewRequest("POST", "/query", jr)
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
obj := collectLogEvent(t, func() {
server.ServeHTTP(w, req)
})
resp := w.Result()
assert.NotNil(t, obj)
assert.Equal(t, float64(resp.StatusCode), obj["response.status"])
assert.Equal(t, "application/json", obj["request.content-type"])
assert.IsType(t, make(map[string]interface{}), obj["request.body"])
assert.Equal(t, body, obj["request.body"])
}
func TestRequestInvalidJSONBodyLogging(t *testing.T) {
logrusLock.Lock()
defer logrusLock.Unlock()
server := NewGateway(nil, nil).Router(&Config{})
body := `{ "invalid": "json`
jr, jw := io.Pipe()
go func() {
jw.Write([]byte(body))
jw.Close()
}()
defer jr.Close()
req := httptest.NewRequest("POST", "/query", jr)
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
obj := collectLogEvent(t, func() {
server.ServeHTTP(w, req)
})
w.Result()
assert.NotNil(t, obj)
assert.Equal(t, "application/json", obj["request.content-type"])
assert.IsType(t, "string", obj["request.body"])
assert.Equal(t, body, obj["request.body"])
assert.Equal(t, "unexpected end of JSON input", obj["request.error"])
}
func TestRequestTextBodyLogging(t *testing.T) {
logrusLock.Lock()
defer logrusLock.Unlock()
server := NewGateway(nil, nil).Router(&Config{})
body := `the request body`
jr, jw := io.Pipe()
go func() {
jw.Write([]byte(body))
jw.Close()
}()
defer jr.Close()
req := httptest.NewRequest("POST", "/query", jr)
req.Header.Set("Content-Type", "text/plain")
w := httptest.NewRecorder()
obj := collectLogEvent(t, func() {
server.ServeHTTP(w, req)
})
w.Result()
assert.NotNil(t, obj)
assert.Equal(t, "text/plain", obj["request.content-type"])
assert.IsType(t, "string", obj["request.body"])
assert.Equal(t, body, obj["request.body"])
assert.Equal(t, nil, obj["request.error"])
}
func TestDebugMiddleware(t *testing.T) {
t.Run("without debug header", func(t *testing.T) {
called := false
req := httptest.NewRequest("POST", "/", nil)
h := func(w http.ResponseWriter, r *http.Request) {
called = true
info, ok := r.Context().Value(DebugKey).(DebugInfo)
assert.True(t, ok, "context should include debugInfo")
assert.False(t, info.Variables)
assert.False(t, info.Query)
assert.False(t, info.Plan)
w.WriteHeader(http.StatusOK)
}
server := debugMiddleware(http.HandlerFunc(h))
w := httptest.NewRecorder()
server.ServeHTTP(w, req)
assert.True(t, called, "handler not called")
})
for header, expected := range map[string]DebugInfo{
"all": {
Variables: true,
Query: true,
Plan: true,
},
"query": {
Query: true,
},
"variables": {
Variables: true,
},
"plan": {
Plan: true,
},
"query plan": {
Query: true,
Plan: true,
},
} {
t.Run("with debug header value all", func(t *testing.T) {
called := false
req := httptest.NewRequest("POST", "/", nil)
req.Header.Set(debugHeader, header)
h := func(w http.ResponseWriter, r *http.Request) {
called = true
info, ok := r.Context().Value(DebugKey).(DebugInfo)
assert.True(t, ok, "context should include debugInfo")
assert.Equal(t, expected.Variables, info.Variables)
assert.Equal(t, expected.Query, info.Query)
assert.Equal(t, expected.Plan, info.Plan)
w.WriteHeader(http.StatusOK)
}
server := debugMiddleware(http.HandlerFunc(h))
w := httptest.NewRecorder()
server.ServeHTTP(w, req)
assert.True(t, called, "handler not called")
})
}
}