MCPcopy
hub / github.com/grpc-ecosystem/grpc-gateway / TestOutgoingHeaderMatcher

Function TestOutgoingHeaderMatcher

runtime/handler_test.go:397–466  ·  runtime/handler_test.go::TestOutgoingHeaderMatcher
(t *testing.T)

Source from the content-addressed store, hash-verified

395}
396
397func TestOutgoingHeaderMatcher(t *testing.T) {
398 t.Parallel()
399 msg := &pb.SimpleMessage{Id: "foo"}
400 for _, tc := range []struct {
401 name string
402 md runtime.ServerMetadata
403 headers http.Header
404 matcher runtime.HeaderMatcherFunc
405 }{
406 {
407 name: "default matcher",
408 md: runtime.ServerMetadata{
409 HeaderMD: metadata.Pairs(
410 "foo", "bar",
411 "baz", "qux",
412 ),
413 },
414 headers: http.Header{
415 "Content-Type": []string{"application/json"},
416 "Grpc-Metadata-Foo": []string{"bar"},
417 "Grpc-Metadata-Baz": []string{"qux"},
418 },
419 },
420 {
421 name: "custom matcher",
422 md: runtime.ServerMetadata{
423 HeaderMD: metadata.Pairs(
424 "foo", "bar",
425 "baz", "qux",
426 ),
427 },
428 headers: http.Header{
429 "Content-Type": []string{"application/json"},
430 "Custom-Foo": []string{"bar"},
431 },
432 matcher: func(key string) (string, bool) {
433 switch key {
434 case "foo":
435 return "custom-foo", true
436 default:
437 return "", false
438 }
439 },
440 },
441 } {
442 tc := tc
443 t.Run(tc.name, func(t *testing.T) {
444 t.Parallel()
445 ctx := runtime.NewServerMetadataContext(context.Background(), tc.md)
446
447 req := httptest.NewRequest("GET", "http://example.com/foo", nil)
448 resp := httptest.NewRecorder()
449
450 mux := runtime.NewServeMux(
451 runtime.WithOutgoingHeaderMatcher(tc.matcher),
452 )
453 runtime.ForwardResponseMessage(ctx, mux, &runtime.JSONPb{}, resp, req, msg)
454

Callers

nothing calls this directly

Calls 4

NewServerMetadataContextFunction · 0.92
NewServeMuxFunction · 0.92
ForwardResponseMessageFunction · 0.92

Tested by

no test coverage detected