refactor: executors and routers

* refactor: executors and routers
This commit is contained in:
Ilia Denisov
2026-02-09 15:53:34 +03:00
committed by GitHub
parent e48a0c8b96
commit d9c8de27e5
38 changed files with 508 additions and 838 deletions
+20 -26
View File
@@ -1,22 +1,25 @@
package router_test
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/iliadenisov/galaxy/internal/model/rest"
"github.com/iliadenisov/galaxy/internal/router"
"github.com/stretchr/testify/assert"
)
func TestCommand(t *testing.T) {
r := router.SetupRouter()
r := setupRouter()
payload := rest.Command{
Race: "SomeRace",
Vote: &rest.CommandVote{
Recipient: "AnotherRace",
Actor: "SomeRace",
Commands: []json.RawMessage{
encodeCommand(&rest.CommandVote{
CommandMeta: rest.CommandMeta{Type: rest.CommandTypeVote},
Recipient: "AnotherRace",
}),
},
}
@@ -24,17 +27,17 @@ func TestCommand(t *testing.T) {
req, _ := http.NewRequest("PUT", "/api/v1/command", asBody(payload))
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code, w.Body)
assert.Equal(t, http.StatusNoContent, w.Code, w.Body)
// error: notblank validator
payload.Race = ""
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.Race = " "
payload.Actor = " "
w = httptest.NewRecorder()
req, _ = http.NewRequest("PUT", "/api/v1/command", asBody(payload))
r.ServeHTTP(w, req)
@@ -43,24 +46,7 @@ func TestCommand(t *testing.T) {
// error: no commands
payload = rest.Command{
Race: "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)
// error: more than one command
payload = rest.Command{
Race: "SomeRace",
Vote: &rest.CommandVote{
Recipient: "AnotherRace",
},
DeclarePeace: &rest.CommandDeclarePeace{
Opponent: "OpponentRace",
},
Actor: "SomeRace",
}
w = httptest.NewRecorder()
@@ -69,3 +55,11 @@ func TestCommand(t *testing.T) {
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
}