@@ -111,3 +111,85 @@ func TestInjectMetadataIntoHTTPRequestHeaders(t *testing.T) {
111111 require .Equal (t , "ContentsOfTestHeader2" , header2 [0 ])
112112
113113}
114+
115+ func TestContextWithRequestMetadataMapFromHeaderSlice (t * testing.T ) {
116+ tests := []struct {
117+ name string
118+ headerSlice []string
119+ expectedResult map [string ]string
120+ }{
121+ {
122+ name : "empty header slice" ,
123+ headerSlice : []string {},
124+ expectedResult : map [string ]string {},
125+ },
126+ {
127+ name : "nil header slice" ,
128+ headerSlice : nil ,
129+ expectedResult : map [string ]string {},
130+ },
131+ {
132+ name : "odd number of elements" ,
133+ headerSlice : []string {"header1" , "value1" , "header2" },
134+ expectedResult : nil ,
135+ },
136+ {
137+ name : "single key-value pair" ,
138+ headerSlice : []string {"header1" , "value1" },
139+ expectedResult : map [string ]string {
140+ "header1" : "value1" ,
141+ },
142+ },
143+ {
144+ name : "multiple key-value pairs" ,
145+ headerSlice : []string {"header1" , "value1" , "header2" , "value2" , "header3" , "value3" },
146+ expectedResult : map [string ]string {
147+ "header1" : "value1" ,
148+ "header2" : "value2" ,
149+ "header3" : "value3" ,
150+ },
151+ },
152+ {
153+ name : "duplicate keys (last value wins)" ,
154+ headerSlice : []string {"header1" , "value1" , "header1" , "value2" },
155+ expectedResult : map [string ]string {
156+ "header1" : "value2" ,
157+ },
158+ },
159+ {
160+ name : "empty values" ,
161+ headerSlice : []string {"header1" , "" , "header2" , "value2" },
162+ expectedResult : map [string ]string {
163+ "header1" : "" ,
164+ "header2" : "value2" ,
165+ },
166+ },
167+ {
168+ name : "special characters in keys and values" ,
169+ headerSlice : []string {"header-1" , "value with spaces" , "header_2" , "value-with-dashes" },
170+ expectedResult : map [string ]string {
171+ "header-1" : "value with spaces" ,
172+ "header_2" : "value-with-dashes" ,
173+ },
174+ },
175+ }
176+
177+ for _ , tt := range tests {
178+ t .Run (tt .name , func (t * testing.T ) {
179+ ctx := context .Background ()
180+ result := ContextWithRequestMetadataMapFromHeaderSlice (ctx , tt .headerSlice )
181+ metadataMap := MapFromContext (result )
182+
183+ if tt .expectedResult == nil {
184+ require .Nil (t , metadataMap )
185+ } else {
186+ require .NotNil (t , metadataMap )
187+ require .Equal (t , len (tt .expectedResult ), len (metadataMap ))
188+ for key , expectedValue := range tt .expectedResult {
189+ require .Contains (t , metadataMap , key )
190+ require .Equal (t , expectedValue , metadataMap [key ])
191+ }
192+ }
193+ })
194+ }
195+ }
0 commit comments