Refactor and implement the api functions
This commit is contained in:
parent
3474e6ca8c
commit
f82565bf22
5 changed files with 139 additions and 108 deletions
|
@ -1,111 +0,0 @@
|
|||
package server
|
||||
|
||||
import "github.com/mdiluz/rove/pkg/game"
|
||||
|
||||
// ==============================
|
||||
// API: /status method: GET
|
||||
// Queries the status of the server
|
||||
|
||||
// StatusResponse is a struct that contains information on the status of the server
|
||||
type StatusResponse struct {
|
||||
Ready bool `json:"ready"`
|
||||
Version string `json:"version"`
|
||||
}
|
||||
|
||||
// ==============================
|
||||
// API: /register method: POST
|
||||
// Registers a user account by name
|
||||
// Responds with a unique ID for that account to be used in future requests
|
||||
|
||||
// RegisterData describes the data to send when registering
|
||||
type RegisterData struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// RegisterResponse describes the response to a register request
|
||||
type RegisterResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Error string `json:"error,omitempty"`
|
||||
|
||||
Id string `json:"id"`
|
||||
}
|
||||
|
||||
// ==============================
|
||||
// API: /spawn method: POST
|
||||
// Spawns the rover for an account
|
||||
// Responds with the position of said rover
|
||||
|
||||
// SpawnData is the data to be sent for the spawn command
|
||||
type SpawnData struct {
|
||||
Id string `json:"id"`
|
||||
}
|
||||
|
||||
// SpawnResponse is the data to respond with on a spawn command
|
||||
type SpawnResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Error string `json:"error,omitempty"`
|
||||
|
||||
// The location of the spawned entity
|
||||
Position game.Vector `json:"position"`
|
||||
}
|
||||
|
||||
// ==============================
|
||||
// API: /commands method: POST
|
||||
// Issues a set of commands from the user
|
||||
|
||||
// CommandsData is a set of commands to execute in order
|
||||
type CommandsData struct {
|
||||
Id string `json:"id"`
|
||||
Commands []Command `json:"commands"`
|
||||
}
|
||||
|
||||
// CommandsResponse is the response to be sent back
|
||||
type CommandsResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
const (
|
||||
// CommandMove describes a single move command
|
||||
CommandMove = "move"
|
||||
)
|
||||
|
||||
const (
|
||||
BearingNorth = "North"
|
||||
BearingNorthEast
|
||||
BearingEast
|
||||
BearingSouthEast
|
||||
BearingSouth
|
||||
BearingSouthWest
|
||||
BearingWest
|
||||
BearingNorthWest
|
||||
)
|
||||
|
||||
// Command describes a single command to execute
|
||||
// it contains the type, and then any members used for each command type
|
||||
type Command struct {
|
||||
// Command is the main command string
|
||||
Command string `json:"command"`
|
||||
|
||||
// Used for CommandMove
|
||||
Bearing string `json:"bearing"` // The direction to move on a compass in short (NW) or long (NorthWest) form
|
||||
Duration int `json:"duration"` // The duration of the move in ticks
|
||||
}
|
||||
|
||||
// ================
|
||||
// API: /radar POST
|
||||
// Queries the current radar for the user
|
||||
|
||||
// RadarData describes the input data to request an accounts current radar
|
||||
type RadarData struct {
|
||||
Id string `json:"id"`
|
||||
}
|
||||
|
||||
// RadarResponse describes the response to a /radar call
|
||||
type RadarResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Error string `json:"error,omitempty"`
|
||||
|
||||
// The set of positions for nearby rovers
|
||||
Rovers []game.Vector `json:"rovers"`
|
||||
}
|
|
@ -1,89 +0,0 @@
|
|||
// +build integration
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var serverUrl = "localhost:80"
|
||||
|
||||
func TestStatus(t *testing.T) {
|
||||
url := url.URL{
|
||||
Scheme: "http",
|
||||
Host: serverUrl,
|
||||
Path: "status",
|
||||
}
|
||||
resp, err := http.Get(url.String())
|
||||
assert.NoError(t, err, "http.Get must not return error")
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode, "http.Get must return StatusOK")
|
||||
|
||||
var status StatusResponse
|
||||
err = json.NewDecoder(resp.Body).Decode(&status)
|
||||
assert.NoError(t, err, "json decode must not return error")
|
||||
|
||||
assert.NoError(t, err, "Status must not return error")
|
||||
assert.True(t, status.Ready, "Server must return ready")
|
||||
assert.NotZero(t, len(status.Version), "Version must not be empty")
|
||||
}
|
||||
|
||||
// helper for register test
|
||||
func register(name string) (register RegisterResponse, err error) {
|
||||
url := url.URL{
|
||||
Scheme: "http",
|
||||
Host: serverUrl,
|
||||
Path: "register",
|
||||
}
|
||||
|
||||
// Marshal the register data struct
|
||||
data := RegisterData{Name: name}
|
||||
marshalled, err := json.Marshal(data)
|
||||
|
||||
// Set up the request
|
||||
req, err := http.NewRequest("POST", url.String(), bytes.NewReader(marshalled))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
// Do the request
|
||||
client := &http.Client{}
|
||||
if resp, err := client.Do(req); err != nil {
|
||||
return RegisterResponse{}, err
|
||||
} else {
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Handle any errors
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return RegisterResponse{}, fmt.Errorf("Status request returned %d", resp.StatusCode)
|
||||
} else {
|
||||
// Decode the reply
|
||||
err = json.NewDecoder(resp.Body).Decode(®ister)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func TestRegister(t *testing.T) {
|
||||
a := uuid.New().String()
|
||||
reg1, err := register(a)
|
||||
assert.NoError(t, err, "Register must not return error")
|
||||
assert.True(t, reg1.Success, "Register must return success")
|
||||
assert.NotZero(t, len(reg1.Id), "Register must return registration ID")
|
||||
|
||||
b := uuid.New().String()
|
||||
reg2, err := register(b)
|
||||
assert.NoError(t, err, "Register must not return error")
|
||||
assert.True(t, reg2.Success, "Register must return success")
|
||||
assert.NotZero(t, len(reg2.Id), "Register must return registration ID")
|
||||
|
||||
reg2, err = register(a)
|
||||
assert.NoError(t, err, "Register must not return error")
|
||||
assert.False(t, reg2.Success, "Register must return fail for duplicate registration")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue