MCPcopy
hub / github.com/go-kit/kit / TestEncodeJSONRequest

Function TestEncodeJSONRequest

transport/nats/publisher_test.go:232–281  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

230}
231
232func TestEncodeJSONRequest(t *testing.T) {
233 var data string
234
235 s, c := newNATSConn(t)
236 defer func() { s.Shutdown(); s.WaitForShutdown() }()
237 defer c.Close()
238
239 sub, err := c.QueueSubscribe("natstransport.test", "natstransport", func(msg *nats.Msg) {
240 data = string(msg.Data)
241
242 if err := c.Publish(msg.Reply, []byte("")); err != nil {
243 t.Fatal(err)
244 }
245 })
246 if err != nil {
247 t.Fatal(err)
248 }
249 defer sub.Unsubscribe()
250
251 publisher := natstransport.NewPublisher(
252 c,
253 "natstransport.test",
254 natstransport.EncodeJSONRequest,
255 func(context.Context, *nats.Msg) (interface{}, error) { return nil, nil },
256 ).Endpoint()
257
258 for _, test := range []struct {
259 value interface{}
260 body string
261 }{
262 {nil, "null"},
263 {12, "12"},
264 {1.2, "1.2"},
265 {true, "true"},
266 {"test", "\"test\""},
267 {struct {
268 Foo string `json:"foo"`
269 }{"foo"}, "{\"foo\":\"foo\"}"},
270 } {
271 if _, err := publisher(context.Background(), test.value); err != nil {
272 t.Fatal(err)
273 continue
274 }
275
276 if data != test.body {
277 t.Errorf("%v: actual %#v, expected %#v", test.value, data, test.body)
278 }
279 }
280
281}

Callers

nothing calls this directly

Calls 4

newNATSConnFunction · 0.85
PublishMethod · 0.65
EndpointMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…