forked from kothar/go-backblaze
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackblaze_test.go
171 lines (147 loc) · 3.92 KB
/
backblaze_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package backblaze
import (
"fmt"
"log"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/pquerna/ffjson/ffjson"
)
type response struct {
code int
body interface{}
headers map[string]string
}
// Based on http://keighl.com/post/mocking-http-responses-in-golang/
func prepareResponses(responses []response) (*http.Client, *httptest.Server) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if len(responses) == 0 {
w.WriteHeader(500)
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, "No more responses")
return
}
next := responses[0]
responses = responses[1:]
for k, v := range next.headers {
w.Header().Add(k, v)
}
w.WriteHeader(next.code)
w.Header().Set("Content-Type", "application/json")
if body, ok := next.body.([]byte); ok {
w.Write(body)
} else {
fmt.Fprint(w, toJSON(next.body))
}
}))
// Make a transport that reroutes all traffic to the example server
transport := &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return url.Parse(server.URL + req.URL.Path)
},
}
// Make a http.Client with the transport
client := &http.Client{Transport: transport}
return client, server
}
func toJSON(o interface{}) string {
bytes, err := ffjson.Marshal(o)
if err != nil {
log.Fatal(err)
}
return string(bytes)
}
func TestUnauthorizedError(T *testing.T) {
err := &B2Error{Status: 401, Code: "expired_auth_token"}
if err.IsFatal() {
T.Fatal("401 expired_auth_token error should not be considered fatal")
}
}
func TestAuth(T *testing.T) {
accountID := "test"
token := "testToken"
apiURL := "http://api.url"
downloadURL := "http://download.url"
client, server := prepareResponses([]response{
{code: 200, body: authorizeAccountResponse{
AccountID: accountID,
APIEndpoint: apiURL,
AuthorizationToken: token,
DownloadURL: downloadURL,
}},
})
defer server.Close()
b2 := &B2{
Credentials: Credentials{
AccountID: accountID,
ApplicationKey: "test",
},
Debug: testing.Verbose(),
host: server.URL,
httpClient: *client,
}
if err := b2.AuthorizeAccount(); err != nil {
T.Fatal(err)
}
if b2.auth.AuthorizationToken != token {
T.Errorf("Auth token not set correctly: expecting %q, saw %q", token, b2.auth.AuthorizationToken)
}
if b2.auth.APIEndpoint != apiURL {
T.Errorf("API Endpoint not set correctly: expecting %q, saw %q", apiURL, b2.auth.APIEndpoint)
}
if b2.auth.DownloadURL != downloadURL {
T.Errorf("Download URL not set correctly: expecting %q, saw %q", downloadURL, b2.auth.DownloadURL)
}
}
func TestReAuth(T *testing.T) {
accountID := "test"
bucketID := "bucketid"
token2 := "testToken2"
client, server := prepareResponses([]response{
{code: 200, body: authorizeAccountResponse{
AccountID: accountID,
APIEndpoint: "http://api.url",
AuthorizationToken: "testToken",
DownloadURL: "http://download.url",
}},
{code: 401, body: B2Error{
Status: 401,
Code: "expired_auth_token",
Message: "Authentication token expired",
}},
{code: 200, body: authorizeAccountResponse{
AccountID: accountID,
APIEndpoint: "http://api.url",
AuthorizationToken: token2,
DownloadURL: "http://download.url",
}},
{code: 200, body: listBucketsResponse{
Buckets: []*BucketInfo{
&BucketInfo{
ID: bucketID,
AccountID: accountID,
Name: "testbucket",
BucketType: AllPrivate,
},
},
}},
})
defer server.Close()
b2 := &B2{
Credentials: Credentials{
AccountID: accountID,
ApplicationKey: "test",
},
Debug: testing.Verbose(),
httpClient: *client,
host: server.URL,
}
_, err := b2.ListBuckets()
if err != nil {
T.Fatal(err)
}
if b2.auth.AuthorizationToken != token2 {
T.Errorf("Expected auth token after re-auth to be %q, saw %q", token2, b2.auth.AuthorizationToken)
}
}