github.com/clubpay/ronykit/kit@v0.14.4-0.20240515065620-d0dace45cbc7/ctx_kv_test.go (about)

     1  package kit_test
     2  
     3  import (
     4  	"github.com/clubpay/ronykit/kit"
     5  	. "github.com/onsi/ginkgo/v2"
     6  	. "github.com/onsi/gomega"
     7  )
     8  
     9  var _ = DescribeTable(
    10  	"GetString",
    11  	func(key string, val, def string) {
    12  		ctx := kit.NewContext(nil)
    13  		Expect(ctx.GetString(key, def)).To(Equal(def))
    14  		Expect(ctx.Set(key, val).GetString(key, def)).To(Equal(val))
    15  	},
    16  	Entry("", "key1", "val1", "defVal1"),
    17  	Entry("", "key2", "val2", "defVal2"),
    18  	Entry("", "key1", "val1", ""),
    19  	Entry("", "key1", "", "defVal1"),
    20  )
    21  
    22  var _ = DescribeTable(
    23  	"GetInt32",
    24  	func(key string, val, def int32) {
    25  		ctx := kit.NewContext(nil)
    26  		Expect(ctx.GetInt32(key, def)).To(Equal(def))
    27  		Expect(ctx.Set(key, val).GetInt32(key, def)).To(Equal(val))
    28  	},
    29  	Entry("", "key1", int32(100), int32(200)),
    30  	Entry("", "key2", int32(50), int32(0)),
    31  	Entry("", "key1", int32(0), int32(0)),
    32  	Entry("", "key1", int32(-1), int32(10000)),
    33  )
    34  
    35  var _ = DescribeTable(
    36  	"GetInt64",
    37  	func(key string, val, def int64) {
    38  		ctx := kit.NewContext(nil)
    39  		Expect(ctx.GetInt64(key, def)).To(Equal(def))
    40  		Expect(ctx.Set(key, val).GetInt64(key, def)).To(Equal(val))
    41  	},
    42  	Entry("", "key1", int64(100), int64(200)),
    43  	Entry("", "key2", int64(50), int64(0)),
    44  	Entry("", "key1", int64(0), int64(0)),
    45  	Entry("", "key1", int64(-1), int64(10000)),
    46  )
    47  
    48  var _ = DescribeTable(
    49  	"GetUInt64",
    50  	func(key string, val, def uint64) {
    51  		ctx := kit.NewContext(nil)
    52  		Expect(ctx.GetUint64(key, def)).To(Equal(def))
    53  		Expect(ctx.Set(key, val).GetUint64(key, def)).To(Equal(val))
    54  	},
    55  	Entry("", "key1", uint64(100), uint64(200)),
    56  	Entry("", "key2", uint64(50), uint64(0)),
    57  	Entry("", "key1", uint64(0), uint64(0)),
    58  )
    59  
    60  var _ = DescribeTable(
    61  	"GetUInt32",
    62  	func(key string, val, def uint32) {
    63  		ctx := kit.NewContext(nil)
    64  		Expect(ctx.GetUint32(key, def)).To(Equal(def))
    65  		Expect(ctx.Set(key, val).GetUint32(key, def)).To(Equal(val))
    66  	},
    67  	Entry("", "key1", uint32(100), uint32(200)),
    68  	Entry("", "key2", uint32(50), uint32(0)),
    69  	Entry("", "key1", uint32(0), uint32(0)),
    70  )
    71  
    72  var _ = Describe(
    73  	"Walk",
    74  	func() {
    75  		ctx := kit.NewContext(nil)
    76  		ctx.Set("k1", "v1")
    77  		ctx.Set("k2", "v2")
    78  
    79  		hdr := map[string]any{}
    80  		ctx.Walk(func(key string, val any) bool {
    81  			hdr[key] = val
    82  
    83  			return true
    84  		})
    85  
    86  		It("Should load all the header keys", func() {
    87  			Expect(hdr).To(HaveKeyWithValue("k1", "v1"))
    88  			Expect(hdr).To(HaveKeyWithValue("k2", "v2"))
    89  		})
    90  	},
    91  )