forked from gstotts/insightcloudsec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
64 lines (52 loc) · 1.42 KB
/
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package insightcloudsec
import (
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
var (
// mux is the HTTP request multiplexer used with the test server.
mux *http.ServeMux
// client is the API client being tested.
client *Client
// server is a test HTTP server used to provide mock API responses.
server *httptest.Server
)
func setup() {
// Test Server
mux = http.NewServeMux()
server = httptest.NewServer(mux)
// Configure client to use with test server
test_config := Config{
BaseURL: server.URL,
ApiKey: "whoami",
Headers: map[string][]string{},
HTTPClient: &http.Client{},
}
client, _ = NewClient(&test_config)
}
func teardown() {
server.Close()
}
func getJSONFile(path string) string {
b, err := os.ReadFile("testdata/" + path)
if err != nil {
panic(err)
}
return string(b)
}
func TestClient_Headers(t *testing.T) {
// Default headers should be set appropriately
setup()
mux.HandleFunc("/v2/public/user/info", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'POST', got %s", r.Method)
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
assert.Equal(t, "whoami", r.Header.Get("Api-Key"))
assert.Equal(t, "application/json", r.Header.Get("Accept"))
assert.Equal(t, "insightcloudsec-client", r.Header.Get("User-Agent"))
})
client.Users.CurrentUserInfo()
teardown()
}