-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathserver.go
More file actions
390 lines (327 loc) · 8.65 KB
/
Copy pathserver.go
File metadata and controls
390 lines (327 loc) · 8.65 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package testserver
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"maps"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"regexp"
"strconv"
"strings"
"sync"
"github.com/databricks/cli/internal/testutil"
"github.com/databricks/cli/libs/testserver/testsql"
)
const testPidKey = "test-pid"
var testPidRegex = regexp.MustCompile(testPidKey + `/(\d+)`)
func ExtractPidFromHeaders(headers http.Header) int {
ua := headers.Get("User-Agent")
matches := testPidRegex.FindStringSubmatch(ua)
if len(matches) < 2 {
return 0
}
pid, err := strconv.Atoi(matches[1])
if err != nil {
return 0
}
return pid
}
type Server struct {
*httptest.Server
*Router
t testutil.TestingT
fakeWorkspaces map[string]*FakeWorkspace
fakeOidc *FakeOidc
mu sync.Mutex
kills *killRules
faults *FaultRules
sqlHandler *testsql.Handler
RequestCallback func(request *Request)
ResponseCallback func(request *Request, response *EncodedResponse)
}
type Request struct {
Method string
URL *url.URL
Headers http.Header
Body []byte
Vars map[string]string
Workspace *FakeWorkspace
Context context.Context
Token string
}
type Response struct {
StatusCode int
Headers http.Header
Body any
}
type EncodedResponse struct {
StatusCode int
Headers http.Header
Body []byte
}
func NewRequest(t testutil.TestingT, r *http.Request, fakeWorkspace *FakeWorkspace) Request {
body, err := io.ReadAll(r.Body)
if err != nil {
t.Logf("Error while reading request body: %s", err)
}
return Request{
Method: r.Method,
URL: r.URL,
Headers: r.Header,
Body: body,
Workspace: fakeWorkspace,
Context: r.Context(),
}
}
func normalizeResponse(t testutil.TestingT, resp any) EncodedResponse {
result := normalizeResponseBody(t, resp)
if result.StatusCode == 0 {
result.StatusCode = 200
}
return result
}
func normalizeResponseBody(t testutil.TestingT, resp any) EncodedResponse {
if isNil(resp) {
t.Errorf("Handler must not return nil")
return EncodedResponse{StatusCode: 500}
}
respBytes, ok := resp.([]byte)
if ok {
return EncodedResponse{
Body: respBytes,
Headers: getHeaders(respBytes),
}
}
respString, ok := resp.(string)
if ok {
return EncodedResponse{
Body: []byte(respString),
Headers: getHeaders([]byte(respString)),
}
}
respStruct, ok := resp.(Response)
if ok {
if isNil(respStruct.Body) {
return EncodedResponse{
StatusCode: respStruct.StatusCode,
Headers: respStruct.Headers,
Body: []byte{},
}
}
bytesVal, isBytes := respStruct.Body.([]byte)
if isBytes {
return EncodedResponse{
StatusCode: respStruct.StatusCode,
Headers: respStruct.Headers,
Body: bytesVal,
}
}
stringVal, isString := respStruct.Body.(string)
if isString {
return EncodedResponse{
StatusCode: respStruct.StatusCode,
Headers: respStruct.Headers,
Body: []byte(stringVal),
}
}
respBytes, err := json.MarshalIndent(respStruct.Body, "", " ")
if err != nil {
t.Errorf("JSON encoding error: %s", err)
return EncodedResponse{
StatusCode: 500,
Body: []byte("internal error"),
}
}
headers := respStruct.Headers
if headers == nil {
headers = getJsonHeaders()
}
return EncodedResponse{
StatusCode: respStruct.StatusCode,
Headers: headers,
Body: respBytes,
}
}
respBytes, err := json.MarshalIndent(resp, "", " ")
if err != nil {
t.Errorf("JSON encoding error: %s", err)
return EncodedResponse{
StatusCode: 500,
Body: []byte("internal error"),
}
}
return EncodedResponse{
Body: respBytes,
Headers: getJsonHeaders(),
}
}
func getJsonHeaders() http.Header {
return map[string][]string{
"Content-Type": {"application/json"},
}
}
func getHeaders(value []byte) http.Header {
if json.Valid(value) {
return getJsonHeaders()
} else {
return map[string][]string{
"Content-Type": {"text/plain"},
}
}
}
func New(t testutil.TestingT) *Server {
router := NewRouter()
kills := newKillRules()
faults := NewFaultRules()
// Wrap the router so kill rules fire for ALL requests, including those with
// no registered handler that would otherwise bypass serve() entirely.
killMiddleware := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
token := getToken(r)
if kills.check(t, r.Method, r.URL.Path, token, r.Header) {
return
}
router.ServeHTTP(w, r)
})
server := httptest.NewServer(killMiddleware)
t.Cleanup(server.Close)
s := &Server{
Server: server,
Router: router,
t: t,
fakeWorkspaces: map[string]*FakeWorkspace{},
fakeOidc: &FakeOidc{url: server.URL},
kills: kills,
faults: faults,
sqlHandler: testsql.New(),
}
router.Dispatch = s.serve
t.Cleanup(func() {
for _, ws := range s.fakeWorkspaces {
ws.Cleanup()
}
})
// Set up the not found handler as fallback
notFoundFunc := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
pattern := r.Method + " " + r.URL.Path
bodyBytes, err := io.ReadAll(r.Body)
var body string
if err != nil {
body = fmt.Sprintf("failed to read the body: %s", err)
} else {
body = fmt.Sprintf("[%d bytes] %s", len(bodyBytes), bodyBytes)
}
t.Errorf(`No handler for URL: %s
Body: %s
For acceptance tests, add this to test.toml:
[[Server]]
Pattern = %q
Response.Body = '<response body here>'
# Response.StatusCode = <response code if not 200>
`, r.URL, body, pattern)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotImplemented)
resp := map[string]string{
"message": "No stub found for pattern: " + pattern,
}
respBytes, err := json.Marshal(resp)
if err != nil {
t.Errorf("JSON encoding error: %s", err)
respBytes = []byte("{\"message\": \"JSON encoding error\"}")
}
if _, err := w.Write(respBytes); err != nil {
t.Errorf("Response write error: %s", err)
}
})
router.NotFound = notFoundFunc
// Register test-only endpoints for setting up kill and fault rules from scripts.
s.Handle("POST", "/__testserver/kill", killEndpointHandler(s.kills))
s.Handle("POST", "/__testserver/fault", faultEndpointHandler(s.faults))
// Register a default handler for the SDK's host metadata discovery endpoint.
// The SDK resolves this during config initialization (as of v0.126.0) to
// determine workspace/account IDs, cloud, and OIDC endpoints. Without this
// handler, any test that creates an SDK client against this server would fail
// with "No handler for URL: /.well-known/databricks-config".
s.Handle("GET", "/.well-known/databricks-config", func(_ Request) any {
return map[string]any{
"oidc_endpoint": server.URL + "/oidc",
"workspace_id": "900800700600",
}
})
return s
}
func (s *Server) getWorkspaceForToken(token string) *FakeWorkspace {
if token == "" {
return nil
}
s.mu.Lock()
defer s.mu.Unlock()
if _, ok := s.fakeWorkspaces[token]; !ok {
s.fakeWorkspaces[token] = NewFakeWorkspace(s.URL, token)
}
return s.fakeWorkspaces[token]
}
func (s *Server) serve(w http.ResponseWriter, r *http.Request, handler HandlerFunc, vars map[string]string) {
token := getToken(r)
// Each test uses unique DATABRICKS_TOKEN, we simulate each token having
// it's own fake fakeWorkspace to avoid interference between tests.
fakeWorkspace := s.getWorkspaceForToken(token)
request := NewRequest(s.t, r, fakeWorkspace)
request.Vars = vars
request.Token = token
if s.RequestCallback != nil {
s.RequestCallback(&request)
}
var resp EncodedResponse
if rule := s.faults.Check(r.Method, r.URL.Path, token); rule != nil {
resp = EncodedResponse{
StatusCode: rule.StatusCode,
Body: []byte(rule.Body),
Headers: getJsonHeaders(),
}
} else if bytes.Contains(request.Body, []byte("INJECT_ERROR")) {
resp = EncodedResponse{
StatusCode: 500,
Body: []byte("INJECTED"),
}
} else {
respAny := handler(request)
if respAny == nil && request.Context.Err() != nil {
return
}
resp = normalizeResponse(s.t, respAny)
}
maps.Copy(w.Header(), resp.Headers)
w.WriteHeader(resp.StatusCode)
if s.ResponseCallback != nil {
s.ResponseCallback(&request, &resp)
}
if _, err := w.Write(resp.Body); err != nil {
s.t.Errorf("Failed to write response: %s", err)
return
}
}
func getToken(r *http.Request) string {
header := r.Header.Get("Authorization")
prefix := "Bearer "
if !strings.HasPrefix(header, prefix) {
return ""
}
return header[len(prefix):]
}
func isNil(i any) bool {
if i == nil {
return true
}
v := reflect.ValueOf(i)
switch v.Kind() {
case reflect.Chan, reflect.Func, reflect.Map, reflect.Pointer, reflect.Interface, reflect.Slice:
return v.IsNil()
default:
return false
}
}