2013-08-21 07:53:34 -07:00
|
|
|
package main
|
2013-08-19 20:42:28 -07:00
|
|
|
|
|
|
|
import (
|
2013-08-25 23:10:02 -07:00
|
|
|
"code.google.com/p/go.net/websocket"
|
2013-08-19 20:42:28 -07:00
|
|
|
"encoding/json"
|
2013-08-25 23:10:02 -07:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2013-08-19 20:42:28 -07:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type result struct {
|
|
|
|
b bool
|
|
|
|
msg string
|
|
|
|
}
|
|
|
|
type clientIDTest struct {
|
|
|
|
clientid ClientID
|
|
|
|
expected result
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2013-08-25 23:10:02 -07:00
|
|
|
|
|
|
|
func TestProtocol(t *testing.T) {
|
|
|
|
http.Handle("/ws/", websocket.Handler(addPlayer))
|
|
|
|
|
|
|
|
g = NewGame()
|
|
|
|
go g.run()
|
|
|
|
|
|
|
|
go http.ListenAndServe(*addr, nil)
|
|
|
|
|
|
|
|
origin := "http://localhost/"
|
|
|
|
url := "ws://localhost:8666/ws/"
|
|
|
|
ws, err := websocket.Dial(url, "", origin)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("fail: %v", err)
|
|
|
|
}
|
|
|
|
conf, err := Negociate(ws, "robot0", 800, 400)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("fail: %v", err)
|
|
|
|
}
|
|
|
|
log.Printf("%+v", conf)
|
|
|
|
}
|