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

Function TestCustomInboxPrefix

test/nats_test.go:1119–1160  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1117}
1118
1119func TestCustomInboxPrefix(t *testing.T) {
1120 opts := &nats.Options{}
1121 for _, p := range []string{"$BOB.", "$BOB.*", "$BOB.>", ">", ".", "", "BOB.*.X", "BOB.>.X"} {
1122 err := nats.CustomInboxPrefix(p)(opts)
1123 if err == nil {
1124 t.Fatalf("Expected error for %q", p)
1125 }
1126 }
1127
1128 s := RunServerOnPort(-1)
1129 defer s.Shutdown()
1130
1131 nc, err := nats.Connect(s.ClientURL(), nats.CustomInboxPrefix("$BOB"))
1132 if err != nil {
1133 t.Fatalf("Expected to connect to server, got %v", err)
1134 }
1135 defer nc.Close()
1136
1137 sub, err := nc.Subscribe(nats.NewInbox(), func(msg *nats.Msg) {
1138 if !strings.HasPrefix(msg.Reply, "$BOB.") {
1139 t.Fatalf("invalid inbox subject %q received", msg.Reply)
1140 }
1141
1142 if len(strings.Split(msg.Reply, ".")) != 3 {
1143 t.Fatalf("invalid number tokens in %s", msg.Reply)
1144 }
1145
1146 msg.Respond([]byte("ok"))
1147 })
1148 if err != nil {
1149 t.Fatalf("subscribe failed: %s", err)
1150 }
1151
1152 resp, err := nc.Request(sub.Subject, nil, time.Second)
1153 if err != nil {
1154 t.Fatalf("request failed: %s", err)
1155 }
1156
1157 if !bytes.Equal(resp.Data, []byte("ok")) {
1158 t.Fatalf("did not receive ok: %q", resp.Data)
1159 }
1160}
1161
1162func TestRespInbox(t *testing.T) {
1163 s := RunServerOnPort(-1)

Callers

nothing calls this directly

Calls 9

FatalfMethod · 0.80
ConnectMethod · 0.80
NewInboxMethod · 0.80
EqualMethod · 0.80
RunServerOnPortFunction · 0.70
SubscribeMethod · 0.65
RespondMethod · 0.65
CloseMethod · 0.45
RequestMethod · 0.45

Tested by

no test coverage detected