vendor: switch to using go1.11 modules
This commit is contained in:
-125
@@ -1,125 +0,0 @@
|
||||
// +-------------------------------------------------------------------------
|
||||
// | Copyright (C) 2016 Yunify, Inc.
|
||||
// +-------------------------------------------------------------------------
|
||||
// | Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// | you may not use this work except in compliance with the License.
|
||||
// | You may obtain a copy of the License in the LICENSE file, or at:
|
||||
// |
|
||||
// | http://www.apache.org/licenses/LICENSE-2.0
|
||||
// |
|
||||
// | Unless required by applicable law or agreed to in writing, software
|
||||
// | distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// | See the License for the specific language governing permissions and
|
||||
// | limitations under the License.
|
||||
// +-------------------------------------------------------------------------
|
||||
|
||||
package builder
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/yunify/qingstor-sdk-go/config"
|
||||
"github.com/yunify/qingstor-sdk-go/request/data"
|
||||
)
|
||||
|
||||
type FakeProperties struct {
|
||||
A *string `name:"a"`
|
||||
B *string `name:"b"`
|
||||
CD *int `name:"c-d"`
|
||||
}
|
||||
type FakeInput struct {
|
||||
ParamA *string `location:"query" name:"a"`
|
||||
ParamB *string `location:"query" name:"b"`
|
||||
ParamCD *int `location:"query" name:"c_d" default:"1024"`
|
||||
HeaderA *string `location:"headers" name:"A"`
|
||||
HeaderB *time.Time `location:"headers" name:"B" format:"RFC 822"`
|
||||
HeaderCD *int `location:"headers" name:"C-D"`
|
||||
ElementA *string `location:"elements" name:"a"`
|
||||
ElementB *string `location:"elements" name:"b"`
|
||||
ElementCD *int64 `location:"elements" name:"cd"`
|
||||
Body *string `localtion:"body"`
|
||||
}
|
||||
|
||||
func (i *FakeInput) Validate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func String(v string) *string {
|
||||
return &v
|
||||
}
|
||||
|
||||
func Int(v int) *int {
|
||||
return &v
|
||||
}
|
||||
|
||||
func Int64(v int64) *int64 {
|
||||
return &v
|
||||
}
|
||||
|
||||
func Time(v time.Time) *time.Time {
|
||||
return &v
|
||||
}
|
||||
|
||||
func TestBaseBuilder_BuildHTTPRequest(t *testing.T) {
|
||||
conf, err := config.NewDefault()
|
||||
assert.Nil(t, err)
|
||||
|
||||
tz, err := time.LoadLocation("Asia/Shanghai")
|
||||
assert.Nil(t, err)
|
||||
|
||||
builder := BaseBuilder{}
|
||||
operation := &data.Operation{
|
||||
Config: conf,
|
||||
APIName: "This is API name",
|
||||
ServiceName: "Base",
|
||||
Properties: &FakeProperties{
|
||||
A: String("property_a"),
|
||||
B: String("property_b"),
|
||||
CD: Int(0),
|
||||
},
|
||||
RequestMethod: "GET",
|
||||
RequestURI: "/hello/<a>/<c-d>/<b>/world",
|
||||
StatusCodes: []int{
|
||||
200,
|
||||
201,
|
||||
},
|
||||
}
|
||||
inputValue := reflect.ValueOf(&FakeInput{
|
||||
ParamA: String("param_a"),
|
||||
ParamCD: Int(1024),
|
||||
HeaderA: String("header_a"),
|
||||
HeaderB: Time(time.Date(2016, 9, 1, 15, 30, 0, 0, tz)),
|
||||
ElementA: String("element_a"),
|
||||
ElementB: String("element_b"),
|
||||
ElementCD: Int64(0),
|
||||
Body: String("This is body string"),
|
||||
})
|
||||
httpRequest, err := builder.BuildHTTPRequest(operation, &inputValue)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, &map[string]string{
|
||||
"a": "property_a",
|
||||
"b": "property_b",
|
||||
"c-d": "0",
|
||||
}, builder.parsedProperties)
|
||||
assert.Equal(t, &map[string]string{
|
||||
"a": "param_a",
|
||||
"c_d": "1024",
|
||||
}, builder.parsedQuery)
|
||||
assert.Equal(t, &map[string]string{
|
||||
"A": "header_a",
|
||||
"B": "Thu, 01 Sep 2016 07:30:00 GMT",
|
||||
"Content-Type": "application/json",
|
||||
}, builder.parsedHeaders)
|
||||
assert.NotNil(t, httpRequest.Header.Get("Date"))
|
||||
assert.Equal(t, "40", httpRequest.Header.Get("Content-Length"))
|
||||
|
||||
buffer := &bytes.Buffer{}
|
||||
buffer.ReadFrom(httpRequest.Body)
|
||||
httpRequest.Body.Close()
|
||||
assert.Equal(t, "{\"a\":\"element_a\",\"b\":\"element_b\",\"cd\":0}", buffer.String())
|
||||
}
|
||||
-83
@@ -1,83 +0,0 @@
|
||||
// +-------------------------------------------------------------------------
|
||||
// | Copyright (C) 2016 Yunify, Inc.
|
||||
// +-------------------------------------------------------------------------
|
||||
// | Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// | you may not use this work except in compliance with the License.
|
||||
// | You may obtain a copy of the License in the LICENSE file, or at:
|
||||
// |
|
||||
// | http://www.apache.org/licenses/LICENSE-2.0
|
||||
// |
|
||||
// | Unless required by applicable law or agreed to in writing, software
|
||||
// | distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// | See the License for the specific language governing permissions and
|
||||
// | limitations under the License.
|
||||
// +-------------------------------------------------------------------------
|
||||
|
||||
package builder
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/yunify/qingstor-sdk-go/config"
|
||||
"github.com/yunify/qingstor-sdk-go/request/data"
|
||||
)
|
||||
|
||||
type ObjectSubServiceProperties struct {
|
||||
BucketName *string `json:"bucket-name" name:"bucket-name"`
|
||||
ObjectKey *string `json:"object-key" name:"object-key"`
|
||||
Zone *string `json:"zone" name:"zone"`
|
||||
}
|
||||
type GetObjectInput struct {
|
||||
IfMatch *string `json:"If-Match" name:"If-Match" location:"headers"`
|
||||
IfModifiedSince *time.Time `json:"If-Modified-Since" name:"If-Modified-Since" format:"RFC 822" location:"headers"`
|
||||
IfNoneMatch *string `json:"If-None-Match" name:"If-None-Match" location:"headers"`
|
||||
IfUnmodifiedSince time.Time `json:"If-Unmodified-Since" name:"If-Unmodified-Since" format:"RFC 822" location:"headers"`
|
||||
// Specified range of the Object
|
||||
Range *string `json:"Range" name:"Range" location:"headers"`
|
||||
}
|
||||
|
||||
func (i *GetObjectInput) Validate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestQingStorBuilder_BuildHTTPRequest(t *testing.T) {
|
||||
conf, err := config.NewDefault()
|
||||
assert.Nil(t, err)
|
||||
conf.Host = "qingstor.dev"
|
||||
|
||||
tz, err := time.LoadLocation("Asia/Shanghai")
|
||||
assert.Nil(t, err)
|
||||
|
||||
qsBuilder := &QingStorBuilder{}
|
||||
operation := &data.Operation{
|
||||
Config: conf,
|
||||
APIName: "GET Object",
|
||||
ServiceName: "QingStor",
|
||||
Properties: &ObjectSubServiceProperties{
|
||||
BucketName: String("test"),
|
||||
ObjectKey: String("path/to/key.txt"),
|
||||
Zone: String("beta"),
|
||||
},
|
||||
RequestMethod: "GET",
|
||||
RequestURI: "/<bucket-name>/<object-key>",
|
||||
StatusCodes: []int{
|
||||
201,
|
||||
},
|
||||
}
|
||||
inputValue := reflect.ValueOf(&GetObjectInput{
|
||||
IfModifiedSince: Time(time.Date(2016, 9, 1, 15, 30, 0, 0, tz)),
|
||||
Range: String("100-"),
|
||||
})
|
||||
httpRequest, err := qsBuilder.BuildHTTPRequest(operation, &inputValue)
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, httpRequest.Header.Get("Date"))
|
||||
assert.Equal(t, "0", httpRequest.Header.Get("Content-Length"))
|
||||
assert.Equal(t, "", httpRequest.Header.Get("If-Match"))
|
||||
assert.Equal(t, "Thu, 01 Sep 2016 07:30:00 GMT", httpRequest.Header.Get("If-Modified-Since"))
|
||||
assert.Equal(t, "100-", httpRequest.Header.Get("Range"))
|
||||
assert.Equal(t, "https://beta.qingstor.dev:443/test/path/to/key.txt", httpRequest.URL.String())
|
||||
}
|
||||
-136
@@ -1,136 +0,0 @@
|
||||
// +-------------------------------------------------------------------------
|
||||
// | Copyright (C) 2016 Yunify, Inc.
|
||||
// +-------------------------------------------------------------------------
|
||||
// | Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// | you may not use this work except in compliance with the License.
|
||||
// | You may obtain a copy of the License in the LICENSE file, or at:
|
||||
// |
|
||||
// | http://www.apache.org/licenses/LICENSE-2.0
|
||||
// |
|
||||
// | Unless required by applicable law or agreed to in writing, software
|
||||
// | distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// | See the License for the specific language governing permissions and
|
||||
// | limitations under the License.
|
||||
// +-------------------------------------------------------------------------
|
||||
|
||||
package request
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/yunify/qingstor-sdk-go/config"
|
||||
"github.com/yunify/qingstor-sdk-go/logger"
|
||||
"github.com/yunify/qingstor-sdk-go/request/data"
|
||||
"github.com/yunify/qingstor-sdk-go/request/errors"
|
||||
)
|
||||
|
||||
type SomeActionProperties struct {
|
||||
A *string `json:"a" name:"a"`
|
||||
B *string `json:"b" name:"b"`
|
||||
CD *string `json:"c-d" name:"c-d"`
|
||||
}
|
||||
|
||||
type SomeActionInput struct {
|
||||
Date *time.Time `json:"Date" name:"Date" format:"RFC 822" location:"headers"`
|
||||
IfModifiedSince *time.Time `json:"If-Modified-Since" name:"If-Modified-Since" format:"RFC 822" location:"headers"`
|
||||
Range *string `json:"Range" name:"Range" location:"headers"`
|
||||
UploadID *string `json:"upload_id" name:"upload_id" location:"query"`
|
||||
Count *int `json:"count" name:"count" location:"elements"`
|
||||
}
|
||||
|
||||
func (s *SomeActionInput) Validate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type SomeActionOutput struct {
|
||||
StatusCode *int `location:"statusCode"`
|
||||
Error *errors.QingStorError
|
||||
RequestID *string `location:"requestID"`
|
||||
}
|
||||
|
||||
func String(v string) *string {
|
||||
return &v
|
||||
}
|
||||
|
||||
func Int(v int) *int {
|
||||
return &v
|
||||
}
|
||||
|
||||
func Time(v time.Time) *time.Time {
|
||||
return &v
|
||||
}
|
||||
|
||||
func TestRequestSend(t *testing.T) {
|
||||
conf, err := config.New("ACCESS_KEY_ID", "SECRET_ACCESS_KEY")
|
||||
assert.Nil(t, err)
|
||||
logger.SetLevel("warn")
|
||||
|
||||
operation := &data.Operation{
|
||||
Config: conf,
|
||||
Properties: &SomeActionProperties{
|
||||
A: String("aaa"),
|
||||
B: String("bbb"),
|
||||
CD: String("ccc-ddd"),
|
||||
},
|
||||
APIName: "Some Action",
|
||||
RequestMethod: "GET",
|
||||
RequestURI: "/<a>/<b>/<c-d>",
|
||||
StatusCodes: []int{
|
||||
200, // OK
|
||||
206, // Partial content
|
||||
304, // Not modified
|
||||
412, // Precondition failed
|
||||
},
|
||||
}
|
||||
|
||||
output := &SomeActionOutput{}
|
||||
r, err := New(operation, &SomeActionInput{
|
||||
Date: Time(time.Date(2016, 9, 1, 15, 30, 0, 0, time.UTC)),
|
||||
IfModifiedSince: Time(time.Date(2016, 9, 1, 15, 30, 0, 0, time.UTC)),
|
||||
Range: String("100-"),
|
||||
UploadID: String("0"),
|
||||
Count: Int(23),
|
||||
}, output)
|
||||
assert.Nil(t, err)
|
||||
|
||||
err = r.build()
|
||||
assert.Nil(t, err)
|
||||
|
||||
err = r.sign()
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Equal(t, r.HTTPRequest.URL.String(), "https://qingstor.com:443/aaa/bbb/ccc-ddd?upload_id=0")
|
||||
assert.Equal(t, r.HTTPRequest.Header.Get("Range"), "100-")
|
||||
assert.Equal(t, r.HTTPRequest.Header.Get("If-Modified-Since"), "Thu, 01 Sep 2016 15:30:00 GMT")
|
||||
assert.Equal(t, r.HTTPRequest.Header.Get("Content-Length"), "12")
|
||||
assert.Equal(t, r.HTTPRequest.Header.Get("Authorization"), "QS ACCESS_KEY_ID:pA7G9qo4iQ6YHu7p4fX9Wcg4V9S6Mcgvz7p/0wEdz78=")
|
||||
|
||||
httpResponse := &http.Response{Header: http.Header{}}
|
||||
httpResponse.StatusCode = 400
|
||||
httpResponse.Header.Set("Content-Type", "application/json")
|
||||
responseString := `{
|
||||
"code": "bad_request",
|
||||
"message": "Invalid argument(s) or invalid argument value(s)",
|
||||
"request_id": "1e588695254aa08cf7a43f612e6ce14b",
|
||||
"url": "http://docs.qingcloud.com/object_storage/api/object/get.html"
|
||||
}`
|
||||
httpResponse.Body = ioutil.NopCloser(bytes.NewReader([]byte(responseString)))
|
||||
assert.Nil(t, err)
|
||||
r.HTTPResponse = httpResponse
|
||||
|
||||
err = r.unpack()
|
||||
assert.NotNil(t, err)
|
||||
|
||||
switch e := err.(type) {
|
||||
case *errors.QingStorError:
|
||||
assert.Equal(t, "bad_request", e.Code)
|
||||
assert.Equal(t, "1e588695254aa08cf7a43f612e6ce14b", e.RequestID)
|
||||
}
|
||||
}
|
||||
-127
@@ -1,127 +0,0 @@
|
||||
// +-------------------------------------------------------------------------
|
||||
// | Copyright (C) 2016 Yunify, Inc.
|
||||
// +-------------------------------------------------------------------------
|
||||
// | Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// | you may not use this work except in compliance with the License.
|
||||
// | You may obtain a copy of the License in the LICENSE file, or at:
|
||||
// |
|
||||
// | http://www.apache.org/licenses/LICENSE-2.0
|
||||
// |
|
||||
// | Unless required by applicable law or agreed to in writing, software
|
||||
// | distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// | See the License for the specific language governing permissions and
|
||||
// | limitations under the License.
|
||||
// +-------------------------------------------------------------------------
|
||||
|
||||
package signer
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/pengsrc/go-shared/convert"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestQingStorSignerWriteSignature(t *testing.T) {
|
||||
url := "https://qingstor.com/?acl&upload_id=fde133b5f6d932cd9c79bac3c7318da1&part_number=0&other=abc"
|
||||
httpRequest, err := http.NewRequest("GET", url, nil)
|
||||
httpRequest.Header.Set("Date", convert.TimeToString(time.Time{}, convert.RFC822))
|
||||
httpRequest.Header.Set("X-QS-Test-2", "Test 2")
|
||||
httpRequest.Header.Set("X-QS-Test-1", "Test 1")
|
||||
assert.Nil(t, err)
|
||||
|
||||
s := QingStorSigner{
|
||||
AccessKeyID: "ENV_ACCESS_KEY_ID",
|
||||
SecretAccessKey: "ENV_SECRET_ACCESS_KEY",
|
||||
}
|
||||
|
||||
err = s.WriteSignature(httpRequest)
|
||||
assert.Nil(t, err)
|
||||
|
||||
signature := "QS ENV_ACCESS_KEY_ID:bvglZF9iMOv1RaCTxPYWxexmt1UN2m5WKngYnhDEp2c="
|
||||
assert.Equal(t, signature, httpRequest.Header.Get("Authorization"))
|
||||
}
|
||||
|
||||
func TestQingStorSignerWriteSignatureWithXQSDate(t *testing.T) {
|
||||
url := "https://qingstor.com/?acl&upload_id=fde133b5f6d932cd9c79bac3c7318da1&part_number=0&other=abc"
|
||||
httpRequest, err := http.NewRequest("GET", url, nil)
|
||||
httpRequest.Header.Set("Date", convert.TimeToString(time.Time{}, convert.RFC822))
|
||||
httpRequest.Header.Set("X-QS-Date", convert.TimeToString(time.Time{}, convert.RFC822))
|
||||
httpRequest.Header.Set("X-QS-Test-2", "Test 2")
|
||||
httpRequest.Header.Set("X-QS-Test-1", "Test 1")
|
||||
assert.Nil(t, err)
|
||||
|
||||
s := QingStorSigner{
|
||||
AccessKeyID: "ENV_ACCESS_KEY_ID",
|
||||
SecretAccessKey: "ENV_SECRET_ACCESS_KEY",
|
||||
}
|
||||
|
||||
err = s.WriteSignature(httpRequest)
|
||||
assert.Nil(t, err)
|
||||
|
||||
signature := "QS ENV_ACCESS_KEY_ID:qkY+tOMdqfDAVv+ZBtlWeEBxlbyIKaQmj5lQlylENzo="
|
||||
assert.Equal(t, signature, httpRequest.Header.Get("Authorization"))
|
||||
}
|
||||
|
||||
func TestQingStorSignerWriteSignatureChinese(t *testing.T) {
|
||||
url := "https://zone.qingstor.com/bucket-name/中文"
|
||||
httpRequest, err := http.NewRequest("GET", url, nil)
|
||||
httpRequest.Header.Set("Date", convert.TimeToString(time.Time{}, convert.RFC822))
|
||||
assert.Nil(t, err)
|
||||
|
||||
s := QingStorSigner{
|
||||
AccessKeyID: "ENV_ACCESS_KEY_ID",
|
||||
SecretAccessKey: "ENV_SECRET_ACCESS_KEY",
|
||||
}
|
||||
|
||||
err = s.WriteSignature(httpRequest)
|
||||
assert.Nil(t, err)
|
||||
|
||||
signature := "QS ENV_ACCESS_KEY_ID:XsTXX50kzqBf92zLG1aIUIJmZ0hqIHoaHgkumwnV3fs="
|
||||
assert.Equal(t, signature, httpRequest.Header.Get("Authorization"))
|
||||
}
|
||||
|
||||
func TestQingStorSignerWriteQuerySignature(t *testing.T) {
|
||||
url := "https://qingstor.com/?acl&upload_id=fde133b5f6d932cd9c79bac3c7318da1&part_number=0"
|
||||
httpRequest, err := http.NewRequest("GET", url, nil)
|
||||
httpRequest.Header.Set("Date", convert.TimeToString(time.Time{}, convert.RFC822))
|
||||
httpRequest.Header.Set("X-QS-Test-2", "Test 2")
|
||||
httpRequest.Header.Set("X-QS-Test-1", "Test 1")
|
||||
assert.Nil(t, err)
|
||||
|
||||
s := QingStorSigner{
|
||||
AccessKeyID: "ENV_ACCESS_KEY_ID",
|
||||
SecretAccessKey: "ENV_SECRET_ACCESS_KEY",
|
||||
}
|
||||
|
||||
err = s.WriteQuerySignature(httpRequest, 3600)
|
||||
assert.Nil(t, err)
|
||||
|
||||
targetURL := "https://qingstor.com/?acl&upload_id=fde133b5f6d932cd9c79bac3c7318da1&part_number=0&access_key_id=ENV_ACCESS_KEY_ID&expires=3600&signature=GRL3p3NOgHR9CQygASvyo344vdnO1hFke6ZvQ5mDVHM="
|
||||
assert.Equal(t, httpRequest.URL.String(), targetURL)
|
||||
}
|
||||
|
||||
func TestQingStorSignerWriteQuerySignatureWithXQSDate(t *testing.T) {
|
||||
url := "https://qingstor.com/?acl&upload_id=fde133b5f6d932cd9c79bac3c7318da1&part_number=0"
|
||||
httpRequest, err := http.NewRequest("GET", url, nil)
|
||||
httpRequest.Header.Set("Date", convert.TimeToString(time.Time{}, convert.RFC822))
|
||||
httpRequest.Header.Set("X-QS-Date", convert.TimeToString(time.Time{}, convert.RFC822))
|
||||
httpRequest.Header.Set("X-QS-Test-2", "Test 2")
|
||||
httpRequest.Header.Set("X-QS-Test-1", "Test 1")
|
||||
assert.Nil(t, err)
|
||||
|
||||
s := QingStorSigner{
|
||||
AccessKeyID: "ENV_ACCESS_KEY_ID",
|
||||
SecretAccessKey: "ENV_SECRET_ACCESS_KEY",
|
||||
}
|
||||
|
||||
err = s.WriteQuerySignature(httpRequest, 3600)
|
||||
assert.Nil(t, err)
|
||||
|
||||
targetURL := "https://qingstor.com/?acl&upload_id=fde133b5f6d932cd9c79bac3c7318da1&part_number=0&access_key_id=ENV_ACCESS_KEY_ID&expires=3600&signature=plFxMFP1EzKVtdF%2BbApT8rhW9AUAIWfmZcOGH3m27t0="
|
||||
assert.Equal(t, httpRequest.URL.String(), targetURL)
|
||||
}
|
||||
-85
@@ -1,85 +0,0 @@
|
||||
// +-------------------------------------------------------------------------
|
||||
// | Copyright (C) 2016 Yunify, Inc.
|
||||
// +-------------------------------------------------------------------------
|
||||
// | Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// | you may not use this work except in compliance with the License.
|
||||
// | You may obtain a copy of the License in the LICENSE file, or at:
|
||||
// |
|
||||
// | http://www.apache.org/licenses/LICENSE-2.0
|
||||
// |
|
||||
// | Unless required by applicable law or agreed to in writing, software
|
||||
// | distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// | See the License for the specific language governing permissions and
|
||||
// | limitations under the License.
|
||||
// +-------------------------------------------------------------------------
|
||||
|
||||
package unpacker
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/yunify/qingstor-sdk-go/request/data"
|
||||
)
|
||||
|
||||
func StringValue(v *string) string {
|
||||
if v != nil {
|
||||
return *v
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func IntValue(v *int) int {
|
||||
if v != nil {
|
||||
return *v
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func Int64Value(v *int64) int64 {
|
||||
if v != nil {
|
||||
return *v
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func TimeValue(v *time.Time) time.Time {
|
||||
if v != nil {
|
||||
return *v
|
||||
}
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
func TestBaseUnpacker_UnpackHTTPRequest(t *testing.T) {
|
||||
type FakeOutput struct {
|
||||
StatusCode *int
|
||||
|
||||
A *string `location:"elements" json:"a" name:"a"`
|
||||
B *string `location:"elements" json:"b" name:"b"`
|
||||
CD *int `location:"elements" json:"cd" name:"cd"`
|
||||
EF *int64 `location:"elements" json:"ef" name:"ef"`
|
||||
}
|
||||
|
||||
httpResponse := &http.Response{Header: http.Header{}}
|
||||
httpResponse.StatusCode = 200
|
||||
httpResponse.Header.Set("Content-Type", "application/json")
|
||||
responseString := `{"a": "el_a", "b": "el_b", "cd": 1024, "ef": 2048}`
|
||||
httpResponse.Body = ioutil.NopCloser(bytes.NewReader([]byte(responseString)))
|
||||
|
||||
output := &FakeOutput{}
|
||||
outputValue := reflect.ValueOf(output)
|
||||
unpacker := BaseUnpacker{}
|
||||
err := unpacker.UnpackHTTPRequest(&data.Operation{}, httpResponse, &outputValue)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 200, IntValue(output.StatusCode))
|
||||
assert.Equal(t, "el_a", StringValue(output.A))
|
||||
assert.Equal(t, "el_b", StringValue(output.B))
|
||||
assert.Equal(t, 1024, IntValue(output.CD))
|
||||
assert.Equal(t, int64(2048), Int64Value(output.EF))
|
||||
}
|
||||
-150
@@ -1,150 +0,0 @@
|
||||
// +-------------------------------------------------------------------------
|
||||
// | Copyright (C) 2016 Yunify, Inc.
|
||||
// +-------------------------------------------------------------------------
|
||||
// | Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// | you may not use this work except in compliance with the License.
|
||||
// | You may obtain a copy of the License in the LICENSE file, or at:
|
||||
// |
|
||||
// | http://www.apache.org/licenses/LICENSE-2.0
|
||||
// |
|
||||
// | Unless required by applicable law or agreed to in writing, software
|
||||
// | distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// | See the License for the specific language governing permissions and
|
||||
// | limitations under the License.
|
||||
// +-------------------------------------------------------------------------
|
||||
|
||||
package unpacker
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/yunify/qingstor-sdk-go/request/data"
|
||||
"github.com/yunify/qingstor-sdk-go/request/errors"
|
||||
)
|
||||
|
||||
func TestQingStorUnpacker_UnpackHTTPRequest(t *testing.T) {
|
||||
type Bucket struct {
|
||||
// Created time of the Bucket
|
||||
Created *time.Time `json:"created" name:"created" format:"RFC 822"`
|
||||
// QingCloud Zone ID
|
||||
Location *string `json:"location" name:"location"`
|
||||
// Bucket name
|
||||
Name *string `json:"name" name:"name"`
|
||||
// URL to access the Bucket
|
||||
URL *string `json:"url" name:"url"`
|
||||
}
|
||||
|
||||
type ListBucketsOutput struct {
|
||||
StatusCode *int `location:"statusCode"`
|
||||
Error *errors.QingStorError
|
||||
RequestID *string `location:"requestID"`
|
||||
|
||||
XTestHeader *string `json:"X-Test-Header" name:"X-Test-Header" location:"headers"`
|
||||
XTestTime *time.Time `json:"X-Test-Time" name:"X-Test-Time" format:"RFC 822" location:"headers"`
|
||||
|
||||
// Buckets information
|
||||
Buckets []*Bucket `json:"buckets" name:"buckets"`
|
||||
// Bucket count
|
||||
Count *int `json:"count" name:"count"`
|
||||
}
|
||||
|
||||
httpResponse := &http.Response{Header: http.Header{
|
||||
"X-Test-Header": []string{"test-header"},
|
||||
"X-Test-Time": []string{"Thu, 01 Sep 2016 07:30:00 GMT"},
|
||||
}}
|
||||
httpResponse.StatusCode = 200
|
||||
httpResponse.Header.Set("Content-Type", "application/json")
|
||||
responseString := `{
|
||||
"count": 2,
|
||||
"buckets": [
|
||||
{
|
||||
"name": "test-bucket",
|
||||
"location": "pek3a",
|
||||
"url": "https://test-bucket.pek3a.qingstor.com",
|
||||
"created": "2015-07-11T04:45:57Z"
|
||||
},
|
||||
{
|
||||
"name": "test-photos",
|
||||
"location": "pek3a",
|
||||
"url": "https://test-photos.pek3a.qingstor.com",
|
||||
"created": "2015-07-12T09:40:32Z"
|
||||
}
|
||||
]
|
||||
}`
|
||||
httpResponse.Body = ioutil.NopCloser(bytes.NewReader([]byte(responseString)))
|
||||
|
||||
output := &ListBucketsOutput{}
|
||||
outputValue := reflect.ValueOf(output)
|
||||
unpacker := QingStorUnpacker{}
|
||||
err := unpacker.UnpackHTTPRequest(&data.Operation{}, httpResponse, &outputValue)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "test-header", StringValue(output.XTestHeader))
|
||||
assert.Equal(t, time.Date(2016, 9, 1, 7, 30, 0, 0, time.UTC), TimeValue(output.XTestTime))
|
||||
assert.Equal(t, 2, IntValue(output.Count))
|
||||
assert.Equal(t, "test-bucket", StringValue(output.Buckets[0].Name))
|
||||
assert.Equal(t, "pek3a", StringValue(output.Buckets[0].Location))
|
||||
assert.Equal(t, time.Date(2015, 7, 12, 9, 40, 32, 0, time.UTC), TimeValue(output.Buckets[1].Created))
|
||||
}
|
||||
|
||||
func TestQingStorUnpacker_UnpackHTTPRequestWithError(t *testing.T) {
|
||||
type ListBucketsOutput struct {
|
||||
StatusCode *int `location:"statusCode"`
|
||||
Error *errors.QingStorError
|
||||
RequestID *string `location:"requestID"`
|
||||
}
|
||||
|
||||
httpResponse := &http.Response{Header: http.Header{}}
|
||||
httpResponse.StatusCode = 400
|
||||
httpResponse.Header.Set("Content-Type", "application/json")
|
||||
responseString := `{
|
||||
"code": "bad_request",
|
||||
"message": "Invalid argument(s) or invalid argument value(s)",
|
||||
"request_id": "aa08cf7a43f611e5886952542e6ce14b",
|
||||
"url": "http://docs.qingcloud.com/object_storage/api/bucket/get.html"
|
||||
}`
|
||||
httpResponse.Body = ioutil.NopCloser(bytes.NewReader([]byte(responseString)))
|
||||
|
||||
output := &ListBucketsOutput{}
|
||||
outputValue := reflect.ValueOf(output)
|
||||
unpacker := QingStorUnpacker{}
|
||||
err := unpacker.UnpackHTTPRequest(&data.Operation{}, httpResponse, &outputValue)
|
||||
assert.NotNil(t, err)
|
||||
switch e := err.(type) {
|
||||
case *errors.QingStorError:
|
||||
assert.Equal(t, "bad_request", e.Code)
|
||||
assert.Equal(t, "aa08cf7a43f611e5886952542e6ce14b", e.RequestID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestQingStorUnpacker_UnpackHeadHTTPRequestWithError(t *testing.T) {
|
||||
type HeadBucketsOutput struct {
|
||||
StatusCode *int `location:"statusCode"`
|
||||
Error *errors.QingStorError
|
||||
RequestID *string `location:"requestID"`
|
||||
}
|
||||
|
||||
httpResponse := &http.Response{Header: http.Header{}}
|
||||
httpResponse.StatusCode = 404
|
||||
httpResponse.Header.Set("Content-Type", "application/json")
|
||||
httpResponse.Header.Set("X-QS-Request-ID", "aa08cf7a43f611e5886952542e6ce14b")
|
||||
httpResponse.Body = ioutil.NopCloser(strings.NewReader(""))
|
||||
|
||||
output := &HeadBucketsOutput{}
|
||||
outputValue := reflect.ValueOf(output)
|
||||
unpacker := QingStorUnpacker{}
|
||||
err := unpacker.UnpackHTTPRequest(&data.Operation{}, httpResponse, &outputValue)
|
||||
assert.NotNil(t, err)
|
||||
switch e := err.(type) {
|
||||
case *errors.QingStorError:
|
||||
assert.Equal(t, "aa08cf7a43f611e5886952542e6ce14b", e.RequestID)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user