metadata_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. *
  3. * Copyright 2020 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package metadata
  19. import (
  20. "errors"
  21. "reflect"
  22. "testing"
  23. "github.com/google/go-cmp/cmp"
  24. "google.golang.org/grpc/attributes"
  25. "google.golang.org/grpc/metadata"
  26. "google.golang.org/grpc/resolver"
  27. )
  28. func TestGet(t *testing.T) {
  29. tests := []struct {
  30. name string
  31. addr resolver.Address
  32. want metadata.MD
  33. }{
  34. {
  35. name: "not set",
  36. addr: resolver.Address{},
  37. want: nil,
  38. },
  39. {
  40. name: "not set",
  41. addr: resolver.Address{
  42. Attributes: attributes.New(mdKey, mdValue(metadata.Pairs("k", "v"))),
  43. },
  44. want: metadata.Pairs("k", "v"),
  45. },
  46. }
  47. for _, tt := range tests {
  48. t.Run(tt.name, func(t *testing.T) {
  49. if got := Get(tt.addr); !cmp.Equal(got, tt.want) {
  50. t.Errorf("Get() = %v, want %v", got, tt.want)
  51. }
  52. })
  53. }
  54. }
  55. func TestSet(t *testing.T) {
  56. tests := []struct {
  57. name string
  58. addr resolver.Address
  59. md metadata.MD
  60. }{
  61. {
  62. name: "unset before",
  63. addr: resolver.Address{},
  64. md: metadata.Pairs("k", "v"),
  65. },
  66. {
  67. name: "set before",
  68. addr: resolver.Address{
  69. Attributes: attributes.New(mdKey, mdValue(metadata.Pairs("bef", "ore"))),
  70. },
  71. md: metadata.Pairs("k", "v"),
  72. },
  73. }
  74. for _, tt := range tests {
  75. t.Run(tt.name, func(t *testing.T) {
  76. newAddr := Set(tt.addr, tt.md)
  77. newMD := Get(newAddr)
  78. if !cmp.Equal(newMD, tt.md) {
  79. t.Errorf("md after Set() = %v, want %v", newMD, tt.md)
  80. }
  81. })
  82. }
  83. }
  84. func TestValidate(t *testing.T) {
  85. for _, test := range []struct {
  86. md metadata.MD
  87. want error
  88. }{
  89. {
  90. md: map[string][]string{string(rune(0x19)): {"testVal"}},
  91. want: errors.New("header key \"\\x19\" contains illegal characters not in [0-9a-z-_.]"),
  92. },
  93. {
  94. md: map[string][]string{"test": {string(rune(0x19))}},
  95. want: errors.New("header key \"test\" contains value with non-printable ASCII characters"),
  96. },
  97. {
  98. md: map[string][]string{"": {"valid"}},
  99. want: errors.New("there is an empty key in the header"),
  100. },
  101. {
  102. md: map[string][]string{"test-bin": {string(rune(0x19))}},
  103. want: nil,
  104. },
  105. } {
  106. err := Validate(test.md)
  107. if !reflect.DeepEqual(err, test.want) {
  108. t.Errorf("validating metadata which is %v got err :%v, want err :%v", test.md, err, test.want)
  109. }
  110. }
  111. }