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

Function TestWsWithCustomHeaders

test/ws_test.go:616–699  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

614}
615
616func TestWsWithCustomHeaders(t *testing.T) {
617 sopts := testWSGetDefaultOptions(t, false)
618 s := RunServerWithOptions(sopts)
619 defer s.Shutdown()
620
621 staticHeader := make(http.Header, 0)
622 staticHeader.Set("Authorization", "Bearer Random Token")
623 headerProvider := func() (http.Header, error) {
624 return staticHeader, nil
625 }
626
627 for _, test := range []struct {
628 name string
629 connectionOptions []nats.Option
630 wantErr bool
631 }{
632 {
633 name: "Failure 1: Both headers and handler present",
634 connectionOptions: []nats.Option{
635 nats.WebSocketConnectionHeadersHandler(headerProvider),
636 nats.WebSocketConnectionHeaders(staticHeader),
637 },
638 wantErr: true,
639 },
640 {
641 name: "Success 1: Headers present as static headers",
642 connectionOptions: []nats.Option{
643 nats.WebSocketConnectionHeaders(staticHeader),
644 },
645 wantErr: false,
646 },
647 {
648 name: "Success 2: Header supplied through handler",
649 connectionOptions: []nats.Option{
650 nats.WebSocketConnectionHeadersHandler(headerProvider),
651 },
652 wantErr: false,
653 },
654 } {
655 t.Run(test.name, func(t *testing.T) {
656 url := fmt.Sprintf("ws://127.0.0.1:%d", sopts.Websocket.Port)
657 nc, err := nats.Connect(url, test.connectionOptions...)
658 if err != nil && test.wantErr {
659 return
660 }
661 if err != nil && !test.wantErr {
662 t.Fatalf("Did not expect error, found error: %v", err)
663 }
664 defer nc.Close()
665 sub, err := nc.SubscribeSync("foo")
666 if err != nil {
667 t.Fatalf("Error on subscribe: %v", err)
668 }
669
670 msgs := make([][]byte, 100)
671 for i := 0; i < len(msgs); i++ {
672 msg := make([]byte, 100)
673 for j := 0; j < len(msg); j++ {

Callers

nothing calls this directly

Calls 10

testWSGetDefaultOptionsFunction · 0.85
ConnectMethod · 0.80
FatalfMethod · 0.80
EqualMethod · 0.80
NextMsgMethod · 0.80
RunServerWithOptionsFunction · 0.70
SetMethod · 0.65
SubscribeSyncMethod · 0.65
PublishMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected