metadata_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package metadata
  2. import (
  3. "context"
  4. "reflect"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestPairsMD(t *testing.T) {
  9. for _, test := range []struct {
  10. // input
  11. kv []interface{}
  12. // output
  13. md MD
  14. }{
  15. {[]interface{}{}, MD{}},
  16. {[]interface{}{"k1", "v1", "k1", "v2"}, MD{"k1": "v2"}},
  17. } {
  18. md := Pairs(test.kv...)
  19. if !reflect.DeepEqual(md, test.md) {
  20. t.Fatalf("Pairs(%v) = %v, want %v", test.kv, md, test.md)
  21. }
  22. }
  23. }
  24. func TestCopy(t *testing.T) {
  25. const key, val = "key", "val"
  26. orig := Pairs(key, val)
  27. copy := orig.Copy()
  28. if !reflect.DeepEqual(orig, copy) {
  29. t.Errorf("copied value not equal to the original, got %v, want %v", copy, orig)
  30. }
  31. orig[key] = "foo"
  32. if v := copy[key]; v != val {
  33. t.Errorf("change in original should not affect copy, got %q, want %q", v, val)
  34. }
  35. }
  36. func TestJoin(t *testing.T) {
  37. for _, test := range []struct {
  38. mds []MD
  39. want MD
  40. }{
  41. {[]MD{}, MD{}},
  42. {[]MD{Pairs("foo", "bar")}, Pairs("foo", "bar")},
  43. {[]MD{Pairs("foo", "bar"), Pairs("foo", "baz")}, Pairs("foo", "bar", "foo", "baz")},
  44. {[]MD{Pairs("foo", "bar"), Pairs("foo", "baz"), Pairs("zip", "zap")}, Pairs("foo", "bar", "foo", "baz", "zip", "zap")},
  45. } {
  46. md := Join(test.mds...)
  47. if !reflect.DeepEqual(md, test.want) {
  48. t.Errorf("context's metadata is %v, want %v", md, test.want)
  49. }
  50. }
  51. }
  52. func TestWithContext(t *testing.T) {
  53. md := MD(map[string]interface{}{RemoteIP: "127.0.0.1", Color: "red", Mirror: true})
  54. c := NewContext(context.Background(), md)
  55. ctx := WithContext(c)
  56. md1, ok := FromContext(ctx)
  57. if !ok {
  58. t.Errorf("expect ok be true")
  59. t.FailNow()
  60. }
  61. if !reflect.DeepEqual(md1, md) {
  62. t.Errorf("expect md1 equal to md")
  63. t.FailNow()
  64. }
  65. }
  66. func TestBool(t *testing.T) {
  67. md := MD{RemoteIP: "127.0.0.1", Color: "red"}
  68. mdcontext := NewContext(context.Background(), md)
  69. assert.Equal(t, false, Bool(mdcontext, Mirror))
  70. mdcontext = NewContext(context.Background(), MD{Mirror: true})
  71. assert.Equal(t, true, Bool(mdcontext, Mirror))
  72. mdcontext = NewContext(context.Background(), MD{Mirror: "true"})
  73. assert.Equal(t, true, Bool(mdcontext, Mirror))
  74. mdcontext = NewContext(context.Background(), MD{Mirror: "1"})
  75. assert.Equal(t, true, Bool(mdcontext, Mirror))
  76. mdcontext = NewContext(context.Background(), MD{Mirror: "0"})
  77. assert.Equal(t, false, Bool(mdcontext, Mirror))
  78. }
  79. func TestInt64(t *testing.T) {
  80. mdcontext := NewContext(context.Background(), MD{Uid: int64(1)})
  81. assert.Equal(t, int64(1), Int64(mdcontext, Uid))
  82. mdcontext = NewContext(context.Background(), MD{Uid: int64(2)})
  83. assert.NotEqual(t, int64(1), Int64(mdcontext, Uid))
  84. mdcontext = NewContext(context.Background(), MD{Uid: 10})
  85. assert.NotEqual(t, int64(10), Int64(mdcontext, Uid))
  86. }