-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_test.go
98 lines (92 loc) · 2.18 KB
/
api_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
package scv
import (
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func TestAPIURL(t *testing.T) {
tests := []struct {
zoneName string
serverID string
want string
}{
{
zoneName: "is1a",
serverID: "1129XXXXXXX1",
want: "https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/server/1129XXXXXXX1/vnc/proxy",
},
}
for _, test := range tests {
api := &API{}
got := api.URL(test.zoneName, test.serverID)
if got != test.want {
t.Fatalf("want: %s, got: %s", test.want, got)
}
}
}
func TestAPIGET(t *testing.T) {
mockHandler := func() func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
username, password, ok := r.BasicAuth()
if !ok || !(username == "xxxx" && password == "xxxx") {
w.WriteHeader(401)
w.Write([]byte("401 Unauthorized"))
} else {
w.WriteHeader(201)
w.Write([]byte("foo"))
}
}
}
mockServer := httptest.NewServer(http.HandlerFunc(mockHandler()))
defer mockServer.Close()
tests := []struct {
url string
accessToken string
accessTokenSecret string
want []byte
err bool
}{
{
url: mockServer.URL,
accessToken: "",
accessTokenSecret: "",
want: nil,
err: true,
},
{
url: mockServer.URL,
accessToken: "xxxx",
accessTokenSecret: "yyyy",
want: nil,
err: true,
},
{
url: mockServer.URL,
accessToken: "yyyy",
accessTokenSecret: "xxxx",
want: nil,
err: true,
},
{
url: mockServer.URL,
accessToken: "xxxx",
accessTokenSecret: "xxxx",
want: []byte("foo"),
err: false,
},
}
api := &API{}
for _, test := range tests {
got, err := api.GET(test.url, test.accessToken, test.accessTokenSecret)
if test.err && err == nil {
t.Fatalf("test.err: %v, err: %v", test.err, err)
}
if !test.err && err != nil {
t.Fatalf("test.err: %v, err: %v", test.err, err)
}
if !reflect.DeepEqual(test.want, got) {
t.Fatalf("test.want: %s, got: %s", test.want, got)
}
}
}