-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting.go
64 lines (53 loc) · 1.42 KB
/
testing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package roomba_api
import (
"fmt"
"log"
"net"
"net/http"
"net/http/httptest"
"net/rpc"
"net/rpc/jsonrpc"
"testing"
"golang.org/x/net/websocket"
)
var testServerAddr string
var testHttpServer *httptest.Server
var testRoombaServer RoombaServer
func StartTestServer() {
testRoombaServer = MakeServer()
rpc_server := rpc.NewServer()
rpc_server.Register(testRoombaServer)
var WebSocketHandler = func(ws *websocket.Conn) {
server_codec := jsonrpc.NewServerCodec(ws)
rpc_server.ServeCodec(server_codec)
}
serveMux := http.NewServeMux()
serveMux.Handle("/ws", websocket.Handler(WebSocketHandler))
testHttpServer = httptest.NewServer(serveMux)
testServerAddr = testHttpServer.Listener.Addr().String()
log.Print("Test WebSocket server listening on ", testServerAddr)
}
func StopTestServer() {
testHttpServer.CloseClientConnections()
testHttpServer.Close()
testHttpServer = nil
}
func newTestClientConfig(path string) *websocket.Config {
config, _ := websocket.NewConfig(fmt.Sprintf("ws://%s%s", testServerAddr, path), "http://localhost")
return config
}
func NewTestClient(t *testing.T) *rpc.Client {
// websocket.Dial()
tcp_client, err := net.Dial("tcp", testServerAddr)
if err != nil {
t.Fatal("dialing", err)
}
ws_client, err := websocket.NewClient(
newTestClientConfig("/ws"),
tcp_client)
if err != nil {
t.Errorf("WebSocket handshake error: %v", err)
return nil
}
return jsonrpc.NewClient(ws_client)
}