Skip to content

Commit

Permalink
Add test cases for response.go
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Nov 12, 2018
1 parent bd5368a commit c4e1182
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 65 deletions.
5 changes: 3 additions & 2 deletions sdk/responses/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ import (
"bytes"
"encoding/xml"
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/errors"
"io/ioutil"
"net/http"
"strings"

"github.com/aliyun/alibaba-cloud-sdk-go/sdk/errors"
)

type AcsResponse interface {
Expand Down Expand Up @@ -117,7 +118,7 @@ func (baseResponse *BaseResponse) parseFromHttpResponse(httpResponse *http.Respo
func (baseResponse *BaseResponse) String() string {
resultBuilder := bytes.Buffer{}
// statusCode
resultBuilder.WriteString("\n")
// resultBuilder.WriteString("\n")
resultBuilder.WriteString(fmt.Sprintf("%s %s\n", baseResponse.originHttpResponse.Proto, baseResponse.originHttpResponse.Status))
// httpHeaders
//resultBuilder.WriteString("Headers:\n")
Expand Down
51 changes: 51 additions & 0 deletions sdk/responses/response_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package responses

import (
"bytes"
"io"
"io/ioutil"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_CommonResponse(t *testing.T) {
r := NewCommonResponse()
assert.NotNil(t, r)

assert.Equal(t, 0, r.GetHttpStatus())
// assert.Equal(t, nil, r.GetHttpHeaders())
assert.Equal(t, "", r.GetHttpContentString())
assert.Equal(t, 0, len(r.GetHttpContentBytes()))
assert.Nil(t, r.GetOriginHttpResponse())
assert.False(t, r.IsSuccess())
}

func Test_CommonResponse_parseFromHttpResponse(t *testing.T) {
r := NewCommonResponse()
header := make(http.Header)
status := "200"
statusCode := 200
res := &http.Response{
Proto: "HTTP/1.1",
ProtoMajor: 1,
Header: header,
StatusCode: statusCode,
Status: status + " " + http.StatusText(statusCode),
}
var noBody io.ReadCloser = ioutil.NopCloser(bytes.NewReader(nil))
res.Header = make(http.Header)
res.Header.Add("Server", "GitHub.com")
res.Body = noBody
r.parseFromHttpResponse(res)
expected := `HTTP/1.1 200 OK
Server: GitHub.com
`

assert.True(t, r.IsSuccess())
assert.Equal(t, "GitHub.com", r.GetHttpHeaders()["Server"][0])
assert.Equal(t, expected, r.String())
}
34 changes: 17 additions & 17 deletions sdk/utils/debug.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
package utils

import (
"fmt"
"strings"
"os"
"fmt"
"os"
"strings"
)

type Debug func(format string, v ...interface{})

func Init(flag string) Debug {
enable := false
enable := false

env := os.Getenv("DEBUG")
parts := strings.Split(env, ",")
for _, part := range parts {
if part == flag {
enable = true
break
}
}
env := os.Getenv("DEBUG")
parts := strings.Split(env, ",")
for _, part := range parts {
if part == flag {
enable = true
break
}
}

return func (format string, v ...interface{}) {
if enable {
fmt.Println(fmt.Sprintf(format, v...))
}
}
return func(format string, v ...interface{}) {
if enable {
fmt.Println(fmt.Sprintf(format, v...))
}
}
}
7 changes: 3 additions & 4 deletions sdk/utils/debug_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package utils

import (
"testing"
// "fmt"
"testing"
)

func TestMain(t *testing.T) {
debug := Init("sdk")
debug("%s", "testing")
debug := Init("sdk")
debug("%s", "testing")
}
83 changes: 41 additions & 42 deletions sdk/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,65 +1,64 @@
package utils

import (
"testing"
"time"
"regexp"
"regexp"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
)

func TestInitStructWithDefaultTag(t *testing.T) {
config := &struct {
B bool `default:"true"`;
S string `default:"default string"`;
I int `default:"10"`
T time.Duration `default:"100"`
E int `default:""`
}{}
InitStructWithDefaultTag(config)
assert.NotNil(t, config)
assert.Equal(t, true, config.B)
assert.Equal(t, "default string", config.S)
assert.Equal(t, 10, config.I)
assert.Equal(t, time.Duration(100), config.T)
assert.Equal(t, 0, config.E)
config := &struct {
B bool `default:"true"`
S string `default:"default string"`
I int `default:"10"`
T time.Duration `default:"100"`
E int `default:""`
}{}
InitStructWithDefaultTag(config)
assert.NotNil(t, config)
assert.Equal(t, true, config.B)
assert.Equal(t, "default string", config.S)
assert.Equal(t, 10, config.I)
assert.Equal(t, time.Duration(100), config.T)
assert.Equal(t, 0, config.E)
}

func TestGetUUIDV4(t *testing.T) {
uuid := GetUUIDV4()
assert.Equal(t, 32, len(uuid))
assert.NotEqual(t, GetUUIDV4(), GetUUIDV4())
uuid := GetUUIDV4()
assert.Equal(t, 32, len(uuid))
assert.NotEqual(t, GetUUIDV4(), GetUUIDV4())
}

func TestGetMD5Base64(t *testing.T) {
assert.Equal(t, "ERIHLmRX2uZmssDdxQnnxQ==",
GetMD5Base64([]byte("That's all folks!!")))
assert.Equal(t, "GsJRdI3kAbAnHo/0+3wWJw==",
GetMD5Base64([]byte("中文也没啥问题")))
assert.Equal(t, "ERIHLmRX2uZmssDdxQnnxQ==",
GetMD5Base64([]byte("That's all folks!!")))
assert.Equal(t, "GsJRdI3kAbAnHo/0+3wWJw==",
GetMD5Base64([]byte("中文也没啥问题")))
}

func TestGetTimeInFormatRFC2616(t *testing.T) {
s := GetTimeInFormatRFC2616()
assert.Equal(t, 29, len(s))
re := regexp.MustCompile(`^[A-Z][a-z]{2}, [0-9]{2} [A-Z][a-z]{2} [0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2} GMT$`)
assert.True(t, re.MatchString(s))
s := GetTimeInFormatRFC2616()
assert.Equal(t, 29, len(s))
re := regexp.MustCompile(`^[A-Z][a-z]{2}, [0-9]{2} [A-Z][a-z]{2} [0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2} GMT$`)
assert.True(t, re.MatchString(s))
}

func TestGetTimeInFormatISO8601(t *testing.T) {
s := GetTimeInFormatISO8601()
assert.Equal(t, 20, len(s))
// 2006-01-02T15:04:05Z
re := regexp.MustCompile(`^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$`)
assert.True(t, re.MatchString(s))
s := GetTimeInFormatISO8601()
assert.Equal(t, 20, len(s))
// 2006-01-02T15:04:05Z
re := regexp.MustCompile(`^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$`)
assert.True(t, re.MatchString(s))
}

func TestGetUrlFormedMap(t *testing.T) {
m := make(map[string]string)
m["key"] = "value"
s := GetUrlFormedMap(m)
assert.Equal(t, "key=value", s)
m["key2"] = "http://domain/?key=value&key2=value2"
s2 := GetUrlFormedMap(m)
assert.Equal(t, "key=value&key2=http%3A%2F%2Fdomain%2F%3Fkey%3Dvalue%26key2%3Dvalue2", s2)
m := make(map[string]string)
m["key"] = "value"
s := GetUrlFormedMap(m)
assert.Equal(t, "key=value", s)
m["key2"] = "http://domain/?key=value&key2=value2"
s2 := GetUrlFormedMap(m)
assert.Equal(t, "key=value&key2=http%3A%2F%2Fdomain%2F%3Fkey%3Dvalue%26key2%3Dvalue2", s2)
}

0 comments on commit c4e1182

Please sign in to comment.