]> code.delx.au - gnu-emacs-elpa/blob - packages/websocket/websocket-functional-test.el
* websocket/websocket.el (websocket-server-accept): Mark arg as unused
[gnu-emacs-elpa] / packages / websocket / websocket-functional-test.el
1 ;;; websocket-functional-test.el --- Simple functional testing
2
3 ;; Copyright (c) 2013, 2016 Free Software Foundation, Inc.
4
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.
9 ;;
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.
14 ;;
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/>.
17
18 ;;; Commentary:
19
20 ;; Usage: emacs -batch -Q -L . -l websocket-functional-test.el
21 ;;
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.
25
26 (require 'tls) ;; tests a particular bug we had on emacs 23
27 (setq debug-on-error t)
28 (require 'websocket)
29 (eval-when-compile (require 'cl))
30
31 ;;;;;;;;;;;;;;;;;;;;;;;
32 ;; Local server test ;;
33 ;;;;;;;;;;;;;;;;;;;;;;;
34
35 (message "Testing with local server")
36
37 (setq websocket-debug t)
38
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"))
44 (sleep-for 1)
45
46 (defvar wstest-msgs nil)
47 (defvar wstest-closed nil)
48
49 (message "Opening the websocket")
50
51 (defvar wstest-ws
52 (websocket-open
53 "ws://127.0.0.1:9999"
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))))
59
60 (defun wstest-pop-to-debug ()
61 "Open websocket log buffer. Not used in testing. Just for debugging."
62 (interactive)
63 (pop-to-buffer (websocket-get-debug-buffer-create wstest-ws)))
64
65 (sleep-for 0.1)
66 (assert (websocket-openp wstest-ws))
67
68 (assert (null wstest-msgs))
69
70 (websocket-send-text wstest-ws "Hi!")
71
72 (sleep-for 0.1)
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!")
76 (sleep-for 0.1)
77 (assert (equal (car wstest-msgs) "You said: Hi after error!"))
78
79 (websocket-close wstest-ws)
80 (assert (null (websocket-openp wstest-ws)))
81
82 (if (not (eq system-type 'windows-nt))
83 ; Windows doesn't have support for the SIGSTP signal, so we'll just kill
84 ; the process.
85 (stop-process wstest-server-proc))
86 (kill-process wstest-server-proc)
87
88 ;; Make sure the processes are closed. This happens asynchronously,
89 ;; so let's wait for it.
90 (sleep-for 1)
91 (assert (null (process-list)) t)
92
93 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
94 ;; Remote server test, with wss ;;
95 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
96
97 (when (>= (string-to-number (substring emacs-version 0 2)) 24)
98 (message "Testing with wss://echo.websocket.org")
99 (when (eq system-type 'windows-nt)
100 (message "Windows users must have gnutls DLLs in the emacs bin directory."))
101 (setq wstest-ws
102 (websocket-open
103 "wss://echo.websocket.org"
104 :on-open (lambda (_websocket)
105 (message "Websocket opened"))
106 :on-message (lambda (_websocket frame)
107 (push (websocket-frame-payload frame) wstest-msgs)
108 (message "ws frame: %S" (websocket-frame-payload frame)))
109 :on-close (lambda (_websocket)
110 (message "Websocket closed")
111 (setq wstest-closed t)))
112 wstest-msgs nil)
113 (sleep-for 0.3)
114 (assert (websocket-openp wstest-ws))
115 (assert (eq 'open (websocket-ready-state wstest-ws)))
116 (assert (null wstest-msgs))
117 (websocket-send-text wstest-ws "Hi!")
118 (sleep-for 1)
119 (assert (equal (car wstest-msgs) "Hi!"))
120 (websocket-close wstest-ws))
121
122 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
123 ;; Local client and server ;;
124 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
125
126 (message "Testing with emacs websocket server.")
127 (message "If this does not pass, make sure your firewall allows the connection.")
128 (setq wstest-closed nil)
129 (let ((server-conn (websocket-server
130 9998
131 :host 'local
132 :on-message (lambda (ws frame)
133 (message "Server received text!")
134 (websocket-send-text ws
135 (websocket-frame-payload frame)))
136 :on-open (lambda (_websocket) "Client connection opened!")
137 :on-close (lambda (_websocket)
138 (setq wstest-closed t)))))
139
140 (setq wstest-msgs nil
141 wstest-ws
142 (websocket-open
143 "ws://localhost:9998"
144 :on-message (lambda (_websocket frame)
145 (push (websocket-frame-payload frame) wstest-msgs)
146 (message "ws frame: %S" (websocket-frame-payload frame)))))
147
148 (assert (websocket-openp wstest-ws))
149 (websocket-send-text wstest-ws "Hi to self!")
150 (sleep-for 0.3)
151 (assert (equal (car wstest-msgs) "Hi to self!"))
152 (websocket-server-close server-conn))
153 (assert wstest-closed)
154 (websocket-close wstest-ws)
155
156 (sleep-for 1)
157 (assert (null (process-list)) t)
158 (message "\nAll tests passed!\n")