forked from tencentyun/cos-go-sdk-v5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cos_test.go
181 lines (155 loc) · 4.5 KB
/
cos_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
172
173
174
175
176
177
178
179
180
181
package cos
import (
"bytes"
"context"
"encoding/xml"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"
"time"
)
var (
// mux is the HTTP request multiplexer used with the test server.
mux *http.ServeMux
// client is the COS client being tested.
client *Client
// server is a test HTTP server used to provide mock API responses.
server *httptest.Server
)
// setup sets up a test HTTP server along with a cos.Client that is
// configured to talk to that test server. Tests should register handlers on
// mux which provide mock responses for the API method being tested.
func setup() {
// test server
mux = http.NewServeMux()
server = httptest.NewServer(mux)
u, _ := url.Parse(server.URL)
client = NewClient(&BaseURL{u, u, u, u, u}, nil)
}
// teardown closes the test HTTP server.
func teardown() {
server.Close()
}
type values map[string]string
func testFormValues(t *testing.T, r *http.Request, values values) {
want := url.Values{}
for k, v := range values {
want.Set(k, v)
}
r.ParseForm()
if got := r.Form; !reflect.DeepEqual(got, want) {
t.Errorf("Request parameters: %v, want %v", got, want)
}
}
func testMethod(t *testing.T, r *http.Request, want string) {
if got := r.Method; got != want {
t.Errorf("Request method: %v, want %v", got, want)
}
}
func testHeader(t *testing.T, r *http.Request, header string, want string) {
if got := r.Header.Get(header); got != want {
t.Errorf("Header.Get(%q) returned %q, want %q", header, got, want)
}
}
func testURLParseError(t *testing.T, err error) {
if err == nil {
t.Errorf("Expected error to be returned")
}
if err, ok := err.(*url.Error); !ok || err.Op != "parse" {
t.Errorf("Expected URL parse error, got %+v", err)
}
}
func testBody(t *testing.T, r *http.Request, want string) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Errorf("Error reading request body: %v", err)
}
if got := string(b); got != want {
t.Errorf("request Body is %s, want %s", got, want)
}
}
// Helper function to test that a value is marshalled to XML as expected.
func testXMLMarshal(t *testing.T, v interface{}, want string) {
j, err := xml.Marshal(v)
if err != nil {
t.Errorf("Unable to marshal JSON for %v", v)
}
w := new(bytes.Buffer)
err = xml.NewEncoder(w).Encode([]byte(want))
if err != nil {
t.Errorf("String is not valid json: %s", want)
}
if w.String() != string(j) {
t.Errorf("xml.Marshal(%q) returned %s, want %s", v, j, w)
}
// now go the other direction and make sure things unmarshal as expected
u := reflect.ValueOf(v).Interface()
if err := xml.Unmarshal([]byte(want), u); err != nil {
t.Errorf("Unable to unmarshal XML for %v", want)
}
if !reflect.DeepEqual(v, u) {
t.Errorf("xml.Unmarshal(%q) returned %s, want %s", want, u, v)
}
}
func TestNewClient(t *testing.T) {
c := NewClient(nil, nil)
if got, want := c.BaseURL.ServiceURL.String(), defaultServiceBaseURL; got != want {
t.Errorf("NewClient BaseURL is %v, want %v", got, want)
}
if got, want := c.UserAgent, UserAgent; got != want {
t.Errorf("NewClient UserAgent is %v, want %v", got, want)
}
}
func TestNewBucketURL_secure_false(t *testing.T) {
u, _ := NewBucketURL("bname-idx", "ap-guangzhou", false)
got := u.String()
want := "http://bname-idx.cos.ap-guangzhou.myqcloud.com"
if got != want {
t.Errorf("NewBucketURL is %v, want %v", got, want)
}
_, err := NewBucketURL("", "ap-guangzhou", false)
if err == nil {
t.Errorf("NewBucketURL should return error")
}
_, err = NewBucketURL("bname-idx", "", false)
if err == nil {
t.Errorf("NewBucketURL should return error")
}
}
func TestNewBucketURL_secure_true(t *testing.T) {
u, _ := NewBucketURL("bname-idx", "ap-guangzhou", true)
got := u.String()
want := "https://bname-idx.cos.ap-guangzhou.myqcloud.com"
if got != want {
t.Errorf("NewBucketURL is %v, want %v", got, want)
}
}
func TestClient_doAPI(t *testing.T) {
setup()
defer teardown()
}
func TestNewAuthTime(t *testing.T) {
a := NewAuthTime(time.Hour)
if a.SignStartTime != a.KeyStartTime ||
a.SignEndTime != a.SignEndTime ||
a.SignStartTime.Add(time.Hour) != a.SignEndTime {
t.Errorf("NewAuthTime request got %+v is not valid", a)
}
}
func Test_addHeaderOptions(t *testing.T) {
val := &XOptionalValue{
&http.Header{},
}
val.Header.Add("key", "value")
ctx := context.WithValue(context.Background(), XOptionalKey, val)
res, err := addHeaderOptions(ctx, http.Header{}, nil)
if err != nil {
t.Errorf("addHeaderOptions return failed: %v", err)
}
if res.Get("key") != "value" {
t.Errorf("addHeaderOptions failed")
}
}