added some protocol tests

This commit is contained in:
Stephen McQuay 2013-08-26 23:00:27 -07:00
parent 880d73b54e
commit 63f6d1a2e0

View File

@ -47,23 +47,126 @@ func TestClientIDParse(t *testing.T) {
} }
} }
func TestProtocol(t *testing.T) { var handled bool
http.Handle("/ws/", websocket.Handler(addPlayer))
func DummyServer() (*websocket.Conn, error) {
if !handled {
http.Handle("/ws/", websocket.Handler(addPlayer))
handled = true
}
g = NewGame() g = NewGame()
go g.run() go g.run()
go http.ListenAndServe(*addr, nil) go http.ListenAndServe(*addr, nil)
origin := "http://localhost/" origin := "http://localhost/"
url := "ws://localhost:8666/ws/" url := "ws://localhost:8666/ws/"
ws, err := websocket.Dial(url, "", origin) return websocket.Dial(url, "", origin)
if err != nil { }
t.Errorf("fail: %v", err)
} func TestGoodProtocol(t *testing.T) {
conf, err := Negociate(ws, "robot0", 800, 400) ws, err := DummyServer()
if err != nil {
t.Errorf("fail: %v", err) var idreq IdRequest
} err = websocket.JSON.Receive(ws, &idreq)
log.Printf("%+v", conf) 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")
}
} }