From d7116f11cd3c5d7944fada563ce419fbe79b19df Mon Sep 17 00:00:00 2001 From: giongto35 Date: Wed, 1 May 2019 16:37:15 +0800 Subject: [PATCH] Update test --- cmd/main.go | 3 ++- cmd/main_test.go | 8 +++++++- handler/gamelist/games.go | 15 ++++++++------- handler/handlers.go | 9 ++++++--- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 63a8deef..b37edf59 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -17,6 +17,7 @@ import ( const ( gameboyIndex = "./static/gameboy.html" debugIndex = "./static/index_ws.html" + gamePath = "games" ) // Time allowed to write a message to the peer. @@ -53,7 +54,7 @@ func initializeServer() { log.Println("Run as a single server") } - handler := handler.NewHandler(conn, *config.IsDebug) + handler := handler.NewHandler(conn, *config.IsDebug, gamePath) // ignore origin upgrader.CheckOrigin = func(r *http.Request) bool { return true } diff --git a/cmd/main_test.go b/cmd/main_test.go index 7d30f0ac..3ff088a2 100644 --- a/cmd/main_test.go +++ b/cmd/main_test.go @@ -17,6 +17,9 @@ import ( ) var host = "http://localhost:8000" + +// Test is in cmd, so gamePath is in parent path +var testGamePath = "../games" var webrtcconfig = webrtc.Configuration{ICEServers: []webrtc.ICEServer{{URLs: []string{"stun:stun.l.google.com:19302"}}}} func initOverlord() *httptest.Server { @@ -26,7 +29,8 @@ func initOverlord() *httptest.Server { } func initServer(t *testing.T, oconn *websocket.Conn) *httptest.Server { - handler := handler.NewHandler(oconn, true) + fmt.Println("Spawn new server") + handler := handler.NewHandler(oconn, true, testGamePath) server := httptest.NewServer(http.HandlerFunc(handler.WS)) return server } @@ -52,6 +56,7 @@ func initClient(t *testing.T, host string) { u := "ws" + strings.TrimPrefix(host, "http") // Connect to the server + fmt.Println("Connecting to server") ws, _, err := websocket.DefaultDialer.Dial(u, nil) if err != nil { t.Fatalf("%v", err) @@ -60,6 +65,7 @@ func initClient(t *testing.T, host string) { // Simulate peerconnection initialization from client + fmt.Println("Simulating PeerConnection") peerConnection, err := webrtc.NewPeerConnection(webrtcconfig) if err != nil { t.Fatalf("%v", err) diff --git a/handler/gamelist/games.go b/handler/gamelist/games.go index 6d0cdf4a..8cc714b8 100644 --- a/handler/gamelist/games.go +++ b/handler/gamelist/games.go @@ -2,18 +2,18 @@ package gamelist import ( "encoding/json" + "fmt" "os" "path/filepath" ) -const gamePath = "games" - // getGameList returns list of games stored in games -func GetGameList() []string { +// TODO: change to class +func GetGameList(gamePath string) []string { var games []string filepath.Walk(gamePath, func(path string, info os.FileInfo, err error) error { - if !info.IsDir() { - // remove prefix + if info != nil && !info.IsDir() { + // Remove prefix to obtain file names path = path[len(gamePath)+1:] // Add to games list games = append(games, path) @@ -25,7 +25,8 @@ func GetGameList() []string { } // GetEncodedGameList returns game list in encoded wspacket format -func GetEncodedGameList() string { - encodedList, _ := json.Marshal(GetGameList()) +func GetEncodedGameList(gamePath string) string { + encodedList, _ := json.Marshal(GetGameList(gamePath)) + fmt.Println(encodedList) return string(encodedList) } diff --git a/handler/handlers.go b/handler/handlers.go index 79e62337..918b6b47 100644 --- a/handler/handlers.go +++ b/handler/handlers.go @@ -40,6 +40,7 @@ type Handler struct { // isDebug determines the mode handler is running isDebug bool isOverlord bool + gamePath string // ID to peerconnection peerconnections map[string]*webrtc.WebRTC @@ -48,16 +49,18 @@ type Handler struct { } // NewHandler returns a new server -func NewHandler(overlordConn *websocket.Conn, isDebug bool) *Handler { +func NewHandler(overlordConn *websocket.Conn, isDebug bool, gamePath string) *Handler { //conn, err := createOverlordConnection() //if err != nil { //return nil, err //} return &Handler{ - isDebug: isDebug, oClient: NewOverlordClient(overlordConn), rooms: map[string]*Room{}, peerconnections: map[string]*webrtc.WebRTC{}, + + isDebug: isDebug, + gamePath: gamePath, } } @@ -106,7 +109,7 @@ func (h *Handler) WS(w http.ResponseWriter, r *http.Request) { wssession.BrowserClient.Send(cws.WSPacket{ ID: "gamelist", - Data: gamelist.GetEncodedGameList(), + Data: gamelist.GetEncodedGameList(h.gamePath), }, nil) wssession.BrowserClient.Listen()