فهرست منبع

Add test cases for signature_does_not_match_wrapper.go

Jackson Tian 7 سال پیش
والد
کامیت
246a198554

+ 3 - 3
sdk/errors/server_error.go

@@ -36,7 +36,7 @@ type ServerError struct {
 }
 
 type ServerErrorWrapper interface {
-	tryWrap(error *ServerError, wrapInfo map[string]string) (bool, *ServerError)
+	tryWrap(error *ServerError, wrapInfo map[string]string) bool
 }
 
 func (err *ServerError) Error() string {
@@ -82,9 +82,9 @@ func NewServerError(httpStatus int, responseContent, comment string) Error {
 
 func WrapServerError(originError *ServerError, wrapInfo map[string]string) *ServerError {
 	for _, wrapper := range wrapperList {
-		ok, newError := wrapper.tryWrap(originError, wrapInfo)
+		ok := wrapper.tryWrap(originError, wrapInfo)
 		if ok {
-			return newError
+			return originError
 		}
 	}
 	return originError

+ 28 - 0
sdk/errors/server_error_test.go

@@ -37,3 +37,31 @@ func TestServerErrorWithContent(t *testing.T) {
 	assert.Equal(t, "message", serverError.Message())
 	assert.Equal(t, "SDK.ServerError\nErrorCode: InvalidAK\nRecommend: commentrecommend\nRequestId: request id\nMessage: message", serverError.Error())
 }
+
+func TestWrapServerError(t *testing.T) {
+	err := NewServerError(400, `{"Code":"SignatureDoesNotMatch","Message":"Specified signature is not matched with our calculation. server string to sign is:hehe"}`, "comment")
+	se, ok := err.(*ServerError)
+	assert.True(t, ok)
+	assert.Equal(t, "SignatureDoesNotMatch", se.ErrorCode())
+	m := make(map[string]string)
+	m["StringToSign"] = "not match"
+	WrapServerError(se, m)
+	assert.Equal(t, "This may be a bug with the SDK and we hope you can submit this question in the github issue(https://github.com/aliyun/alibaba-cloud-sdk-go/issues), thanks very much", se.Recommend())
+
+	err = NewServerError(400, `{"Code":"SignatureDoesNotMatch","Message":"Specified signature is not matched with our calculation. server string to sign is:match"}`, "comment")
+	se, ok = err.(*ServerError)
+	assert.True(t, ok)
+	assert.Equal(t, "SignatureDoesNotMatch", se.ErrorCode())
+	m = make(map[string]string)
+	m["StringToSign"] = "match"
+	WrapServerError(se, m)
+	assert.Equal(t, "Please check you AccessKeySecret", se.Recommend())
+
+	err = NewServerError(400, `{"Code":"Other"}`, "comment")
+	se, ok = err.(*ServerError)
+	assert.True(t, ok)
+	assert.Equal(t, "Other", se.ErrorCode())
+	m = make(map[string]string)
+	WrapServerError(se, m)
+	assert.Equal(t, "", se.Recommend())
+}

+ 5 - 4
sdk/errors/signature_does_not_match_wrapper.go

@@ -8,7 +8,7 @@ const MessagePrefix = "Specified signature is not matched with our calculation.
 type SignatureDostNotMatchWrapper struct {
 }
 
-func (*SignatureDostNotMatchWrapper) tryWrap(error *ServerError, wrapInfo map[string]string) (bool, *ServerError) {
+func (*SignatureDostNotMatchWrapper) tryWrap(error *ServerError, wrapInfo map[string]string) (ok bool) {
 	clientStringToSign := wrapInfo["StringToSign"]
 	if error.errorCode == SignatureDostNotMatchErrorCode && clientStringToSign != "" {
 		message := error.message
@@ -22,8 +22,9 @@ func (*SignatureDostNotMatchWrapper) tryWrap(error *ServerError, wrapInfo map[st
 					"github issue(https://github.com/aliyun/alibaba-cloud-sdk-go/issues), thanks very much"
 			}
 		}
-		return true, error
-	} else {
-		return false, nil
+		ok = true
+		return
 	}
+	ok = false
+	return
 }

+ 43 - 0
sdk/errors/signature_does_not_match_wrapper_test.go

@@ -0,0 +1,43 @@
+package errors
+
+import (
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestWrap(t *testing.T) {
+	wrapper := &SignatureDostNotMatchWrapper{}
+	m := make(map[string]string)
+	err := NewServerError(400, "content", "comment")
+	se, ok := err.(*ServerError)
+	assert.True(t, ok)
+	wrapped := wrapper.tryWrap(se, m)
+	assert.False(t, wrapped)
+}
+
+func TestWrapNotMatch(t *testing.T) {
+	wrapper := &SignatureDostNotMatchWrapper{}
+	err := NewServerError(400, `{"Code":"SignatureDoesNotMatch","Message":"Specified signature is not matched with our calculation. server string to sign is:hehe"}`, "comment")
+	se, ok := err.(*ServerError)
+	assert.True(t, ok)
+	assert.Equal(t, "SignatureDoesNotMatch", se.ErrorCode())
+	m := make(map[string]string)
+	m["StringToSign"] = "not match"
+	wrapped := wrapper.tryWrap(se, m)
+	assert.True(t, wrapped)
+	assert.Equal(t, "This may be a bug with the SDK and we hope you can submit this question in the github issue(https://github.com/aliyun/alibaba-cloud-sdk-go/issues), thanks very much", se.Recommend())
+}
+
+func TestWrapMatch(t *testing.T) {
+	wrapper := &SignatureDostNotMatchWrapper{}
+	err := NewServerError(400, `{"Code":"SignatureDoesNotMatch","Message":"Specified signature is not matched with our calculation. server string to sign is:match"}`, "comment")
+	se, ok := err.(*ServerError)
+	assert.True(t, ok)
+	assert.Equal(t, "SignatureDoesNotMatch", se.ErrorCode())
+	m := make(map[string]string)
+	m["StringToSign"] = "match"
+	wrapped := wrapper.tryWrap(se, m)
+	assert.True(t, wrapped)
+	assert.Equal(t, "Please check you AccessKeySecret", se.Recommend())
+}