package router_test import ( "encoding/json" "net/http" "net/http/httptest" "testing" "github.com/iliadenisov/galaxy/internal/model/rest" "github.com/stretchr/testify/assert" ) func TestCommand(t *testing.T) { r := setupRouter() payload := rest.Command{ Actor: "SomeRace", Commands: []json.RawMessage{ encodeCommand(&rest.CommandVote{ CommandMeta: rest.CommandMeta{Type: rest.CommandTypeVote}, Recipient: "AnotherRace", }), }, } w := httptest.NewRecorder() req, _ := http.NewRequest("PUT", "/api/v1/command", asBody(payload)) r.ServeHTTP(w, req) assert.Equal(t, http.StatusNoContent, w.Code, w.Body) // error: notblank validator payload.Actor = "" w = httptest.NewRecorder() req, _ = http.NewRequest("PUT", "/api/v1/command", asBody(payload)) r.ServeHTTP(w, req) assert.Equal(t, http.StatusBadRequest, w.Code, w.Body) payload.Actor = " " w = httptest.NewRecorder() req, _ = http.NewRequest("PUT", "/api/v1/command", asBody(payload)) r.ServeHTTP(w, req) assert.Equal(t, http.StatusBadRequest, w.Code, w.Body) // error: no commands payload = rest.Command{ Actor: "SomeRace", } w = httptest.NewRecorder() req, _ = http.NewRequest("PUT", "/api/v1/command", asBody(payload)) r.ServeHTTP(w, req) assert.Equal(t, http.StatusBadRequest, w.Code, w.Body) } func encodeCommand(cmd any) json.RawMessage { v, err := json.Marshal(cmd) if err != nil { panic(err) } return v }