MCPcopy
hub / github.com/nats-io/nats.go / TestMsgHeadersCasePreserving

Function TestMsgHeadersCasePreserving

test/headers_test.go:221–527  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

219}
220
221func TestMsgHeadersCasePreserving(t *testing.T) {
222 s := RunServerOnPort(-1)
223 defer s.Shutdown()
224
225 nc, err := nats.Connect(s.ClientURL())
226 if err != nil {
227 t.Fatalf("Error connecting to server: %v", err)
228 }
229 defer nc.Close()
230
231 subject := "headers.test"
232 sub, err := nc.SubscribeSync(subject)
233 if err != nil {
234 t.Fatalf("Could not subscribe to %q: %v", subject, err)
235 }
236 defer sub.Unsubscribe()
237
238 m := nats.NewMsg(subject)
239
240 // http.Header preserves the original keys and allows case-sensitive
241 // lookup by accessing the map directly.
242 hdr := http.Header{
243 "CorrelationID": []string{"123"},
244 "Msg-ID": []string{"456"},
245 "X-NATS-Keys": []string{"A", "B", "C"},
246 "X-Test-Keys": []string{"D", "E", "F"},
247 }
248
249 // Validate that can be used interchangeably with http.Header
250 type HeaderInterface interface {
251 Add(key, value string)
252 Del(key string)
253 Get(key string) string
254 Set(key, value string)
255 Values(key string) []string
256 }
257 var _ HeaderInterface = http.Header{}
258 var _ HeaderInterface = nats.Header{}
259
260 // A NATS Header is the same type as http.Header so simple casting
261 // works to use canonical form used in Go HTTP servers if needed,
262 // and it also preserves the same original keys like Go HTTP requests.
263 m.Header = nats.Header(hdr)
264 http.Header(m.Header).Set("accept-encoding", "json")
265 http.Header(m.Header).Add("AUTHORIZATION", "s3cr3t")
266
267 // Multi Value using the same matching key.
268 m.Header.Set("X-Test", "First")
269 m.Header.Add("X-Test", "Second")
270 m.Header.Add("X-Test", "Third")
271
272 m.Data = []byte("Simple Headers")
273 nc.PublishMsg(m)
274 msg, err := sub.NextMsg(time.Second)
275 if err != nil {
276 t.Fatalf("Did not receive response: %v", err)
277 }
278 // Confirm that received message is just like the one originally sent.

Callers

nothing calls this directly

Calls 15

EqualMethod · 0.95
GetMethod · 0.95
ValuesMethod · 0.95
DelMethod · 0.95
ConnectMethod · 0.80
FatalfMethod · 0.80
UnsubscribeMethod · 0.80
NextMsgMethod · 0.80
ErrorfMethod · 0.80
RequestMsgMethod · 0.80
RunServerOnPortFunction · 0.70
SubscribeSyncMethod · 0.65

Tested by

no test coverage detected