1 ;;; websocket-functional-test.el --- Simple functional testing
3 ;; Copyright (c) 2013 Free Software Foundation, Inc.
5 ;; This program is free software; you can redistribute it and/or
6 ;; modify it under the terms of the GNU General Public License as
7 ;; published by the Free Software Foundation; either version 3 of the
8 ;; License, or (at your option) any later version.
10 ;; This program is distributed in the hope that it will be useful, but
11 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;; General Public License for more details.
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
20 ;; Usage: emacs -batch -Q -L . -l websocket-functional-test.el
22 ;; Note: this functional tests requires that you have python with the
23 ;; Tornado web server. See http://www.tornadoweb.org/en/stable/ for
24 ;; information on aquiring.
26 (require 'tls) ;; tests a particular bug we had on emacs 23
27 (setq debug-on-error t)
29 (eval-when-compile (require 'cl))
31 ;;;;;;;;;;;;;;;;;;;;;;;
32 ;; Local server test ;;
33 ;;;;;;;;;;;;;;;;;;;;;;;
35 (message "Testing with local server")
37 (setq websocket-debug t)
39 (defvar wstest-server-buffer (get-buffer-create "*wstest-server*"))
40 (defvar wstest-server-name "wstest-server")
41 (defvar wstest-server-proc
42 (start-process wstest-server-name wstest-server-buffer
43 "python" "testserver.py" "--log_to_stderr" "--logging=debug"))
46 (defvar wstest-msgs nil)
47 (defvar wstest-closed nil)
49 (message "Opening the websocket")
54 :on-message (lambda (websocket frame)
55 (push (websocket-frame-payload frame) wstest-msgs)
56 (message "ws frame: %S" (websocket-frame-payload frame))
57 (error "Test error (expected)"))
58 :on-close (lambda (websocket) (setq wstest-closed t))))
60 (defun wstest-pop-to-debug ()
61 "Open websocket log buffer. Not used in testing. Just for debugging."
63 (pop-to-buffer (websocket-get-debug-buffer-create wstest-ws)))
66 (assert (websocket-openp wstest-ws))
68 (assert (null wstest-msgs))
70 (websocket-send-text wstest-ws "Hi!")
73 (assert (equal (car wstest-msgs) "You said: Hi!"))
74 (setf (websocket-on-error wstest-ws) (lambda (ws type err)))
75 (websocket-send-text wstest-ws "Hi after error!")
77 (assert (equal (car wstest-msgs) "You said: Hi after error!"))
79 (websocket-close wstest-ws)
80 (assert (null (websocket-openp wstest-ws)))
82 (stop-process wstest-server-proc)
83 (kill-process wstest-server-proc)
85 ;; Make sure the processes are closed. This happens asynchronously,
86 ;; so let's wait for it.
88 (assert (null (process-list)) t)
90 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
91 ;; Remote server test, with wss ;;
92 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
94 (when (>= (string-to-int (substring emacs-version 0 2)) 24)
95 (message "Testing with wss://echo.websocket.org")
98 "wss://echo.websocket.org"
99 :on-open (lambda (websocket)
100 (message "Websocket opened"))
101 :on-message (lambda (websocket frame)
102 (push (websocket-frame-payload frame) wstest-msgs)
103 (message "ws frame: %S" (websocket-frame-payload frame)))
104 :on-close (lambda (websocket)
105 (message "Websocket closed")
106 (setq wstest-closed t)))
109 (assert (websocket-openp wstest-ws))
110 (assert (eq 'open (websocket-ready-state wstest-ws)))
111 (assert (null wstest-msgs))
112 (websocket-send-text wstest-ws "Hi!")
114 (assert (equal (car wstest-msgs) "Hi!"))
115 (websocket-close wstest-ws))
117 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
118 ;; Local client and server ;;
119 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
121 (message "Testing with emacs websocket server.")
122 (message "If this does not pass, make sure your firewall allows the connection.")
123 (setq wstest-closed nil)
124 (setq server-conn (websocket-server
126 :on-message (lambda (ws frame)
127 (message "Server received text!")
128 (websocket-send-text ws
129 (websocket-frame-payload frame)))
130 :on-open (lambda (websocket) "Client connection opened!")
131 :on-close (lambda (websocket)
132 (setq wstest-closed t))))
134 (setq wstest-msgs nil
137 "ws://localhost:9998"
138 :on-message (lambda (websocket frame)
139 (push (websocket-frame-payload frame) wstest-msgs)
140 (message "ws frame: %S" (websocket-frame-payload frame)))))
142 (assert (websocket-openp wstest-ws))
143 (websocket-send-text wstest-ws "Hi to self!")
145 (assert (equal (car wstest-msgs) "Hi to self!"))
146 (websocket-server-close server-conn)
147 (assert wstest-closed)
148 (websocket-close wstest-ws)
151 (assert (null (process-list)) t)
152 (message "\nAll tests passed!\n")