github.com/cloudwego/hertz@v0.9.3/pkg/protocol/trailer_test.go (about) 1 /* 2 * Copyright 2023 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package protocol 18 19 import ( 20 "strings" 21 "testing" 22 23 "github.com/cloudwego/hertz/internal/bytestr" 24 25 "github.com/cloudwego/hertz/pkg/common/test/assert" 26 "github.com/cloudwego/hertz/pkg/protocol/consts" 27 ) 28 29 func TestTrailerAdd(t *testing.T) { 30 var tr Trailer 31 assert.Nil(t, tr.Add("foo", "value1")) 32 assert.Nil(t, tr.Add("foo", "value2")) 33 assert.Nil(t, tr.Add("bar", "value3")) 34 assert.True(t, strings.Contains(string(tr.Header()), "Foo: value1")) 35 assert.True(t, strings.Contains(string(tr.Header()), "Foo: value2")) 36 assert.True(t, strings.Contains(string(tr.Header()), "Bar: value3")) 37 } 38 39 func TestHeaderTrailerSet(t *testing.T) { 40 h := &RequestHeader{} 41 42 // only one trailer 43 h.Set("Trailer", "Foo") 44 assert.True(t, strings.Contains(string(h.Trailer().Header()), "Foo:")) 45 46 // multi trailer 47 h.Set("Trailer", "Foo, bar, HERtz") 48 assert.True(t, strings.Contains(string(h.Trailer().Header()), "Foo:")) 49 assert.True(t, strings.Contains(string(h.Trailer().Header()), "Bar:")) 50 assert.True(t, strings.Contains(string(h.Trailer().Header()), "Hertz:")) 51 52 // all lowercase 53 h.Set("Trailer", "foo,hertz,aaa") 54 assert.True(t, strings.Contains(string(h.Trailer().Header()), "Foo:")) 55 assert.True(t, strings.Contains(string(h.Trailer().Header()), "Hertz:")) 56 assert.True(t, strings.Contains(string(h.Trailer().Header()), "Aaa:")) 57 58 // all uppercase 59 h.Set("Trailer", "FOO,HERTZ,AAA") 60 assert.True(t, strings.Contains(string(h.Trailer().Header()), "Foo:")) 61 assert.True(t, strings.Contains(string(h.Trailer().Header()), "Hertz:")) 62 assert.True(t, strings.Contains(string(h.Trailer().Header()), "Aaa:")) 63 64 // with '-' 65 h.Set("Trailer", "FOO-HERTZ-AAA") 66 assert.True(t, strings.Contains(string(h.Trailer().Header()), "Foo-Hertz-Aaa:")) 67 68 // more space 69 h.Set("Trailer", " foo, hertz , aaa ") 70 assert.True(t, strings.Contains(string(h.Trailer().Header()), "Foo:")) 71 assert.True(t, strings.Contains(string(h.Trailer().Header()), "Hertz:")) 72 assert.True(t, strings.Contains(string(h.Trailer().Header()), "Aaa:")) 73 } 74 75 func TestTrailerAddError(t *testing.T) { 76 var tr Trailer 77 assert.NotNil(t, tr.Add(consts.HeaderContentType, "")) 78 assert.NotNil(t, tr.Set(consts.HeaderProxyConnection, "")) 79 } 80 81 func TestTrailerDel(t *testing.T) { 82 var tr Trailer 83 assert.Nil(t, tr.Add("foo", "value1")) 84 assert.Nil(t, tr.Add("foo", "value2")) 85 assert.Nil(t, tr.Add("bar", "value3")) 86 tr.Del("foo") 87 assert.False(t, strings.Contains(string(tr.Header()), "Foo: value1")) 88 assert.False(t, strings.Contains(string(tr.Header()), "Foo: value2")) 89 assert.True(t, strings.Contains(string(tr.Header()), "Bar: value3")) 90 } 91 92 func TestTrailerSet(t *testing.T) { 93 var tr Trailer 94 assert.Nil(t, tr.Set("foo", "value1")) 95 assert.Nil(t, tr.Set("foo", "value2")) 96 assert.Nil(t, tr.Set("bar", "value3")) 97 assert.False(t, strings.Contains(string(tr.Header()), "Foo: value1")) 98 assert.True(t, strings.Contains(string(tr.Header()), "Foo: value2")) 99 assert.True(t, strings.Contains(string(tr.Header()), "Bar: value3")) 100 } 101 102 func TestTrailerGet(t *testing.T) { 103 var tr Trailer 104 assert.Nil(t, tr.Add("foo", "value1")) 105 assert.Nil(t, tr.Add("bar", "value3")) 106 assert.DeepEqual(t, tr.Get("foo"), "value1") 107 assert.DeepEqual(t, tr.Get("bar"), "value3") 108 } 109 110 func TestTrailerUpdateArgBytes(t *testing.T) { 111 var tr Trailer 112 assert.Nil(t, tr.addArgBytes([]byte("Foo"), []byte("value0"), argsNoValue)) 113 assert.Nil(t, tr.UpdateArgBytes([]byte("Foo"), []byte("value1"))) 114 assert.Nil(t, tr.UpdateArgBytes([]byte("Foo"), []byte("value2"))) 115 assert.Nil(t, tr.UpdateArgBytes([]byte("Bar"), []byte("value3"))) 116 assert.True(t, strings.Contains(string(tr.Header()), "Foo: value1")) 117 assert.False(t, strings.Contains(string(tr.Header()), "Foo: value2")) 118 assert.False(t, strings.Contains(string(tr.Header()), "Bar: value3")) 119 } 120 121 func TestTrailerEmpty(t *testing.T) { 122 var tr Trailer 123 assert.DeepEqual(t, tr.Empty(), true) 124 assert.Nil(t, tr.Set("foo", "")) 125 assert.DeepEqual(t, tr.Empty(), false) 126 } 127 128 func TestTrailerVisitAll(t *testing.T) { 129 var tr Trailer 130 assert.Nil(t, tr.Add("foo", "value1")) 131 assert.Nil(t, tr.Add("bar", "value2")) 132 tr.VisitAll( 133 func(k, v []byte) { 134 key := string(k) 135 value := string(v) 136 if (key != "Foo" || value != "value1") && (key != "Bar" || value != "value2") { 137 t.Fatalf("Unexpected (%v, %v). Expected %v", key, value, "(foo, value1) or (bar, value2)") 138 } 139 }) 140 } 141 142 func TestIsBadTrailer(t *testing.T) { 143 assert.True(t, IsBadTrailer(bytestr.StrAuthorization)) 144 assert.True(t, IsBadTrailer(bytestr.StrContentEncoding)) 145 assert.True(t, IsBadTrailer(bytestr.StrContentLength)) 146 assert.True(t, IsBadTrailer(bytestr.StrContentType)) 147 assert.True(t, IsBadTrailer(bytestr.StrContentRange)) 148 assert.True(t, IsBadTrailer(bytestr.StrConnection)) 149 assert.True(t, IsBadTrailer(bytestr.StrExpect)) 150 assert.True(t, IsBadTrailer(bytestr.StrHost)) 151 assert.True(t, IsBadTrailer(bytestr.StrKeepAlive)) 152 assert.True(t, IsBadTrailer(bytestr.StrMaxForwards)) 153 assert.True(t, IsBadTrailer(bytestr.StrProxyConnection)) 154 assert.True(t, IsBadTrailer(bytestr.StrProxyAuthenticate)) 155 assert.True(t, IsBadTrailer(bytestr.StrProxyAuthorization)) 156 assert.True(t, IsBadTrailer(bytestr.StrRange)) 157 assert.True(t, IsBadTrailer(bytestr.StrTE)) 158 assert.True(t, IsBadTrailer(bytestr.StrTrailer)) 159 assert.True(t, IsBadTrailer(bytestr.StrTransferEncoding)) 160 assert.True(t, IsBadTrailer(bytestr.StrWWWAuthenticate)) 161 }