MCPcopy
hub / github.com/gofiber/fiber / TestSharedState_SetGet_JSONDataKinds

Function TestSharedState_SetGet_JSONDataKinds

shared_state_test.go:508–561  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

506}
507
508func TestSharedState_SetGet_JSONDataKinds(t *testing.T) {
509 t.Parallel()
510
511 type sample struct {
512 Name string `json:"name"`
513 Count int `json:"count"`
514 }
515
516 app := New(Config{SharedStorage: newSharedStateMemoryStorage(t)})
517
518 t.Run("map", func(t *testing.T) {
519 t.Parallel()
520
521 expected := map[string]any{
522 "name": "fiber",
523 "ok": true,
524 }
525 require.NoError(t, app.SharedState().SetJSON("map", expected, time.Minute))
526
527 var out map[string]any
528 raw, found, err := app.SharedState().GetJSON("map", &out)
529 require.NoError(t, err)
530 require.True(t, found)
531 require.NotEmpty(t, raw)
532 require.Equal(t, expected["name"], out["name"])
533 require.Equal(t, expected["ok"], out["ok"])
534 })
535
536 t.Run("slice", func(t *testing.T) {
537 t.Parallel()
538
539 expected := []int{1, 2, 3, 4}
540 require.NoError(t, app.SharedState().SetJSON("slice", expected, time.Minute))
541
542 var out []int
543 _, found, err := app.SharedState().GetJSON("slice", &out)
544 require.NoError(t, err)
545 require.True(t, found)
546 require.Equal(t, expected, out)
547 })
548
549 t.Run("struct", func(t *testing.T) {
550 t.Parallel()
551
552 expected := sample{Name: "shared", Count: 7}
553 require.NoError(t, app.SharedState().SetJSON("struct", expected, time.Minute))
554
555 var out sample
556 _, found, err := app.SharedState().GetJSON("struct", &out)
557 require.NoError(t, err)
558 require.True(t, found)
559 require.Equal(t, expected, out)
560 })
561}
562
563func TestSharedState_UsesAppJSONCodec(t *testing.T) {
564 t.Parallel()

Callers

nothing calls this directly

Calls 5

SharedStateMethod · 0.80
GetJSONMethod · 0.80
NewFunction · 0.70
SetJSONMethod · 0.45

Tested by

no test coverage detected