72 lines
1.6 KiB
Go
72 lines
1.6 KiB
Go
package router_test
|
|
|
|
import (
|
|
"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()
|
|
|
|
payload := rest.Command{
|
|
Race: "SomeRace",
|
|
Vote: &rest.CommandVote{
|
|
Recipient: "AnotherRace",
|
|
},
|
|
}
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("PUT", "/api/v1/command", asBody(payload))
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusOK, w.Code, w.Body)
|
|
|
|
// error: notblank validator
|
|
payload.Race = ""
|
|
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 = " "
|
|
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{
|
|
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",
|
|
},
|
|
}
|
|
|
|
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)
|
|
}
|