-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_test.go
43 lines (33 loc) · 960 Bytes
/
client_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
package auroradns
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
)
func setupTest(t *testing.T) (*Client, *http.ServeMux) {
t.Helper()
apiHandler := http.NewServeMux()
server := httptest.NewServer(apiHandler)
client, err := NewClient(nil, WithBaseURL(server.URL))
require.NoError(t, err)
t.Cleanup(server.Close)
return client, apiHandler
}
func handleAPI(mux *http.ServeMux, pattern, method string, next func(w http.ResponseWriter, r *http.Request)) {
mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
if r.Method != method {
http.Error(w, fmt.Sprintf("invalid method %s", r.Method), http.StatusMethodNotAllowed)
return
}
contentType := r.Header.Get(contentTypeHeader)
if contentType != contentTypeJSON {
http.Error(w, fmt.Sprintf("invalid Content-Type %s", contentType), http.StatusBadRequest)
return
}
if next != nil {
next(w, r)
}
})
}