header_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package eventstream
  2. import (
  3. "reflect"
  4. "testing"
  5. "time"
  6. )
  7. func TestHeaders_Set(t *testing.T) {
  8. expect := Headers{
  9. {Name: "ABC", Value: StringValue("123")},
  10. {Name: "EFG", Value: TimestampValue(time.Time{})},
  11. }
  12. var actual Headers
  13. actual.Set("ABC", Int32Value(123))
  14. actual.Set("ABC", StringValue("123")) // replace case
  15. actual.Set("EFG", TimestampValue(time.Time{}))
  16. if e, a := expect, actual; !reflect.DeepEqual(e, a) {
  17. t.Errorf("expect %v headers, got %v", e, a)
  18. }
  19. }
  20. func TestHeaders_Get(t *testing.T) {
  21. headers := Headers{
  22. {Name: "ABC", Value: StringValue("123")},
  23. {Name: "EFG", Value: TimestampValue(time.Time{})},
  24. }
  25. cases := []struct {
  26. Name string
  27. Value Value
  28. }{
  29. {Name: "ABC", Value: StringValue("123")},
  30. {Name: "EFG", Value: TimestampValue(time.Time{})},
  31. {Name: "NotFound"},
  32. }
  33. for i, c := range cases {
  34. actual := headers.Get(c.Name)
  35. if e, a := c.Value, actual; !reflect.DeepEqual(e, a) {
  36. t.Errorf("%d, expect %v value, got %v", i, e, a)
  37. }
  38. }
  39. }
  40. func TestHeaders_Del(t *testing.T) {
  41. headers := Headers{
  42. {Name: "ABC", Value: StringValue("123")},
  43. {Name: "EFG", Value: TimestampValue(time.Time{})},
  44. {Name: "HIJ", Value: StringValue("123")},
  45. {Name: "KML", Value: TimestampValue(time.Time{})},
  46. }
  47. expectAfterDel := Headers{
  48. {Name: "EFG", Value: TimestampValue(time.Time{})},
  49. }
  50. headers.Del("HIJ")
  51. headers.Del("ABC")
  52. headers.Del("KML")
  53. if e, a := expectAfterDel, headers; !reflect.DeepEqual(e, a) {
  54. t.Errorf("expect %v headers, got %v", e, a)
  55. }
  56. }