github.com/bytedance/gopkg@v0.0.0-20240514070511-01b2cbcf35e1/cloud/metainfo/http_test.go (about) 1 // Copyright 2021 ByteDance Inc. 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 metainfo_test 16 17 import ( 18 "context" 19 "fmt" 20 "net/http" 21 "testing" 22 23 "github.com/bytedance/gopkg/cloud/metainfo" 24 ) 25 26 func TestHTTPHeaderToCGIVariable(t *testing.T) { 27 for k, v := range map[string]string{ 28 "a": "A", 29 "aBc": "ABC", 30 "a1z": "A1Z", 31 "ab-": "AB_", 32 "-cd": "_CD", 33 "abc-def": "ABC_DEF", 34 "Abc-def_ghi": "ABC_DEF_GHI", 35 } { 36 assert(t, metainfo.HTTPHeaderToCGIVariable(k) == v) 37 } 38 } 39 40 func TestCGIVariableToHTTPHeader(t *testing.T) { 41 for k, v := range map[string]string{ 42 "a": "a", 43 "aBc": "abc", 44 "a1z": "a1z", 45 "AB_": "ab-", 46 "_CD": "-cd", 47 "ABC_DEF": "abc-def", 48 "ABC-def_GHI": "abc-def-ghi", 49 } { 50 assert(t, metainfo.CGIVariableToHTTPHeader(k) == v) 51 } 52 } 53 54 func TestFromHTTPHeader(t *testing.T) { 55 assert(t, metainfo.FromHTTPHeader(nil, nil) == nil) 56 57 h := make(http.Header) 58 c := context.Background() 59 c1 := metainfo.FromHTTPHeader(c, metainfo.HTTPHeader(h)) 60 assert(t, c == c1) 61 62 h.Set("abc", "def") 63 h.Set(metainfo.HTTPPrefixTransient+"123", "456") 64 h.Set(metainfo.HTTPPrefixTransient+"abc-def", "ghi") 65 h.Set(metainfo.HTTPPrefixPersistent+"xyz", "000") 66 c1 = metainfo.FromHTTPHeader(c, metainfo.HTTPHeader(h)) 67 assert(t, c != c1) 68 vs := metainfo.GetAllValues(c1) 69 assert(t, len(vs) == 2, vs) 70 assert(t, vs["ABC_DEF"] == "ghi" && vs["123"] == "456", vs) 71 vs = metainfo.GetAllPersistentValues(c1) 72 assert(t, len(vs) == 1 && vs["XYZ"] == "000") 73 } 74 75 func TestFromHTTPHeaderKeepPreviousData(t *testing.T) { 76 c0 := context.Background() 77 c0 = metainfo.WithValue(c0, "uk", "uv") 78 c0 = metainfo.TransferForward(c0) 79 c0 = metainfo.WithValue(c0, "tk", "tv") 80 c0 = metainfo.WithPersistentValue(c0, "pk", "pv") 81 82 h := make(http.Header) 83 h.Set(metainfo.HTTPPrefixTransient+"xk", "xv") 84 h.Set(metainfo.HTTPPrefixPersistent+"yk", "yv") 85 h.Set(metainfo.HTTPPrefixPersistent+"pk", "pp") 86 87 c1 := metainfo.FromHTTPHeader(c0, metainfo.HTTPHeader(h)) 88 assert(t, c0 != c1) 89 vs := metainfo.GetAllValues(c1) 90 assert(t, len(vs) == 3, len(vs)) 91 assert(t, vs["tk"] == "tv" && vs["uk"] == "uv" && vs["XK"] == "xv") 92 vs = metainfo.GetAllPersistentValues(c1) 93 assert(t, len(vs) == 3) 94 assert(t, vs["pk"] == "pv" && vs["YK"] == "yv" && vs["PK"] == "pp") 95 } 96 97 func TestToHTTPHeader(t *testing.T) { 98 metainfo.ToHTTPHeader(nil, nil) 99 100 h := make(http.Header) 101 c := context.Background() 102 metainfo.ToHTTPHeader(c, h) 103 assert(t, len(h) == 0) 104 105 c = metainfo.WithValue(c, "123", "456") 106 c = metainfo.WithPersistentValue(c, "abc", "def") 107 metainfo.ToHTTPHeader(c, h) 108 assert(t, len(h) == 2) 109 assert(t, h.Get(metainfo.HTTPPrefixTransient+"123") == "456") 110 assert(t, h.Get(metainfo.HTTPPrefixPersistent+"abc") == "def") 111 } 112 113 func TestHTTPHeader(t *testing.T) { 114 h := make(metainfo.HTTPHeader) 115 h.Set("Hello", "halo") 116 h.Set("hello", "world") 117 118 kvs := make(map[string]string) 119 h.Visit(func(k, v string) { 120 kvs[k] = v 121 }) 122 assert(t, len(kvs) == 1) 123 assert(t, kvs["hello"] == "world") 124 } 125 126 func BenchmarkHTTPHeaderToCGIVariable(b *testing.B) { 127 b.ReportAllocs() 128 b.ResetTimer() 129 for i := 0; i < b.N; i++ { 130 _ = metainfo.HTTPHeaderToCGIVariable(metainfo.HTTPPrefixPersistent + "hello-world") 131 } 132 } 133 134 func BenchmarkCGIVariableToHTTPHeader(b *testing.B) { 135 b.ReportAllocs() 136 b.ResetTimer() 137 for i := 0; i < b.N; i++ { 138 _ = metainfo.CGIVariableToHTTPHeader(metainfo.PrefixPersistent + "HELLO_WORLD") 139 } 140 } 141 142 func BenchmarkFromHTTPHeader(b *testing.B) { 143 for _, cnt := range []int{10, 20, 50, 100} { 144 hd := make(metainfo.HTTPHeader) 145 hd.Set("content-type", "test") 146 hd.Set("content-length", "12345") 147 for i := 0; len(hd) < cnt; i++ { 148 hd.Set(metainfo.HTTPPrefixTransient+fmt.Sprintf("tk%d", i), fmt.Sprintf("tv-%d", i)) 149 hd.Set(metainfo.HTTPPrefixPersistent+fmt.Sprintf("pk%d", i), fmt.Sprintf("pv-%d", i)) 150 } 151 ctx := context.Background() 152 fun := fmt.Sprintf("FromHTTPHeader-%d", cnt) 153 b.Run(fun, func(b *testing.B) { 154 b.ReportAllocs() 155 b.ResetTimer() 156 for i := 0; i < b.N; i++ { 157 _ = metainfo.FromHTTPHeader(ctx, hd) 158 } 159 }) 160 } 161 } 162 163 func BenchmarkToHTTPHeader(b *testing.B) { 164 for _, cnt := range []int{10, 20, 50, 100} { 165 ctx, _, _ := initMetaInfo(cnt) 166 fun := fmt.Sprintf("ToHTTPHeader-%d", cnt) 167 b.Run(fun, func(b *testing.B) { 168 hd := make(metainfo.HTTPHeader) 169 b.ReportAllocs() 170 b.ResetTimer() 171 for i := 0; i < b.N; i++ { 172 metainfo.ToHTTPHeader(ctx, hd) 173 } 174 }) 175 } 176 }