websocket 增加多分组 fork https://github.com/olahol/melody
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

329 lines
6.2 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package melody
  2. import (
  3. "github.com/gorilla/websocket"
  4. "net/http"
  5. "net/http/httptest"
  6. "strings"
  7. "testing"
  8. "testing/quick"
  9. "time"
  10. )
  11. type TestServer struct {
  12. m *Melody
  13. }
  14. func NewTestServerHandler(handler handleMessageFunc) *TestServer {
  15. m := New()
  16. m.HandleMessage(handler)
  17. return &TestServer{
  18. m: m,
  19. }
  20. }
  21. func NewTestServer() *TestServer {
  22. m := New()
  23. return &TestServer{
  24. m: m,
  25. }
  26. }
  27. func (s *TestServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  28. s.m.HandleRequest(w, r)
  29. }
  30. func NewDialer(url string) (*websocket.Conn, error) {
  31. dialer := &websocket.Dialer{}
  32. conn, _, err := dialer.Dial(strings.Replace(url, "http", "ws", 1), nil)
  33. return conn, err
  34. }
  35. func TestEcho(t *testing.T) {
  36. echo := NewTestServerHandler(func(session *Session, msg []byte) {
  37. session.Write(msg)
  38. })
  39. server := httptest.NewServer(echo)
  40. defer server.Close()
  41. fn := func(msg string) bool {
  42. conn, err := NewDialer(server.URL)
  43. defer conn.Close()
  44. if err != nil {
  45. t.Error(err)
  46. return false
  47. }
  48. conn.WriteMessage(websocket.TextMessage, []byte(msg))
  49. _, ret, err := conn.ReadMessage()
  50. if err != nil {
  51. t.Error(err)
  52. return false
  53. }
  54. if msg != string(ret) {
  55. t.Errorf("%s should equal %s", msg, string(ret))
  56. return false
  57. }
  58. return true
  59. }
  60. if err := quick.Check(fn, nil); err != nil {
  61. t.Error(err)
  62. }
  63. }
  64. func TestEchoBinary(t *testing.T) {
  65. echo := NewTestServer()
  66. echo.m.HandleMessageBinary(func(session *Session, msg []byte) {
  67. session.WriteBinary(msg)
  68. })
  69. server := httptest.NewServer(echo)
  70. defer server.Close()
  71. fn := func(msg string) bool {
  72. conn, err := NewDialer(server.URL)
  73. defer conn.Close()
  74. if err != nil {
  75. t.Error(err)
  76. return false
  77. }
  78. conn.WriteMessage(websocket.BinaryMessage, []byte(msg))
  79. _, ret, err := conn.ReadMessage()
  80. if err != nil {
  81. t.Error(err)
  82. return false
  83. }
  84. if msg != string(ret) {
  85. t.Errorf("%s should equal %s", msg, string(ret))
  86. return false
  87. }
  88. return true
  89. }
  90. if err := quick.Check(fn, nil); err != nil {
  91. t.Error(err)
  92. }
  93. }
  94. func TestHandlers(t *testing.T) {
  95. echo := NewTestServer()
  96. echo.m.HandleMessage(func(session *Session, msg []byte) {
  97. session.Write(msg)
  98. })
  99. server := httptest.NewServer(echo)
  100. defer server.Close()
  101. var q *Session
  102. echo.m.HandleConnect(func(session *Session) {
  103. q = session
  104. session.Close()
  105. })
  106. echo.m.HandleDisconnect(func(session *Session) {
  107. if q != session {
  108. t.Error("disconnecting session should be the same as connecting")
  109. }
  110. })
  111. NewDialer(server.URL)
  112. }
  113. func TestUpgrader(t *testing.T) {
  114. broadcast := NewTestServer()
  115. broadcast.m.HandleMessage(func(session *Session, msg []byte) {
  116. session.Write(msg)
  117. })
  118. server := httptest.NewServer(broadcast)
  119. defer server.Close()
  120. broadcast.m.Upgrader = &websocket.Upgrader{
  121. ReadBufferSize: 1024,
  122. WriteBufferSize: 1024,
  123. CheckOrigin: func(r *http.Request) bool { return false },
  124. }
  125. broadcast.m.HandleError(func(session *Session, err error) {
  126. if err == nil || err.Error() != "websocket: origin not allowed" {
  127. t.Error("there should be a origin error")
  128. }
  129. })
  130. _, err := NewDialer(server.URL)
  131. if err == nil || err.Error() != "websocket: bad handshake" {
  132. t.Error("there should be a badhandshake error")
  133. }
  134. }
  135. /*
  136. func TestBroadcast(t *testing.T) {
  137. broadcast := NewTestServer()
  138. broadcast.m.HandleMessage(func(session *Session, msg []byte) {
  139. broadcast.m.Broadcast(msg)
  140. })
  141. server := httptest.NewServer(broadcast)
  142. defer server.Close()
  143. n := 10
  144. fn := func(msg string) bool {
  145. conn, _ := NewDialer(server.URL)
  146. defer conn.Close()
  147. listeners := make([]*websocket.Conn, n)
  148. for i := 0; i < n; i++ {
  149. listener, _ := NewDialer(server.URL)
  150. listeners[i] = listener
  151. defer listeners[i].Close()
  152. }
  153. conn.WriteMessage(websocket.TextMessage, []byte(msg))
  154. for i := 0; i < n; i++ {
  155. _, ret, err := listeners[i].ReadMessage()
  156. if err != nil {
  157. t.Error(err)
  158. return false
  159. }
  160. if msg != string(ret) {
  161. t.Errorf("%s should equal %s", msg, string(ret))
  162. return false
  163. }
  164. }
  165. return true
  166. }
  167. if err := quick.Check(fn, nil); err != nil {
  168. t.Error(err)
  169. }
  170. }
  171. */
  172. func TestBroadcastOthers(t *testing.T) {
  173. broadcast := NewTestServer()
  174. broadcast.m.HandleMessage(func(session *Session, msg []byte) {
  175. broadcast.m.BroadcastOthers(msg, session)
  176. })
  177. broadcast.m.Config.PongWait = time.Second
  178. broadcast.m.Config.PingPeriod = time.Second * 9 / 10
  179. server := httptest.NewServer(broadcast)
  180. defer server.Close()
  181. n := 10
  182. fn := func(msg string) bool {
  183. conn, _ := NewDialer(server.URL)
  184. defer conn.Close()
  185. listeners := make([]*websocket.Conn, n)
  186. for i := 0; i < n; i++ {
  187. listener, _ := NewDialer(server.URL)
  188. listeners[i] = listener
  189. defer listeners[i].Close()
  190. }
  191. conn.WriteMessage(websocket.TextMessage, []byte(msg))
  192. for i := 0; i < n; i++ {
  193. _, ret, err := listeners[i].ReadMessage()
  194. if err != nil {
  195. t.Error(err)
  196. return false
  197. }
  198. if msg != string(ret) {
  199. t.Errorf("%s should equal %s", msg, string(ret))
  200. return false
  201. }
  202. }
  203. return true
  204. }
  205. if err := quick.Check(fn, nil); err != nil {
  206. t.Error(err)
  207. }
  208. }
  209. func TestPingPong(t *testing.T) {
  210. noecho := NewTestServer()
  211. noecho.m.Config.PongWait = time.Second
  212. noecho.m.Config.PingPeriod = time.Second * 9 / 10
  213. server := httptest.NewServer(noecho)
  214. defer server.Close()
  215. conn, err := NewDialer(server.URL)
  216. conn.SetPingHandler(func(string) error {
  217. return nil
  218. })
  219. defer conn.Close()
  220. if err != nil {
  221. t.Error(err)
  222. }
  223. conn.WriteMessage(websocket.TextMessage, []byte("test"))
  224. _, _, err = conn.ReadMessage()
  225. if err == nil {
  226. t.Error("there should be an error")
  227. }
  228. }
  229. func TestBroadcastFilter(t *testing.T) {
  230. broadcast := NewTestServer()
  231. broadcast.m.HandleMessage(func(session *Session, msg []byte) {
  232. broadcast.m.BroadcastFilter(msg, func(q *Session) bool {
  233. return session == q
  234. })
  235. })
  236. server := httptest.NewServer(broadcast)
  237. defer server.Close()
  238. fn := func(msg string) bool {
  239. conn, err := NewDialer(server.URL)
  240. defer conn.Close()
  241. if err != nil {
  242. t.Error(err)
  243. return false
  244. }
  245. conn.WriteMessage(websocket.TextMessage, []byte(msg))
  246. _, ret, err := conn.ReadMessage()
  247. if err != nil {
  248. t.Error(err)
  249. return false
  250. }
  251. if msg != string(ret) {
  252. t.Errorf("%s should equal %s", msg, string(ret))
  253. return false
  254. }
  255. return true
  256. }
  257. if err := quick.Check(fn, nil); err != nil {
  258. t.Error(err)
  259. }
  260. }