rove/pkg/server/routes_test.go

151 lines
3.6 KiB
Go
Raw Normal View History

package server
import (
2020-05-31 11:22:20 +01:00
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/mdiluz/rove/pkg/game"
"github.com/stretchr/testify/assert"
)
func TestHandleStatus(t *testing.T) {
request, _ := http.NewRequest(http.MethodGet, "/status", nil)
response := httptest.NewRecorder()
s := NewServer()
s.wrapHandler(http.MethodGet, HandleStatus)(response, request)
var status StatusResponse
json.NewDecoder(response.Body).Decode(&status)
if status.Ready != true {
t.Errorf("got false for /status")
}
2020-06-02 15:54:22 +01:00
if len(status.Version) == 0 {
t.Errorf("got empty version info")
}
}
2020-05-31 11:22:20 +01:00
func TestHandleRegister(t *testing.T) {
data := RegisterData{Name: "one"}
b, err := json.Marshal(data)
if err != nil {
t.Error(err)
}
request, _ := http.NewRequest(http.MethodPost, "/register", bytes.NewReader(b))
response := httptest.NewRecorder()
s := NewServer()
s.wrapHandler(http.MethodPost, HandleRegister)(response, request)
2020-05-31 11:22:20 +01:00
var status RegisterResponse
json.NewDecoder(response.Body).Decode(&status)
if status.Success != true {
t.Errorf("got false for /register")
}
}
func TestHandleSpawn(t *testing.T) {
s := NewServer()
a, err := s.accountant.RegisterAccount("test")
assert.NoError(t, err, "Error registering account")
data := SpawnData{Id: a.Id.String()}
b, err := json.Marshal(data)
assert.NoError(t, err, "Error marshalling data")
request, _ := http.NewRequest(http.MethodPost, "/spawn", bytes.NewReader(b))
response := httptest.NewRecorder()
s.wrapHandler(http.MethodPost, HandleSpawn)(response, request)
var status SpawnResponse
json.NewDecoder(response.Body).Decode(&status)
if status.Success != true {
t.Errorf("got false for /spawn")
}
}
func TestHandleCommands(t *testing.T) {
s := NewServer()
a, err := s.accountant.RegisterAccount("test")
assert.NoError(t, err, "Error registering account")
// Spawn the rover rover for the account
_, inst, err := s.SpawnRoverForAccount(a.Id)
pos, err := s.world.RoverPosition(inst)
assert.NoError(t, err, "Couldn't get rover position")
data := CommandsData{
Id: a.Id.String(),
Commands: []Command{
{
Command: CommandMove,
Bearing: 0.0,
Duration: 1,
},
},
}
b, err := json.Marshal(data)
assert.NoError(t, err, "Error marshalling data")
request, _ := http.NewRequest(http.MethodPost, "/commands", bytes.NewReader(b))
response := httptest.NewRecorder()
s.wrapHandler(http.MethodPost, HandleCommands)(response, request)
var status CommandsResponse
json.NewDecoder(response.Body).Decode(&status)
if status.Success != true {
t.Errorf("got false for /commands")
}
attrib, err := s.world.RoverAttributes(inst)
assert.NoError(t, err, "Couldn't get rover attribs")
pos2, err := s.world.RoverPosition(inst)
assert.NoError(t, err, "Couldn't get rover position")
pos.Add(game.Vector{X: 0.0, Y: attrib.Speed * 1}) // Should have moved north by the speed and duration
assert.Equal(t, pos, pos2, "Rover should have moved by bearing")
}
2020-06-04 22:02:33 +01:00
func TestHandleView(t *testing.T) {
s := NewServer()
a, err := s.accountant.RegisterAccount("test")
assert.NoError(t, err, "Error registering account")
// Spawn the rover rover for the account
_, _, err = s.SpawnRoverForAccount(a.Id)
data := ViewData{
Id: a.Id.String(),
}
b, err := json.Marshal(data)
assert.NoError(t, err, "Error marshalling data")
request, _ := http.NewRequest(http.MethodPost, "/view", bytes.NewReader(b))
response := httptest.NewRecorder()
s.wrapHandler(http.MethodPost, HandleView)(response, request)
var status ViewResponse
json.NewDecoder(response.Body).Decode(&status)
if status.Success != true {
t.Errorf("got false for /view")
}
// TODO: Verify the view information
}