...
1 package topic_test
2
3 import (
4 "bytes"
5 "encoding/json"
6 "net/http"
7 "net/http/httptest"
8 "testing"
9
10 "github.com/gin-gonic/gin"
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
13
14 "edge-infra.dev/pkg/sds/interlock/topic"
15 "edge-infra.dev/pkg/sds/interlock/websocket"
16 )
17
18 func init() {
19 gin.SetMode(gin.TestMode)
20 }
21
22 type State struct {
23 ShouldChange string `json:"should-change"`
24 ShouldNotChange string `json:"should-not-change"`
25 }
26
27 type StatePatch struct {
28 ShouldChange string `json:"should-change"`
29 }
30
31 func TestDefaultPatch(t *testing.T) {
32 tpc := topic.NewTopic(
33 "test",
34
35 &State{
36 ShouldChange: "old",
37 ShouldNotChange: "old",
38 },
39 nil,
40 websocket.NewManager(),
41 )
42
43 r := gin.Default()
44 r.PATCH("/default-patch", tpc.DefaultPatch)
45
46
47 input := &StatePatch{
48 ShouldChange: "new",
49 }
50 out, err := json.Marshal(input)
51 require.NoError(t, err)
52
53 w := httptest.NewRecorder()
54 req, _ := http.NewRequest("PATCH", "/default-patch", bytes.NewBuffer(out))
55 r.ServeHTTP(w, req)
56
57
58 assert.Equal(t, http.StatusAccepted, w.Code)
59
60
61
62 expected := &State{
63 ShouldChange: "new",
64 ShouldNotChange: "old",
65 }
66
67 output := &State{}
68 require.NoError(t, json.Unmarshal(w.Body.Bytes(), output))
69
70 assert.Equal(t, expected, output)
71 }
72
View as plain text