File: pingpong_test.go

package info (click to toggle)
golang-github-siddontang-go 0.0~git20170517.0.cb568a3-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 492 kB
  • sloc: makefile: 3
file content (51 lines) | stat: -rw-r--r-- 977 bytes parent folder | download | duplicates (2)
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
package websocket

import (
	"net"
	"net/http"
	"net/url"
	"testing"
	"time"
)

func TestWSPing(t *testing.T) {
	http.HandleFunc("/test/ping", func(w http.ResponseWriter, r *http.Request) {
		conn, err := Upgrade(w, r, nil)
		if err != nil {
			t.Fatal(err.Error())
		}
		//conn := NewConn(c, true)
		conn.Read()
		conn.Pong([]byte{})
		conn.Ping([]byte{})
		msgType, _, _ := conn.Read()
		println(msgType)
	})

	go http.ListenAndServe(":65500", nil)
	time.Sleep(time.Second * 1)

	conn, err := net.Dial("tcp", "127.0.0.1:65500")

	if err != nil {
		t.Fatal(err.Error())
	}
	ws, _, err := NewClient(conn, &url.URL{Host: "127.0.0.1:65500", Path: "/test/ping"}, nil)

	if err != nil {
		t.Fatal(err.Error())
	}
	ws.Ping([]byte{})

	msgType, _, _ := ws.Read()
	if msgType != PongMessage {
		t.Fatal("invalid msg type", msgType)
	}

	msgType, _, _ = ws.Read()
	if msgType != PingMessage {
		t.Fatal("invalid msg type", msgType)
	}
	ws.Pong([]byte{})
	time.Sleep(time.Second * 1)
}