package topic_test import ( "bytes" "encoding/json" "net/http" "net/http/httptest" "testing" "github.com/gin-gonic/gin" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "edge-infra.dev/pkg/sds/interlock/topic" "edge-infra.dev/pkg/sds/interlock/websocket" ) func init() { gin.SetMode(gin.TestMode) } type State struct { ShouldChange string `json:"should-change"` ShouldNotChange string `json:"should-not-change"` } type StatePatch struct { ShouldChange string `json:"should-change"` } func TestDefaultPatch(t *testing.T) { tpc := topic.NewTopic( "test", // initialize the state with "old" strings &State{ ShouldChange: "old", ShouldNotChange: "old", }, nil, websocket.NewManager(), ) r := gin.Default() r.PATCH("/default-patch", tpc.DefaultPatch) // create patch object with ShouldChange field set to "new" input := &StatePatch{ ShouldChange: "new", } out, err := json.Marshal(input) require.NoError(t, err) w := httptest.NewRecorder() req, _ := http.NewRequest("PATCH", "/default-patch", bytes.NewBuffer(out)) r.ServeHTTP(w, req) // assert that the request was successful assert.Equal(t, http.StatusAccepted, w.Code) // create expected object with ShouldNotChange set to original "old" string, // and ShouldChange set to the 'hopefully' patched "new" string expected := &State{ ShouldChange: "new", ShouldNotChange: "old", } output := &State{} require.NoError(t, json.Unmarshal(w.Body.Bytes(), output)) assert.Equal(t, expected, output) }