feat: init api

This commit is contained in:
Ilia Denisov
2026-01-08 13:32:40 +02:00
parent 204d3df8cf
commit 972cfd82be
12 changed files with 240 additions and 108 deletions
+2 -63
View File
@@ -10,71 +10,10 @@ import (
"testing"
"github.com/gin-gonic/gin"
"github.com/iliadenisov/galaxy/internal/model/rest"
"github.com/iliadenisov/galaxy/internal/router"
"github.com/stretchr/testify/assert"
)
func TestRouter(t *testing.T) {
r := router.SetupRouter()
exampleCommand := rest.Command{
Race: "SomeRace",
Vote: &rest.CommandVote{
Recipient: "AnotherRace",
},
}
w := httptest.NewRecorder()
req, _ := http.NewRequest("PUT", "/api/v1/command", cmdBody(exampleCommand))
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code, w.Body)
// error: notblank validator
exampleCommand.Race = ""
w = httptest.NewRecorder()
req, _ = http.NewRequest("PUT", "/api/v1/command", cmdBody(exampleCommand))
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code, w.Body)
exampleCommand.Race = " "
w = httptest.NewRecorder()
req, _ = http.NewRequest("PUT", "/api/v1/command", cmdBody(exampleCommand))
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code, w.Body)
// error: no commands
exampleCommand = rest.Command{
Race: "SomeRace",
}
w = httptest.NewRecorder()
req, _ = http.NewRequest("PUT", "/api/v1/command", cmdBody(exampleCommand))
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code, w.Body)
// error: more than one command
exampleCommand = rest.Command{
Race: "SomeRace",
Vote: &rest.CommandVote{
Recipient: "AnotherRace",
},
DeclarePeace: &rest.CommandDeclarePeace{
Opponent: "OpponentRace",
},
}
w = httptest.NewRecorder()
req, _ = http.NewRequest("PUT", "/api/v1/command", cmdBody(exampleCommand))
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code, w.Body)
}
func TestLimitConnections(t *testing.T) {
r := limitTestingRouter()
@@ -94,8 +33,8 @@ func TestLimitConnections(t *testing.T) {
wg.Wait()
}
func cmdBody(cmd rest.Command) *strings.Reader {
commandJson, _ := json.Marshal(cmd)
func asBody(body any) *strings.Reader {
commandJson, _ := json.Marshal(body)
return strings.NewReader(string(commandJson))
}