go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/grpc/prpc/metadata_test.go (about) 1 // Copyright 2016 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package prpc 16 17 import ( 18 "encoding/base64" 19 "net/http" 20 "testing" 21 22 "google.golang.org/grpc/metadata" 23 24 . "github.com/smartystreets/goconvey/convey" 25 . "go.chromium.org/luci/common/testing/assertions" 26 ) 27 28 func TestBinaryHeader(t *testing.T) { 29 t.Parallel() 30 31 Convey("from headers", t, func() { 32 Convey("regular", func() { 33 md, err := headersIntoMetadata(http.Header{ 34 "Key": {"v1", "v2"}, 35 "Another-Key": {"v3"}, 36 }) 37 So(err, ShouldBeNil) 38 So(md, ShouldResemble, metadata.MD{ 39 "key": {"v1", "v2"}, 40 "another-key": {"v3"}, 41 }) 42 }) 43 44 Convey("binary", func() { 45 data := []byte{10} 46 md, err := headersIntoMetadata(http.Header{ 47 "Key-Bin": {base64.StdEncoding.EncodeToString(data)}, 48 }) 49 So(err, ShouldBeNil) 50 So(md, ShouldResemble, metadata.MD{ 51 "key-bin": {string(data)}, 52 }) 53 }) 54 55 Convey("binary invalid", func() { 56 _, err := headersIntoMetadata(http.Header{ 57 "Key-Bin": {"Z"}, 58 }) 59 So(err, ShouldErrLike, "illegal base64 data at input byte 0") 60 }) 61 62 Convey("reserved", func() { 63 md, err := headersIntoMetadata(http.Header{ 64 "Content-Type": {"zzz"}, 65 "X-Prpc-Zzz": {"zzz"}, 66 }) 67 So(err, ShouldBeNil) 68 So(md, ShouldResemble, metadata.MD(nil)) 69 }) 70 }) 71 72 Convey("to headers", t, func() { 73 Convey("regular", func() { 74 h := http.Header{} 75 err := metaIntoHeaders(metadata.MD{ 76 "key": {"v1", "v2"}, 77 "another-key": {"v3"}, 78 }, h) 79 So(err, ShouldBeNil) 80 So(h, ShouldResemble, http.Header{ 81 "Key": {"v1", "v2"}, 82 "Another-Key": {"v3"}, 83 }) 84 }) 85 86 Convey("binary", func() { 87 data := []byte{10} 88 h := http.Header{} 89 err := metaIntoHeaders(metadata.MD{ 90 "key-bin": {string(data)}, 91 }, h) 92 So(err, ShouldBeNil) 93 So(h, ShouldResemble, http.Header{ 94 "Key-Bin": {base64.StdEncoding.EncodeToString(data)}, 95 }) 96 }) 97 98 Convey("reserved", func() { 99 h := http.Header{} 100 err := metaIntoHeaders(metadata.MD{ 101 "content-type": {"zzz"}, 102 }, h) 103 So(err, ShouldErrLike, "using reserved metadata key") 104 }) 105 }) 106 }