-
Notifications
You must be signed in to change notification settings - Fork 5
/
client_test.go
103 lines (92 loc) · 2.54 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
96
97
98
99
100
101
102
103
package remy
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
)
func TestClientAuthenticatedCalls(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
u, p, _ := r.BasicAuth()
if u != "user" && p != "pass" {
t.Fail()
}
}))
defer ts.Close()
t.Log(ts.URL)
_, err := requestResource(ts.URL,
&AdminServer{AdminURL: ts.URL, Username: "user", Password: "pass"})
assert.NoError(t, err)
}
func TestAcceptJsonHeaderCall(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Accept") != "application/json" {
t.Fail()
}
}))
defer ts.Close()
t.Log(ts.URL)
_, err := requestResource(ts.URL, &AdminServer{AdminURL: ts.URL, Username: "user", Password: "pass"})
assert.NoError(t, err)
}
func CreateTestServerResourceRouters() *mux.Router {
r := mux.NewRouter()
r.HandleFunc(MonitorPath+"/servers", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(serversJSON))
})
r.HandleFunc(MonitorPath+"/servers/{server}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
server := vars["server"]
if server != "adminserver" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("No servername by that name"))
} else {
w.Write([]byte(singleServer))
}
})
return r
}
func TestServerResourceRelatedCalls(t *testing.T) {
ts := httptest.NewServer(CreateTestServerResourceRouters())
defer ts.Close()
service := new(AdminServer)
service.AdminURL = ts.URL
service.Username = "user"
service.Password = "pass"
s, err := service.Servers(false)
if err != nil {
t.Fail()
}
if len(s) != 2 {
t.Fail()
}
server, err := service.Server("adminserver")
if err != nil {
t.Fail()
}
var serverJSONTests = []struct {
in string
out string
}{
{server.Name, "adminserver"},
{server.ClusterName, ""},
{server.State, "RUNNING"},
{server.CurrentMachine, "machine-0"},
{server.WebLogicVersion, "WebLogic Server 12.1.1.0.0 Thu May 5 01:17:16 2011 PDT"},
{fmt.Sprint(server.OpenSocketsCurrentCount), "2"},
{server.Health, "HEALTH_OK"},
{fmt.Sprint(server.HeapSizeCurrent), "536870912"},
{fmt.Sprint(server.HeapFreeCurrent), "39651944"},
{server.JavaVersion, "1.6.0_20"},
{server.OsName, "Linux"},
{server.OsVersion, "2.6.18-238.0.0.0.1.el5xen"},
{fmt.Sprint(server.JvmProcessorLoad), "0.25"},
}
for _, tt := range serverJSONTests {
if tt.in != tt.out {
t.Errorf("want %q, got %q", tt.out, tt.in)
}
}
}