-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd5368a
commit c4e1182
Showing
5 changed files
with
115 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|