renamed the file housing the http control protocol, removed defunct test
This commit is contained in:
parent
2a2f4f6f96
commit
c5bddcdc31
250
protocol_test.go
250
protocol_test.go
@ -1,250 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"code.google.com/p/go.net/websocket"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type result struct {
|
||||
b bool
|
||||
msg string
|
||||
}
|
||||
|
||||
type clientIDTest struct {
|
||||
clientid ClientID
|
||||
expected result
|
||||
}
|
||||
|
||||
type CreatedGame struct {
|
||||
Id string `json:"id"`
|
||||
}
|
||||
|
||||
var clientIDTests = []clientIDTest{
|
||||
{ClientID{Type: "robot"}, result{true, ""}},
|
||||
{ClientID{Type: "spectator"}, result{true, ""}},
|
||||
{ClientID{Type: "schmarglenoggler"}, result{false, "usergent must be 'robot' or 'spectator'"}},
|
||||
}
|
||||
|
||||
func TestClientIDs(t *testing.T) {
|
||||
for _, tt := range clientIDTests {
|
||||
v, msg := tt.clientid.Valid()
|
||||
actual := result{v, msg}
|
||||
if actual.b != tt.expected.b || actual.msg != tt.expected.msg {
|
||||
t.Errorf("%+v: expected %v, actual %v", tt.clientid, tt.expected, actual)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientIDParse(t *testing.T) {
|
||||
var s ClientID
|
||||
err := json.Unmarshal(
|
||||
[]byte(`{
|
||||
"type": "robot",
|
||||
"name": "dummy",
|
||||
"id": "24601",
|
||||
"useragent": "gorobots.js"
|
||||
}`), &s)
|
||||
if err != nil {
|
||||
t.Errorf("fail to parse: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
var handled bool
|
||||
|
||||
func DummyServer() (*websocket.Conn, error) {
|
||||
if !handled {
|
||||
http.Handle("/ws/", websocket.Handler(addPlayer))
|
||||
http.Handle("/game/start/", JsonHandler(startGame))
|
||||
handled = true
|
||||
}
|
||||
|
||||
games = make(map[string]*game)
|
||||
idg = NewIdGenerator()
|
||||
|
||||
go http.ListenAndServe(*addr, nil)
|
||||
|
||||
origin := "http://localhost/"
|
||||
url := "ws://localhost:8666/ws/"
|
||||
return websocket.Dial(url, "", origin)
|
||||
}
|
||||
|
||||
func TestGoodProtocol(t *testing.T) {
|
||||
ws, err := DummyServer()
|
||||
log.Printf("blah: %+v", ws)
|
||||
|
||||
resp, err := http.Get("http://localhost:8666/game/start/")
|
||||
if err != nil {
|
||||
t.Errorf("error getting: %+v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var g CreatedGame
|
||||
if err := json.NewDecoder(resp.Body).Decode(&g); err != nil {
|
||||
t.Errorf("json parse error? %+v", err)
|
||||
}
|
||||
log.Printf("g: %+v\n", g)
|
||||
|
||||
err = websocket.JSON.Send(ws, GameID{
|
||||
"test",
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
var idreq IdRequest
|
||||
err = websocket.JSON.Receive(ws, &idreq)
|
||||
if err != nil {
|
||||
t.Errorf("fail: %v", err)
|
||||
}
|
||||
|
||||
err = websocket.JSON.Send(ws, ClientID{
|
||||
"robot",
|
||||
"smcquay test robot 2",
|
||||
"unittest",
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
var gameparam GameParam
|
||||
err = websocket.JSON.Receive(ws, &gameparam)
|
||||
if gameparam.Type != "gameparam" {
|
||||
t.Errorf("didn't receive a good gameparam")
|
||||
}
|
||||
|
||||
err = websocket.JSON.Send(ws, &Config{
|
||||
"bogus",
|
||||
stats{
|
||||
Hp: 200,
|
||||
Speed: 100,
|
||||
WeaponRadius: 50,
|
||||
ScannerRadius: 50,
|
||||
},
|
||||
})
|
||||
|
||||
var handshake Handshake
|
||||
websocket.JSON.Receive(ws, &handshake)
|
||||
if !handshake.Success {
|
||||
t.Errorf("failed to validate correct stats request")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTooManyStatsProtocol(t *testing.T) {
|
||||
ws, err := DummyServer()
|
||||
|
||||
var idreq IdRequest
|
||||
err = websocket.JSON.Receive(ws, &idreq)
|
||||
if err != nil {
|
||||
t.Errorf("fail: %v", err)
|
||||
}
|
||||
|
||||
err = websocket.JSON.Send(ws, ClientID{
|
||||
"robot",
|
||||
"smcquay test robot",
|
||||
"unittest",
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
var gameparam GameParam
|
||||
err = websocket.JSON.Receive(ws, &gameparam)
|
||||
if gameparam.Type != "gameparam" {
|
||||
t.Errorf("didn't receive a good gameparam")
|
||||
}
|
||||
|
||||
err = websocket.JSON.Send(ws, &Config{
|
||||
"bogus",
|
||||
stats{
|
||||
Hp: 200,
|
||||
Speed: 100,
|
||||
WeaponRadius: 150,
|
||||
ScannerRadius: 150,
|
||||
},
|
||||
})
|
||||
|
||||
var handshake Handshake
|
||||
websocket.JSON.Receive(ws, &handshake)
|
||||
if handshake.Success {
|
||||
t.Errorf("failed to detect bad stats request")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadUseragent(t *testing.T) {
|
||||
ws, err := DummyServer()
|
||||
|
||||
var idreq IdRequest
|
||||
err = websocket.JSON.Receive(ws, &idreq)
|
||||
if err != nil {
|
||||
t.Errorf("fail: %v", err)
|
||||
}
|
||||
|
||||
err = websocket.JSON.Send(ws, ClientID{
|
||||
"shitsnacks",
|
||||
"smcquay test robot",
|
||||
"unittest",
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
var gameparam GameParam
|
||||
err = websocket.JSON.Receive(ws, &gameparam)
|
||||
if gameparam.Type != "failure" {
|
||||
t.Errorf("tried sending bad type, didn't detect it")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOutOfOrderProtocol(t *testing.T) {
|
||||
ws, err := DummyServer()
|
||||
|
||||
// send early here:
|
||||
err = websocket.JSON.Send(ws, ClientID{
|
||||
"robot",
|
||||
"smcquay test robot 2",
|
||||
"unittest",
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
var idreq IdRequest
|
||||
err = websocket.JSON.Receive(ws, &idreq)
|
||||
if idreq.Type != "idreq" {
|
||||
t.Errorf("Server skipped a step (idreq)")
|
||||
}
|
||||
|
||||
// XXX: this thing gets interpreted server-side as a Config ...
|
||||
err = websocket.JSON.Send(ws, ClientID{
|
||||
Type: "why doesn't this matter?",
|
||||
Name: "smcquay test robot 2",
|
||||
Useragent: "unittest",
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
var gameparam GameParam
|
||||
err = websocket.JSON.Receive(ws, &gameparam)
|
||||
if gameparam.Type != "gameparam" {
|
||||
t.Errorf("didn't receive a good gameparam")
|
||||
}
|
||||
|
||||
err = websocket.JSON.Send(ws, &Config{
|
||||
"bogus",
|
||||
stats{
|
||||
Hp: 200000,
|
||||
Speed: 10000,
|
||||
WeaponRadius: 5000,
|
||||
ScannerRadius: 50,
|
||||
},
|
||||
})
|
||||
|
||||
var handshake Handshake
|
||||
websocket.JSON.Receive(ws, &handshake)
|
||||
if !handshake.Success {
|
||||
t.Errorf("failed to validate correct stats request")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user