-
Notifications
You must be signed in to change notification settings - Fork 2
/
client_test.go
95 lines (82 loc) · 2.38 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
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
package gofortiadc
import (
"testing"
)
// TestClient_NewRequest creates an http.Request with authorization header set
func TestClient_NewRequest(t *testing.T) {
// Create Table test case
type testCase struct {
method string
url string
host string
path string
vdom string
query string
}
// Create test cases
testCases := []testCase{
{
method: "GET",
url: "http://localhost:8080/api/v1/status/system",
host: "localhost:8080",
path: "/api/v1/status/system",
vdom: "",
query: "",
},
{
method: "POST",
url: "http://localhost:8080/api/v1/status/system",
host: "localhost:8080",
path: "/api/v1/status/system",
vdom: "root",
query: "vdom=root",
},
{
method: "POST",
url: "http://localhost:8080/api/load_balance_virtual_server?mkey=test",
host: "localhost:8080",
path: "/api/load_balance_virtual_server",
vdom: "root",
query: "mkey=test&vdom=root",
},
}
// Iterate over test cases
for _, tc := range testCases {
// Create client
client, err := NewClientHelper()
if err != nil {
t.Fatal(err)
}
// Set vdom parameter on client
client.VDom = tc.vdom
// Create request
req, err := client.NewRequest(tc.method, tc.url, nil)
if err != nil {
t.Fatal(err)
}
// Test if req.Host is set with the correct value
if req.Host != tc.host {
t.Errorf("req.Host is not set with the correct value, expected %s, got %s", tc.host, req.Host)
}
// Test if req.Path is set with the correct value
if req.URL.Path != tc.path {
t.Errorf("req.Path is not set with the correct value, expected: %s, got: %s", tc.path, req.URL.Path)
}
// Test if authorization header is set with the correct value
if req.Header.Get("Authorization") != "Bearer "+client.Token {
t.Errorf("Authorization header is not set")
}
// Test if vdom parameter is set when vdom is set on client
if tc.vdom != "" && req.URL.Query().Get("vdom") != tc.vdom {
t.Errorf("vdom parameter is not set")
}
// Test if vdom parameter is not set when vdom is not set on client
if tc.vdom == "" && req.URL.Query().Get("vdom") != "" {
t.Errorf("vdom parameter is set")
}
// Test if query parameter is set with the correct value
if tc.query != "" && req.URL.RawQuery != tc.query {
t.Errorf("query parameter is not set with the correct value, expected: %s, got: %s", tc.query, req.URL.RawQuery)
}
}
}