Merge develop
This commit is contained in:
194
client/cache/cache_test.go
vendored
194
client/cache/cache_test.go
vendored
@@ -18,16 +18,17 @@ import (
|
||||
"context"
|
||||
"math"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCacheIncr(t *testing.T) {
|
||||
bm, err := NewCache("memory", `{"interval":20}`)
|
||||
if err != nil {
|
||||
t.Error("init err")
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
// timeoutDuration := 10 * time.Second
|
||||
|
||||
bm.Put(context.Background(), "edwardhey", 0, time.Second*20)
|
||||
@@ -48,9 +49,7 @@ func TestCacheIncr(t *testing.T) {
|
||||
|
||||
func TestCache(t *testing.T) {
|
||||
bm, err := NewCache("memory", `{"interval":1}`)
|
||||
if err != nil {
|
||||
t.Error("init err")
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
timeoutDuration := 5 * time.Second
|
||||
if err = bm.Put(context.Background(), "astaxie", 1, timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
@@ -81,70 +80,48 @@ func TestCache(t *testing.T) {
|
||||
testDecrOverFlow(t, bm, timeoutDuration)
|
||||
|
||||
bm.Delete(context.Background(), "astaxie")
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie"); res {
|
||||
t.Error("delete err")
|
||||
}
|
||||
res, _ := bm.IsExist(context.Background(), "astaxie")
|
||||
assert.False(t, res)
|
||||
|
||||
// test GetMulti
|
||||
if err = bm.Put(context.Background(), "astaxie", "author", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie"); !res {
|
||||
t.Error("check err")
|
||||
}
|
||||
if v, _ := bm.Get(context.Background(), "astaxie"); v.(string) != "author" {
|
||||
t.Error("get err")
|
||||
}
|
||||
assert.Nil(t, bm.Put(context.Background(), "astaxie", "author", timeoutDuration))
|
||||
|
||||
if err = bm.Put(context.Background(), "astaxie1", "author1", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie1"); !res {
|
||||
t.Error("check err")
|
||||
}
|
||||
res, _ = bm.IsExist(context.Background(), "astaxie")
|
||||
assert.True(t, res)
|
||||
|
||||
v, _ := bm.Get(context.Background(), "astaxie")
|
||||
assert.Equal(t, "author", v)
|
||||
|
||||
assert.Nil(t, bm.Put(context.Background(), "astaxie1", "author1", timeoutDuration))
|
||||
|
||||
res, _ = bm.IsExist(context.Background(), "astaxie1")
|
||||
assert.True(t, res)
|
||||
|
||||
vv, _ := bm.GetMulti(context.Background(), []string{"astaxie", "astaxie1"})
|
||||
if len(vv) != 2 {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if vv[0].(string) != "author" {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if vv[1].(string) != "author1" {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
assert.Equal(t, 2, len(vv))
|
||||
assert.Equal(t, "author", vv[0])
|
||||
assert.Equal(t,"author1", vv[1])
|
||||
|
||||
|
||||
|
||||
vv, err = bm.GetMulti(context.Background(), []string{"astaxie0", "astaxie1"})
|
||||
if len(vv) != 2 {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if vv[0] != nil {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if vv[1].(string) != "author1" {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if err != nil && err.Error() != "key [astaxie0] error: the key isn't exist" {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
assert.Equal(t, 2, len(vv))
|
||||
assert.Nil(t, vv[0])
|
||||
assert.Equal(t, "author1", vv[1])
|
||||
|
||||
assert.NotNil(t, err)
|
||||
assert.True(t, strings.Contains(err.Error(), "key isn't exist"))
|
||||
}
|
||||
|
||||
func TestFileCache(t *testing.T) {
|
||||
bm, err := NewCache("file", `{"CachePath":"cache","FileSuffix":".bin","DirectoryLevel":"2","EmbedExpiry":"0"}`)
|
||||
if err != nil {
|
||||
t.Error("init err")
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
timeoutDuration := 10 * time.Second
|
||||
if err = bm.Put(context.Background(), "astaxie", 1, timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie"); !res {
|
||||
t.Error("check err")
|
||||
}
|
||||
assert.Nil(t, bm.Put(context.Background(), "astaxie", 1, timeoutDuration))
|
||||
|
||||
if v, _ := bm.Get(context.Background(), "astaxie"); v.(int) != 1 {
|
||||
t.Error("get err")
|
||||
}
|
||||
res, _ := bm.IsExist(context.Background(), "astaxie")
|
||||
assert.True(t, res)
|
||||
v, _ := bm.Get(context.Background(), "astaxie")
|
||||
assert.Equal(t, 1, v)
|
||||
|
||||
// test different integer type for incr & decr
|
||||
testMultiTypeIncrDecr(t, bm, timeoutDuration)
|
||||
@@ -154,54 +131,35 @@ func TestFileCache(t *testing.T) {
|
||||
testDecrOverFlow(t, bm, timeoutDuration)
|
||||
|
||||
bm.Delete(context.Background(), "astaxie")
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie"); res {
|
||||
t.Error("delete err")
|
||||
}
|
||||
res, _ = bm.IsExist(context.Background(), "astaxie")
|
||||
assert.False(t, res)
|
||||
|
||||
// test string
|
||||
if err = bm.Put(context.Background(), "astaxie", "author", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie"); !res {
|
||||
t.Error("check err")
|
||||
}
|
||||
if v, _ := bm.Get(context.Background(), "astaxie"); v.(string) != "author" {
|
||||
t.Error("get err")
|
||||
}
|
||||
assert.Nil(t, bm.Put(context.Background(), "astaxie", "author", timeoutDuration))
|
||||
res, _ = bm.IsExist(context.Background(), "astaxie")
|
||||
assert.True(t, res)
|
||||
|
||||
v, _ = bm.Get(context.Background(), "astaxie")
|
||||
assert.Equal(t, "author", v)
|
||||
|
||||
// test GetMulti
|
||||
if err = bm.Put(context.Background(), "astaxie1", "author1", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie1"); !res {
|
||||
t.Error("check err")
|
||||
}
|
||||
assert.Nil(t, bm.Put(context.Background(), "astaxie1", "author1", timeoutDuration))
|
||||
|
||||
res, _ = bm.IsExist(context.Background(), "astaxie1")
|
||||
assert.True(t, res)
|
||||
|
||||
vv, _ := bm.GetMulti(context.Background(), []string{"astaxie", "astaxie1"})
|
||||
if len(vv) != 2 {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if vv[0].(string) != "author" {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if vv[1].(string) != "author1" {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
assert.Equal(t, 2, len(vv))
|
||||
assert.Equal(t, "author", vv[0])
|
||||
assert.Equal(t, "author1", vv[1])
|
||||
|
||||
vv, err = bm.GetMulti(context.Background(), []string{"astaxie0", "astaxie1"})
|
||||
if len(vv) != 2 {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if vv[0] != nil {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if vv[1].(string) != "author1" {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if err == nil {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
assert.Equal(t, 2, len(vv))
|
||||
|
||||
assert.Nil(t, vv[0])
|
||||
|
||||
assert.Equal(t, "author1", vv[1])
|
||||
assert.NotNil(t, err)
|
||||
os.RemoveAll("cache")
|
||||
}
|
||||
|
||||
@@ -215,53 +173,33 @@ func testMultiTypeIncrDecr(t *testing.T, c Cache, timeout time.Duration) {
|
||||
}
|
||||
|
||||
func testIncrDecr(t *testing.T, c Cache, beforeIncr interface{}, afterIncr interface{}, timeout time.Duration) {
|
||||
var err error
|
||||
ctx := context.Background()
|
||||
key := "incDecKey"
|
||||
if err = c.Put(ctx, key, beforeIncr, timeout); err != nil {
|
||||
t.Error("Get Error", err)
|
||||
}
|
||||
|
||||
if err = c.Incr(ctx, key); err != nil {
|
||||
t.Error("Incr Error", err)
|
||||
}
|
||||
assert.Nil(t, c.Put(ctx, key, beforeIncr, timeout))
|
||||
assert.Nil(t, c.Incr(ctx, key))
|
||||
|
||||
if v, _ := c.Get(ctx, key); v != afterIncr {
|
||||
t.Error("Get Error")
|
||||
}
|
||||
|
||||
if err = c.Decr(ctx, key); err != nil {
|
||||
t.Error("Decr Error", err)
|
||||
}
|
||||
v, _ := c.Get(ctx, key)
|
||||
assert.Equal(t, afterIncr, v)
|
||||
|
||||
if v, _ := c.Get(ctx, key); v != beforeIncr {
|
||||
t.Error("Get Error")
|
||||
}
|
||||
assert.Nil(t, c.Decr(ctx, key))
|
||||
|
||||
if err := c.Delete(ctx, key); err != nil {
|
||||
t.Error("Delete Error")
|
||||
}
|
||||
v, _ = c.Get(ctx, key)
|
||||
assert.Equal(t, v, beforeIncr)
|
||||
assert.Nil(t, c.Delete(ctx, key))
|
||||
}
|
||||
|
||||
func testIncrOverFlow(t *testing.T, c Cache, timeout time.Duration) {
|
||||
var err error
|
||||
ctx := context.Background()
|
||||
key := "incKey"
|
||||
|
||||
assert.Nil(t, c.Put(ctx, key, int64(math.MaxInt64), timeout))
|
||||
// int64
|
||||
if err = c.Put(ctx, key, int64(math.MaxInt64), timeout); err != nil {
|
||||
t.Error("Put Error: ", err.Error())
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
if err = c.Delete(ctx, key); err != nil {
|
||||
t.Errorf("Delete error: %s", err.Error())
|
||||
}
|
||||
assert.Nil(t, c.Delete(ctx, key))
|
||||
}()
|
||||
if err = c.Incr(ctx, key); err == nil {
|
||||
t.Error("Incr error")
|
||||
return
|
||||
}
|
||||
assert.NotNil(t, c.Incr(ctx, key))
|
||||
}
|
||||
|
||||
func testDecrOverFlow(t *testing.T, c Cache, timeout time.Duration) {
|
||||
|
||||
120
client/cache/conv_test.go
vendored
120
client/cache/conv_test.go
vendored
@@ -16,128 +16,74 @@ package cache
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGetString(t *testing.T) {
|
||||
var t1 = "test1"
|
||||
if "test1" != GetString(t1) {
|
||||
t.Error("get string from string error")
|
||||
}
|
||||
var t2 = []byte("test2")
|
||||
if "test2" != GetString(t2) {
|
||||
t.Error("get string from byte array error")
|
||||
}
|
||||
var t3 = 1
|
||||
if "1" != GetString(t3) {
|
||||
t.Error("get string from int error")
|
||||
}
|
||||
var t4 int64 = 1
|
||||
if "1" != GetString(t4) {
|
||||
t.Error("get string from int64 error")
|
||||
}
|
||||
var t5 = 1.1
|
||||
if "1.1" != GetString(t5) {
|
||||
t.Error("get string from float64 error")
|
||||
}
|
||||
|
||||
if "" != GetString(nil) {
|
||||
t.Error("get string from nil error")
|
||||
}
|
||||
assert.Equal(t, "test1", GetString(t1))
|
||||
var t2 = []byte("test2")
|
||||
assert.Equal(t, "test2", GetString(t2))
|
||||
var t3 = 1
|
||||
assert.Equal(t, "1", GetString(t3))
|
||||
var t4 int64 = 1
|
||||
assert.Equal(t, "1", GetString(t4))
|
||||
var t5 = 1.1
|
||||
assert.Equal(t, "1.1", GetString(t5))
|
||||
assert.Equal(t, "", GetString(nil))
|
||||
}
|
||||
|
||||
func TestGetInt(t *testing.T) {
|
||||
var t1 = 1
|
||||
if 1 != GetInt(t1) {
|
||||
t.Error("get int from int error")
|
||||
}
|
||||
assert.Equal(t, 1, GetInt(t1))
|
||||
var t2 int32 = 32
|
||||
if 32 != GetInt(t2) {
|
||||
t.Error("get int from int32 error")
|
||||
}
|
||||
assert.Equal(t, 32, GetInt(t2))
|
||||
|
||||
var t3 int64 = 64
|
||||
if 64 != GetInt(t3) {
|
||||
t.Error("get int from int64 error")
|
||||
}
|
||||
assert.Equal(t, 64, GetInt(t3))
|
||||
var t4 = "128"
|
||||
if 128 != GetInt(t4) {
|
||||
t.Error("get int from num string error")
|
||||
}
|
||||
if 0 != GetInt(nil) {
|
||||
t.Error("get int from nil error")
|
||||
}
|
||||
|
||||
assert.Equal(t, 128, GetInt(t4))
|
||||
assert.Equal(t, 0, GetInt(nil))
|
||||
}
|
||||
|
||||
func TestGetInt64(t *testing.T) {
|
||||
var i int64 = 1
|
||||
var t1 = 1
|
||||
if i != GetInt64(t1) {
|
||||
t.Error("get int64 from int error")
|
||||
}
|
||||
assert.Equal(t, i, GetInt64(t1))
|
||||
var t2 int32 = 1
|
||||
if i != GetInt64(t2) {
|
||||
t.Error("get int64 from int32 error")
|
||||
}
|
||||
|
||||
assert.Equal(t, i, GetInt64(t2))
|
||||
var t3 int64 = 1
|
||||
if i != GetInt64(t3) {
|
||||
t.Error("get int64 from int64 error")
|
||||
}
|
||||
assert.Equal(t, i, GetInt64(t3))
|
||||
var t4 = "1"
|
||||
if i != GetInt64(t4) {
|
||||
t.Error("get int64 from num string error")
|
||||
}
|
||||
if 0 != GetInt64(nil) {
|
||||
t.Error("get int64 from nil")
|
||||
}
|
||||
assert.Equal(t, i, GetInt64(t4))
|
||||
assert.Equal(t, int64(0), GetInt64(nil))
|
||||
}
|
||||
|
||||
func TestGetFloat64(t *testing.T) {
|
||||
var f = 1.11
|
||||
var t1 float32 = 1.11
|
||||
if f != GetFloat64(t1) {
|
||||
t.Error("get float64 from float32 error")
|
||||
}
|
||||
assert.Equal(t, f, GetFloat64(t1))
|
||||
var t2 = 1.11
|
||||
if f != GetFloat64(t2) {
|
||||
t.Error("get float64 from float64 error")
|
||||
}
|
||||
assert.Equal(t, f, GetFloat64(t2))
|
||||
var t3 = "1.11"
|
||||
if f != GetFloat64(t3) {
|
||||
t.Error("get float64 from string error")
|
||||
}
|
||||
assert.Equal(t, f, GetFloat64(t3))
|
||||
|
||||
var f2 float64 = 1
|
||||
var t4 = 1
|
||||
if f2 != GetFloat64(t4) {
|
||||
t.Error("get float64 from int error")
|
||||
}
|
||||
assert.Equal(t, f2, GetFloat64(t4))
|
||||
|
||||
if 0 != GetFloat64(nil) {
|
||||
t.Error("get float64 from nil error")
|
||||
}
|
||||
assert.Equal(t, float64(0), GetFloat64(nil))
|
||||
}
|
||||
|
||||
func TestGetBool(t *testing.T) {
|
||||
var t1 = true
|
||||
if !GetBool(t1) {
|
||||
t.Error("get bool from bool error")
|
||||
}
|
||||
assert.True(t, GetBool(t1))
|
||||
var t2 = "true"
|
||||
if !GetBool(t2) {
|
||||
t.Error("get bool from string error")
|
||||
}
|
||||
if GetBool(nil) {
|
||||
t.Error("get bool from nil error")
|
||||
}
|
||||
}
|
||||
assert.True(t, GetBool(t2))
|
||||
|
||||
func byteArrayEquals(a []byte, b []byte) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
for i, v := range a {
|
||||
if v != b[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
assert.False(t, GetBool(nil))
|
||||
}
|
||||
|
||||
11
client/cache/file_test.go
vendored
11
client/cache/file_test.go
vendored
@@ -42,6 +42,7 @@ func TestFileCacheStartAndGC(t *testing.T) {
|
||||
|
||||
str := getTestCacheFilePath()
|
||||
err = fc.StartAndGC(fmt.Sprintf(`{"CachePath":"%s","FileSuffix":".bin","DirectoryLevel":"2","EmbedExpiry":"0"}`, str))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, fc.CachePath, str)
|
||||
assert.Equal(t, fc.DirectoryLevel, 2)
|
||||
assert.Equal(t, fc.EmbedExpiry, 0)
|
||||
@@ -65,22 +66,24 @@ func TestFileCacheInit(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestFileGetContents(t *testing.T) {
|
||||
data, err := FileGetContents("/bin/aaa")
|
||||
_, err := FileGetContents("/bin/aaa")
|
||||
assert.NotNil(t, err)
|
||||
fn := filepath.Join(os.TempDir(), "fileCache.txt")
|
||||
f, err := os.Create(fn)
|
||||
assert.Nil(t, err)
|
||||
_, err = f.WriteString("text")
|
||||
assert.Nil(t, err)
|
||||
data, err = FileGetContents(fn)
|
||||
data, err := FileGetContents(fn)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "text", string(data))
|
||||
}
|
||||
|
||||
func TestGobEncodeDecode(t *testing.T) {
|
||||
data, err := GobEncode(func() {})
|
||||
_, err := GobEncode(func() {
|
||||
fmt.Print("test func")
|
||||
})
|
||||
assert.NotNil(t, err)
|
||||
data, err = GobEncode(&FileCacheItem{
|
||||
data, err := GobEncode(&FileCacheItem{
|
||||
Data: "hello",
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
|
||||
4
client/cache/memcache/memcache.go
vendored
4
client/cache/memcache/memcache.go
vendored
@@ -79,7 +79,7 @@ func (rc *Cache) GetMulti(ctx context.Context, keys []string) ([]interface{}, er
|
||||
keysErr := make([]string, 0)
|
||||
for i, ki := range keys {
|
||||
if _, ok := mv[ki]; !ok {
|
||||
keysErr = append(keysErr, fmt.Sprintf("key [%s] error: %s", ki, "the key isn't exist"))
|
||||
keysErr = append(keysErr, fmt.Sprintf("key [%s] error: %s", ki, "key not exist"))
|
||||
continue
|
||||
}
|
||||
rv[i] = mv[ki].Value
|
||||
@@ -100,7 +100,7 @@ func (rc *Cache) Put(ctx context.Context, key string, val interface{}, timeout t
|
||||
item.Value = []byte(str)
|
||||
} else {
|
||||
return berror.Errorf(cache.InvalidMemCacheValue,
|
||||
"the value must be string or byte[]. key: %s, value:%V", key, val)
|
||||
"the value must be string or byte[]. key: %s, value:%v", key, val)
|
||||
}
|
||||
return berror.Wrapf(rc.conn.Set(&item), cache.MemCacheCurdFailed,
|
||||
"could not put key-value to memcache, key: %s", key)
|
||||
|
||||
112
client/cache/memcache/memcache_test.go
vendored
112
client/cache/memcache/memcache_test.go
vendored
@@ -19,10 +19,12 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
_ "github.com/bradfitz/gomemcache/memcache"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/beego/beego/v2/client/cache"
|
||||
)
|
||||
@@ -34,78 +36,63 @@ func TestMemcacheCache(t *testing.T) {
|
||||
}
|
||||
|
||||
bm, err := cache.NewCache("memcache", fmt.Sprintf(`{"conn": "%s"}`, addr))
|
||||
if err != nil {
|
||||
t.Error("init err")
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
|
||||
timeoutDuration := 10 * time.Second
|
||||
if err = bm.Put(context.Background(), "astaxie", "1", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie"); !res {
|
||||
t.Error("check err")
|
||||
}
|
||||
|
||||
assert.Nil(t, bm.Put(context.Background(), "astaxie", "1", timeoutDuration))
|
||||
res, _ := bm.IsExist(context.Background(), "astaxie")
|
||||
assert.True(t, res)
|
||||
|
||||
time.Sleep(11 * time.Second)
|
||||
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie"); res {
|
||||
t.Error("check err")
|
||||
}
|
||||
if err = bm.Put(context.Background(), "astaxie", "1", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
res, _ = bm.IsExist(context.Background(), "astaxie")
|
||||
assert.False(t, res)
|
||||
|
||||
assert.Nil(t, bm.Put(context.Background(), "astaxie", "1", timeoutDuration))
|
||||
|
||||
val, _ := bm.Get(context.Background(), "astaxie")
|
||||
if v, err := strconv.Atoi(string(val.([]byte))); err != nil || v != 1 {
|
||||
t.Error("get err")
|
||||
}
|
||||
v, err := strconv.Atoi(string(val.([]byte)))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 1, v)
|
||||
|
||||
if err = bm.Incr(context.Background(), "astaxie"); err != nil {
|
||||
t.Error("Incr Error", err)
|
||||
}
|
||||
assert.Nil(t, bm.Incr(context.Background(), "astaxie"))
|
||||
|
||||
val, _ = bm.Get(context.Background(), "astaxie")
|
||||
if v, err := strconv.Atoi(string(val.([]byte))); err != nil || v != 2 {
|
||||
t.Error("get err")
|
||||
}
|
||||
v, err = strconv.Atoi(string(val.([]byte)))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 2, v)
|
||||
|
||||
if err = bm.Decr(context.Background(), "astaxie"); err != nil {
|
||||
t.Error("Decr Error", err)
|
||||
}
|
||||
assert.Nil(t, bm.Decr(context.Background(), "astaxie"))
|
||||
|
||||
val, _ = bm.Get(context.Background(), "astaxie")
|
||||
if v, err := strconv.Atoi(string(val.([]byte))); err != nil || v != 1 {
|
||||
t.Error("get err")
|
||||
}
|
||||
v, err = strconv.Atoi(string(val.([]byte)))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 1, v)
|
||||
bm.Delete(context.Background(), "astaxie")
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie"); res {
|
||||
t.Error("delete err")
|
||||
}
|
||||
|
||||
res, _ = bm.IsExist(context.Background(), "astaxie")
|
||||
assert.False(t, res)
|
||||
|
||||
assert.Nil(t,bm.Put(context.Background(), "astaxie", "author", timeoutDuration) )
|
||||
// test string
|
||||
if err = bm.Put(context.Background(), "astaxie", "author", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie"); !res {
|
||||
t.Error("check err")
|
||||
}
|
||||
res, _ = bm.IsExist(context.Background(), "astaxie")
|
||||
assert.True(t, res)
|
||||
|
||||
val, _ = bm.Get(context.Background(), "astaxie")
|
||||
if v := val.([]byte); string(v) != "author" {
|
||||
t.Error("get err")
|
||||
}
|
||||
vs := val.([]byte)
|
||||
assert.Equal(t, "author", string(vs))
|
||||
|
||||
// test GetMulti
|
||||
if err = bm.Put(context.Background(), "astaxie1", "author1", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie1"); !res {
|
||||
t.Error("check err")
|
||||
}
|
||||
assert.Nil(t, bm.Put(context.Background(), "astaxie1", "author1", timeoutDuration))
|
||||
|
||||
|
||||
res, _ = bm.IsExist(context.Background(), "astaxie1")
|
||||
assert.True(t, res)
|
||||
|
||||
vv, _ := bm.GetMulti(context.Background(), []string{"astaxie", "astaxie1"})
|
||||
if len(vv) != 2 {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
assert.Equal(t, 2, len(vv))
|
||||
|
||||
if string(vv[0].([]byte)) != "author" && string(vv[0].([]byte)) != "author1" {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
@@ -114,21 +101,14 @@ func TestMemcacheCache(t *testing.T) {
|
||||
}
|
||||
|
||||
vv, err = bm.GetMulti(context.Background(), []string{"astaxie0", "astaxie1"})
|
||||
if len(vv) != 2 {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if vv[0] != nil {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if string(vv[1].([]byte)) != "author1" {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if err != nil && err.Error() == "key [astaxie0] error: key isn't exist" {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
assert.Equal(t, 2, len(vv))
|
||||
assert.Nil(t, vv[0])
|
||||
|
||||
assert.Equal(t, "author1", string(vv[1].([]byte)))
|
||||
|
||||
assert.NotNil(t, err)
|
||||
assert.True(t, strings.Contains(err.Error(), "key not exist"))
|
||||
|
||||
assert.Nil(t, bm.ClearAll(context.Background()))
|
||||
// test clear all
|
||||
if err = bm.ClearAll(context.Background()); err != nil {
|
||||
t.Error("clear all err")
|
||||
}
|
||||
}
|
||||
|
||||
2
client/cache/redis/redis.go
vendored
2
client/cache/redis/redis.go
vendored
@@ -207,7 +207,7 @@ func (rc *Cache) StartAndGC(config string) error {
|
||||
cf["key"] = DefaultKey
|
||||
}
|
||||
if _, ok := cf["conn"]; !ok {
|
||||
return berror.Wrapf(err, cache.InvalidRedisCacheCfg, "config missing conn field. ", config)
|
||||
return berror.Wrapf(err, cache.InvalidRedisCacheCfg, "config missing conn field: %s", config)
|
||||
}
|
||||
|
||||
// Format redis://<password>@<host>:<port>
|
||||
|
||||
139
client/cache/redis/redis_test.go
vendored
139
client/cache/redis/redis_test.go
vendored
@@ -35,96 +35,74 @@ func TestRedisCache(t *testing.T) {
|
||||
}
|
||||
|
||||
bm, err := cache.NewCache("redis", fmt.Sprintf(`{"conn": "%s"}`, redisAddr))
|
||||
if err != nil {
|
||||
t.Error("init err")
|
||||
}
|
||||
timeoutDuration := 10 * time.Second
|
||||
if err = bm.Put(context.Background(), "astaxie", 1, timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie"); !res {
|
||||
t.Error("check err")
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
timeoutDuration := 3 * time.Second
|
||||
|
||||
time.Sleep(11 * time.Second)
|
||||
assert.Nil(t, bm.Put(context.Background(), "astaxie", 1, timeoutDuration))
|
||||
|
||||
|
||||
res, _ := bm.IsExist(context.Background(), "astaxie")
|
||||
assert.True(t, res)
|
||||
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
res, _ = bm.IsExist(context.Background(), "astaxie")
|
||||
assert.False(t, res)
|
||||
|
||||
assert.Nil(t, bm.Put(context.Background(), "astaxie", 1, timeoutDuration))
|
||||
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie"); res {
|
||||
t.Error("check err")
|
||||
}
|
||||
if err = bm.Put(context.Background(), "astaxie", 1, timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
|
||||
val, _ := bm.Get(context.Background(), "astaxie")
|
||||
if v, _ := redis.Int(val, err); v != 1 {
|
||||
t.Error("get err")
|
||||
}
|
||||
v, _ := redis.Int(val, err)
|
||||
assert.Equal(t, 1, v)
|
||||
|
||||
if err = bm.Incr(context.Background(), "astaxie"); err != nil {
|
||||
t.Error("Incr Error", err)
|
||||
}
|
||||
assert.Nil(t, bm.Incr(context.Background(), "astaxie"))
|
||||
val, _ = bm.Get(context.Background(), "astaxie")
|
||||
if v, _ := redis.Int(val, err); v != 2 {
|
||||
t.Error("get err")
|
||||
}
|
||||
v, _ = redis.Int(val, err)
|
||||
assert.Equal(t, 2, v)
|
||||
|
||||
if err = bm.Decr(context.Background(), "astaxie"); err != nil {
|
||||
t.Error("Decr Error", err)
|
||||
}
|
||||
assert.Nil(t, bm.Decr(context.Background(), "astaxie"))
|
||||
|
||||
val, _ = bm.Get(context.Background(), "astaxie")
|
||||
if v, _ := redis.Int(val, err); v != 1 {
|
||||
t.Error("get err")
|
||||
}
|
||||
v, _ = redis.Int(val, err)
|
||||
assert.Equal(t, 1, v)
|
||||
bm.Delete(context.Background(), "astaxie")
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie"); res {
|
||||
t.Error("delete err")
|
||||
}
|
||||
|
||||
res, _ = bm.IsExist(context.Background(), "astaxie")
|
||||
assert.False(t, res)
|
||||
|
||||
assert.Nil(t, bm.Put(context.Background(), "astaxie", "author", timeoutDuration))
|
||||
// test string
|
||||
if err = bm.Put(context.Background(), "astaxie", "author", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie"); !res {
|
||||
t.Error("check err")
|
||||
}
|
||||
|
||||
res, _ = bm.IsExist(context.Background(), "astaxie")
|
||||
assert.True(t, res)
|
||||
|
||||
val, _ = bm.Get(context.Background(), "astaxie")
|
||||
if v, _ := redis.String(val, err); v != "author" {
|
||||
t.Error("get err")
|
||||
}
|
||||
vs, _ := redis.String(val, err)
|
||||
assert.Equal(t, "author", vs)
|
||||
|
||||
// test GetMulti
|
||||
if err = bm.Put(context.Background(), "astaxie1", "author1", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie1"); !res {
|
||||
t.Error("check err")
|
||||
}
|
||||
assert.Nil(t, bm.Put(context.Background(), "astaxie1", "author1", timeoutDuration))
|
||||
|
||||
res, _ = bm.IsExist(context.Background(), "astaxie1")
|
||||
assert.True(t, res)
|
||||
|
||||
vv, _ := bm.GetMulti(context.Background(), []string{"astaxie", "astaxie1"})
|
||||
if len(vv) != 2 {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if v, _ := redis.String(vv[0], nil); v != "author" {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if v, _ := redis.String(vv[1], nil); v != "author1" {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
assert.Equal(t, 2, len(vv))
|
||||
vs, _ = redis.String(vv[0], nil)
|
||||
assert.Equal(t, "author", vs)
|
||||
|
||||
vs, _ = redis.String(vv[1], nil)
|
||||
assert.Equal(t, "author1", vs)
|
||||
|
||||
vv, _ = bm.GetMulti(context.Background(), []string{"astaxie0", "astaxie1"})
|
||||
if vv[0] != nil {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if v, _ := redis.String(vv[1], nil); v != "author1" {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
assert.Nil(t, vv[0])
|
||||
|
||||
vs, _ = redis.String(vv[1], nil)
|
||||
assert.Equal(t, "author1", vs)
|
||||
|
||||
// test clear all
|
||||
if err = bm.ClearAll(context.Background()); err != nil {
|
||||
t.Error("clear all err")
|
||||
}
|
||||
assert.Nil(t, bm.ClearAll(context.Background()))
|
||||
}
|
||||
|
||||
func TestCache_Scan(t *testing.T) {
|
||||
@@ -137,35 +115,24 @@ func TestCache_Scan(t *testing.T) {
|
||||
|
||||
// init
|
||||
bm, err := cache.NewCache("redis", fmt.Sprintf(`{"conn": "%s"}`, addr))
|
||||
if err != nil {
|
||||
t.Error("init err")
|
||||
}
|
||||
|
||||
assert.Nil(t, err)
|
||||
// insert all
|
||||
for i := 0; i < 100; i++ {
|
||||
if err = bm.Put(context.Background(), fmt.Sprintf("astaxie%d", i), fmt.Sprintf("author%d", i), timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
assert.Nil(t, bm.Put(context.Background(), fmt.Sprintf("astaxie%d", i), fmt.Sprintf("author%d", i), timeoutDuration))
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
// scan all for the first time
|
||||
keys, err := bm.(*Cache).Scan(DefaultKey + ":*")
|
||||
if err != nil {
|
||||
t.Error("scan Error", err)
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Equal(t, 100, len(keys), "scan all error")
|
||||
|
||||
// clear all
|
||||
if err = bm.ClearAll(context.Background()); err != nil {
|
||||
t.Error("clear all err")
|
||||
}
|
||||
assert.Nil(t, bm.ClearAll(context.Background()))
|
||||
|
||||
// scan all for the second time
|
||||
keys, err = bm.(*Cache).Scan(DefaultKey + ":*")
|
||||
if err != nil {
|
||||
t.Error("scan Error", err)
|
||||
}
|
||||
if len(keys) != 0 {
|
||||
t.Error("scan all err")
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 0, len(keys))
|
||||
}
|
||||
|
||||
2
client/cache/ssdb/ssdb.go
vendored
2
client/cache/ssdb/ssdb.go
vendored
@@ -53,7 +53,7 @@ func (rc *Cache) GetMulti(ctx context.Context, keys []string) ([]interface{}, er
|
||||
keysErr := make([]string, 0)
|
||||
for i, ki := range keys {
|
||||
if _, ok := keyIdx[ki]; !ok {
|
||||
keysErr = append(keysErr, fmt.Sprintf("key [%s] error: %s", ki, "the key isn't exist"))
|
||||
keysErr = append(keysErr, fmt.Sprintf("key [%s] error: %s", ki, "key not exist"))
|
||||
continue
|
||||
}
|
||||
values[i] = res[keyIdx[ki]+1]
|
||||
|
||||
137
client/cache/ssdb/ssdb_test.go
vendored
137
client/cache/ssdb/ssdb_test.go
vendored
@@ -5,9 +5,12 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/beego/beego/v2/client/cache"
|
||||
)
|
||||
|
||||
@@ -19,114 +22,80 @@ func TestSsdbcacheCache(t *testing.T) {
|
||||
}
|
||||
|
||||
ssdb, err := cache.NewCache("ssdb", fmt.Sprintf(`{"conn": "%s"}`, ssdbAddr))
|
||||
if err != nil {
|
||||
t.Error("init err")
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
|
||||
// test put and exist
|
||||
if res, _ := ssdb.IsExist(context.Background(), "ssdb"); res {
|
||||
t.Error("check err")
|
||||
}
|
||||
timeoutDuration := 10 * time.Second
|
||||
res, _ := ssdb.IsExist(context.Background(), "ssdb")
|
||||
assert.False(t, res)
|
||||
timeoutDuration := 3 * time.Second
|
||||
// timeoutDuration := -10*time.Second if timeoutDuration is negtive,it means permanent
|
||||
if err = ssdb.Put(context.Background(), "ssdb", "ssdb", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if res, _ := ssdb.IsExist(context.Background(), "ssdb"); !res {
|
||||
t.Error("check err")
|
||||
}
|
||||
|
||||
assert.Nil(t, ssdb.Put(context.Background(), "ssdb", "ssdb", timeoutDuration))
|
||||
|
||||
res, _ = ssdb.IsExist(context.Background(), "ssdb")
|
||||
assert.True(t, res)
|
||||
|
||||
// Get test done
|
||||
if err = ssdb.Put(context.Background(), "ssdb", "ssdb", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
assert.Nil(t, ssdb.Put(context.Background(), "ssdb", "ssdb", timeoutDuration))
|
||||
|
||||
if v, _ := ssdb.Get(context.Background(), "ssdb"); v != "ssdb" {
|
||||
t.Error("get Error")
|
||||
}
|
||||
v, _ := ssdb.Get(context.Background(), "ssdb")
|
||||
assert.Equal(t, "ssdb", v)
|
||||
|
||||
// inc/dec test done
|
||||
if err = ssdb.Put(context.Background(), "ssdb", "2", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if err = ssdb.Incr(context.Background(), "ssdb"); err != nil {
|
||||
t.Error("incr Error", err)
|
||||
}
|
||||
assert.Nil(t, ssdb.Put(context.Background(), "ssdb", "2", timeoutDuration))
|
||||
|
||||
assert.Nil(t, ssdb.Incr(context.Background(), "ssdb"))
|
||||
|
||||
val, _ := ssdb.Get(context.Background(), "ssdb")
|
||||
if v, err := strconv.Atoi(val.(string)); err != nil || v != 3 {
|
||||
t.Error("get err")
|
||||
}
|
||||
v, err = strconv.Atoi(val.(string))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 3, v)
|
||||
|
||||
if err = ssdb.Decr(context.Background(), "ssdb"); err != nil {
|
||||
t.Error("decr error")
|
||||
}
|
||||
assert.Nil(t, ssdb.Decr(context.Background(), "ssdb"))
|
||||
|
||||
// test del
|
||||
if err = ssdb.Put(context.Background(), "ssdb", "3", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
assert.Nil(t, ssdb.Put(context.Background(), "ssdb", "3", timeoutDuration))
|
||||
|
||||
val, _ = ssdb.Get(context.Background(), "ssdb")
|
||||
if v, err := strconv.Atoi(val.(string)); err != nil || v != 3 {
|
||||
t.Error("get err")
|
||||
}
|
||||
if err := ssdb.Delete(context.Background(), "ssdb"); err == nil {
|
||||
if e, _ := ssdb.IsExist(context.Background(), "ssdb"); e {
|
||||
t.Error("delete err")
|
||||
}
|
||||
}
|
||||
v, err = strconv.Atoi(val.(string))
|
||||
assert.Equal(t, 3, v)
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Nil(t, ssdb.Delete(context.Background(), "ssdb"))
|
||||
assert.Nil(t, ssdb.Put(context.Background(), "ssdb", "ssdb", -10*time.Second))
|
||||
// test string
|
||||
if err = ssdb.Put(context.Background(), "ssdb", "ssdb", -10*time.Second); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if res, _ := ssdb.IsExist(context.Background(), "ssdb"); !res {
|
||||
t.Error("check err")
|
||||
}
|
||||
if v, _ := ssdb.Get(context.Background(), "ssdb"); v.(string) != "ssdb" {
|
||||
t.Error("get err")
|
||||
}
|
||||
|
||||
res, _ = ssdb.IsExist(context.Background(), "ssdb")
|
||||
assert.True(t, res)
|
||||
|
||||
v, _ = ssdb.Get(context.Background(), "ssdb")
|
||||
assert.Equal(t, "ssdb", v.(string))
|
||||
|
||||
// test GetMulti done
|
||||
if err = ssdb.Put(context.Background(), "ssdb1", "ssdb1", -10*time.Second); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if res, _ := ssdb.IsExist(context.Background(), "ssdb1"); !res {
|
||||
t.Error("check err")
|
||||
}
|
||||
assert.Nil(t, ssdb.Put(context.Background(), "ssdb1", "ssdb1", -10*time.Second))
|
||||
|
||||
res, _ = ssdb.IsExist(context.Background(), "ssdb1")
|
||||
assert.True(t, res)
|
||||
vv, _ := ssdb.GetMulti(context.Background(), []string{"ssdb", "ssdb1"})
|
||||
if len(vv) != 2 {
|
||||
t.Error("getmulti error")
|
||||
}
|
||||
if vv[0].(string) != "ssdb" {
|
||||
t.Error("getmulti error")
|
||||
}
|
||||
if vv[1].(string) != "ssdb1" {
|
||||
t.Error("getmulti error")
|
||||
}
|
||||
assert.Equal(t, 2, len(vv))
|
||||
|
||||
assert.Equal(t, "ssdb", vv[0])
|
||||
assert.Equal(t, "ssdb1", vv[1])
|
||||
|
||||
vv, err = ssdb.GetMulti(context.Background(), []string{"ssdb", "ssdb11"})
|
||||
if len(vv) != 2 {
|
||||
t.Error("getmulti error")
|
||||
}
|
||||
if vv[0].(string) != "ssdb" {
|
||||
t.Error("getmulti error")
|
||||
}
|
||||
if vv[1] != nil {
|
||||
t.Error("getmulti error")
|
||||
}
|
||||
if err != nil && err.Error() != "key [ssdb11] error: the key isn't exist" {
|
||||
t.Error("getmulti error")
|
||||
}
|
||||
|
||||
assert.Equal(t, 2, len(vv))
|
||||
|
||||
assert.Equal(t, "ssdb", vv[0])
|
||||
assert.Nil(t, vv[1])
|
||||
|
||||
assert.NotNil(t, err)
|
||||
assert.True(t, strings.Contains(err.Error(), "key not exist"))
|
||||
|
||||
// test clear all done
|
||||
if err = ssdb.ClearAll(context.Background()); err != nil {
|
||||
t.Error("clear all err")
|
||||
}
|
||||
assert.Nil(t, ssdb.ClearAll(context.Background()))
|
||||
e1, _ := ssdb.IsExist(context.Background(), "ssdb")
|
||||
e2, _ := ssdb.IsExist(context.Background(), "ssdb1")
|
||||
if e1 || e2 {
|
||||
t.Error("check err")
|
||||
}
|
||||
assert.False(t, e1)
|
||||
assert.False(t, e2)
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ import (
|
||||
"github.com/beego/beego/v2/client/httplib"
|
||||
)
|
||||
|
||||
func TestFilterChainBuilder_FilterChain(t *testing.T) {
|
||||
func TestFilterChainBuilderFilterChain(t *testing.T) {
|
||||
next := func(ctx context.Context, req *httplib.BeegoHTTPRequest) (*http.Response, error) {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
return &http.Response{
|
||||
|
||||
@@ -25,7 +25,7 @@ import (
|
||||
"github.com/beego/beego/v2/client/httplib"
|
||||
)
|
||||
|
||||
func TestFilterChainBuilder_FilterChain(t *testing.T) {
|
||||
func TestFilterChainBuilderFilterChain(t *testing.T) {
|
||||
next := func(ctx context.Context, req *httplib.BeegoHTTPRequest) (*http.Response, error) {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
return &http.Response{
|
||||
|
||||
@@ -55,6 +55,7 @@ import (
|
||||
"github.com/beego/beego/v2/core/logs"
|
||||
)
|
||||
|
||||
const contentTypeKey = "Content-Type"
|
||||
// it will be the last filter and execute request.Do
|
||||
var doRequestFilter = func(ctx context.Context, req *BeegoHTTPRequest) (*http.Response, error) {
|
||||
return req.doRequest(ctx)
|
||||
@@ -311,7 +312,7 @@ func (b *BeegoHTTPRequest) XMLBody(obj interface{}) (*BeegoHTTPRequest, error) {
|
||||
return ioutil.NopCloser(bytes.NewReader(byts)), nil
|
||||
}
|
||||
b.req.ContentLength = int64(len(byts))
|
||||
b.req.Header.Set("Content-Type", "application/xml")
|
||||
b.req.Header.Set(contentTypeKey, "application/xml")
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
@@ -325,7 +326,7 @@ func (b *BeegoHTTPRequest) YAMLBody(obj interface{}) (*BeegoHTTPRequest, error)
|
||||
}
|
||||
b.req.Body = ioutil.NopCloser(bytes.NewReader(byts))
|
||||
b.req.ContentLength = int64(len(byts))
|
||||
b.req.Header.Set("Content-Type", "application/x+yaml")
|
||||
b.req.Header.Set(contentTypeKey, "application/x+yaml")
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
@@ -339,7 +340,7 @@ func (b *BeegoHTTPRequest) JSONBody(obj interface{}) (*BeegoHTTPRequest, error)
|
||||
}
|
||||
b.req.Body = ioutil.NopCloser(bytes.NewReader(byts))
|
||||
b.req.ContentLength = int64(len(byts))
|
||||
b.req.Header.Set("Content-Type", "application/json")
|
||||
b.req.Header.Set(contentTypeKey, "application/json")
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
@@ -359,34 +360,38 @@ func (b *BeegoHTTPRequest) buildURL(paramBody string) {
|
||||
if (b.req.Method == "POST" || b.req.Method == "PUT" || b.req.Method == "PATCH" || b.req.Method == "DELETE") && b.req.Body == nil {
|
||||
// with files
|
||||
if len(b.files) > 0 {
|
||||
pr, pw := io.Pipe()
|
||||
bodyWriter := multipart.NewWriter(pw)
|
||||
go func() {
|
||||
for formname, filename := range b.files {
|
||||
b.handleFileToBody(bodyWriter, formname, filename)
|
||||
}
|
||||
for k, v := range b.params {
|
||||
for _, vv := range v {
|
||||
_ = bodyWriter.WriteField(k, vv)
|
||||
}
|
||||
}
|
||||
_ = bodyWriter.Close()
|
||||
_ = pw.Close()
|
||||
}()
|
||||
b.Header("Content-Type", bodyWriter.FormDataContentType())
|
||||
b.req.Body = ioutil.NopCloser(pr)
|
||||
b.Header("Transfer-Encoding", "chunked")
|
||||
b.handleFiles()
|
||||
return
|
||||
}
|
||||
|
||||
// with params
|
||||
if len(paramBody) > 0 {
|
||||
b.Header("Content-Type", "application/x-www-form-urlencoded")
|
||||
b.Header(contentTypeKey, "application/x-www-form-urlencoded")
|
||||
b.Body(paramBody)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (b *BeegoHTTPRequest) handleFiles() {
|
||||
pr, pw := io.Pipe()
|
||||
bodyWriter := multipart.NewWriter(pw)
|
||||
go func() {
|
||||
for formname, filename := range b.files {
|
||||
b.handleFileToBody(bodyWriter, formname, filename)
|
||||
}
|
||||
for k, v := range b.params {
|
||||
for _, vv := range v {
|
||||
_ = bodyWriter.WriteField(k, vv)
|
||||
}
|
||||
}
|
||||
_ = bodyWriter.Close()
|
||||
_ = pw.Close()
|
||||
}()
|
||||
b.Header(contentTypeKey, bodyWriter.FormDataContentType())
|
||||
b.req.Body = ioutil.NopCloser(pr)
|
||||
b.Header("Transfer-Encoding", "chunked")
|
||||
}
|
||||
|
||||
func (b *BeegoHTTPRequest) handleFileToBody(bodyWriter *multipart.Writer, formname string, filename string) {
|
||||
fileWriter, err := bodyWriter.CreateFormFile(formname, filename)
|
||||
const errFmt = "Httplib: %+v"
|
||||
|
||||
Reference in New Issue
Block a user