Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fix all functional tests, and unit tests, fixing many actual issues.

All tests now pass.
  • Loading branch information...
commit b7707e63d407bb1dd198206033de92f6ea5374db 1 parent 5aef12c
Andrew Hyatt authored

Showing 3 changed files with 82 additions and 44 deletions. Show diff stats Hide diff stats

  1. +12 6 websocket-functional-test.el
  2. +36 16 websocket-test.el
  3. +34 22 websocket.el
18 websocket-functional-test.el
@@ -19,13 +19,19 @@
19 19
20 20 (message "Opening the websocket")
21 21
  22 +(defclass websocket-for-test (websocket)
  23 + ;; no new slots needed
  24 + ())
  25 +
  26 +(defmethod websocket-on-message ((ws websocket-for-test) frame)
  27 + (push (websocket-frame-payload frame) wstest-msgs)
  28 + (message "ws frame: %S" (websocket-frame-payload frame)))
  29 +
  30 +(defmethod websocket-on-close ((ws websocket-for-test))
  31 + (setq wstest-closed t))
  32 +
22 33 (defvar wstest-ws
23   - (websocket-open
24   - "ws://127.0.0.1:9999"
25   - :on-message (lambda (websocket frame)
26   - (push (websocket-frame-payload frame) wstest-msgs)
27   - (message "ws frame: %S" (websocket-frame-payload frame)))
28   - :on-close (lambda (websocket) (setq wstest-closed t))))
  34 + (websocket-open 'websocket-for-test "ws://127.0.0.1:9999"))
29 35
30 36 (defun wstest-pop-to-debug ()
31 37 "Open websocket log buffer. Not used in testing. Just for debugging."
52 websocket-test.el
@@ -28,6 +28,23 @@
28 28 (require 'websocket)
29 29 (eval-when-compile (require 'cl))
30 30
  31 +(defvar websocket-test-process (start-process "dummy-process" "dummy-process"
  32 + "echo")
  33 + "Dummy variable so that we can fake out a connection process.")
  34 +
  35 +(defun* websocket-test-websocket (&key on-open on-message on-close
  36 + (accept-string "")
  37 + protocol
  38 + extensions)
  39 + (let ((ws (websocket-testable "test" :protocol protocol)))
  40 + (websocket-private-initialize ws websocket-test-process accept-string)
  41 + (when on-open (set-slot-value ws 'on-open on-open))
  42 + (when on-message (set-slot-value ws 'on-message on-message))
  43 + (when on-close (set-slot-value ws 'on-close on-close))
  44 + ;; This can't be set during construction.
  45 + (when extensions (set-slot-value ws 'requested-extensions extensions))
  46 + ws))
  47 +
31 48 (ert-deftest websocket-genbytes-length ()
32 49 (loop repeat 100
33 50 do (should (= (string-bytes (websocket-genbytes 16)) 16))))
@@ -118,13 +135,16 @@
118 135 (invalid-accept "Sec-WebSocket-Accept: bad")
119 136 (upgrade "Upgrade: websocket")
120 137 (connection "Connection: upgrade")
121   - (ws (websocket :accept-string "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="))
  138 + (ws (websocket-test-websocket
  139 + :accept-string "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="))
122 140 (ws-with-protocol
123   - (websocket :accept-string "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="
124   - :protocol "myprotocol"))
  141 + (websocket-test-websocket
  142 + :accept-string "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="
  143 + :protocol "myprotocol"))
125 144 (ws-with-extensions
126   - (websocket :accept-string "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="
127   - :extensions '("ext1" "ext2"))))
  145 + (websocket-test-websocket
  146 + :accept-string "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="
  147 + :extensions '("ext1" "ext2"))))
128 148 (should (websocket-verify-headers
129 149 ws
130 150 (websocket-test-header-with-lines accept upgrade connection)))
@@ -165,7 +185,7 @@
165 185 (websocket-test-header-with-lines
166 186 accept upgrade connection "Sec-Websocket-Extensions: ext1, ext2; a=1")))
167 187 (should (equal '("ext1" "ext2; a=1")
168   - (websocket-server-extensions ws-with-extensions)))
  188 + (slot-value ws-with-extensions 'extensions)))
169 189 (should
170 190 (websocket-verify-headers
171 191 ws-with-extensions
@@ -173,7 +193,7 @@
173 193 "Sec-Websocket-Extensions: ext1"
174 194 "Sec-Websocket-Extensions: ext2; a=1")))
175 195 (should (equal '("ext1" "ext2; a=1")
176   - (websocket-server-extensions ws-with-extensions)))))
  196 + (slot-value ws-with-extensions 'extensions)))))
177 197
178 198 (ert-deftest websocket-create-headers ()
179 199 (let ((system-name "mysystem")
@@ -223,9 +243,9 @@
223 243
224 244 (ert-deftest websocket-process-frame ()
225 245 (let* ((sent)
226   - (processed)
  246 + (processed)
227 247 (deleted)
228   - (websocket (websocket-testable
  248 + (websocket (websocket-test-websocket
229 249 :on-message (lambda (websocket frame)
230 250 (setq
231 251 processed
@@ -304,15 +324,15 @@
304 324 (websocket-openp (websocket) t)
305 325 (kill-buffer (buffer))
306 326 (process-buffer (conn)))
307   - (websocket-close (websocket "test"))
  327 + (websocket-close (websocket-test-websocket))
308 328 (should (equal sent-frames (list
309 329 (make-websocket-frame :opcode 'close
310 330 :completep t)))))))
311 331
312 332 (ert-deftest websocket-outer-filter ()
313   - (let* ((fake-ws (websocket-testable "test"
  333 + (let* ((fake-ws (websocket-test-websocket
314 334 :on-open (lambda (websocket)
315   - (should (eq (websocket-ready-state websocket)
  335 + (should (eq (slot-value websocket 'ready-state)
316 336 'open))
317 337 (setq open-callback-called t)
318 338 (error "Ignore me!"))))
@@ -331,7 +351,7 @@
331 351 (websocket-verify-response-code (output) t)
332 352 (websocket-verify-headers (websocket output) t))
333 353 (websocket-outer-filter fake-ws "Sec-")
334   - (should (eq (websocket-ready-state fake-ws) 'connecting))
  354 + (should (eq (slot-value fake-ws 'ready-state) 'connecting))
335 355 (should-not open-callback-called)
336 356 (websocket-outer-filter fake-ws "WebSocket-Accept: acceptstring")
337 357 (should-not open-callback-called)
@@ -345,7 +365,7 @@
345 365 (ert-deftest websocket-outer-filter-bad-connection ()
346 366 (let* ((on-open-calledp)
347 367 (websocket-closed-calledp)
348   - (fake-ws (websocket-testable "test"
  368 + (fake-ws (websocket-test-websocket
349 369 :on-open (lambda (websocket)
350 370 (setq on-open-calledp t))
351 371 :on-close (lambda (websocket)
@@ -364,7 +384,7 @@
364 384 (frames &optional callback)
365 385 (let* ((filter-frames)
366 386 (websocket
367   - (websocket-testable "test"
  387 + (websocket-test-websocket
368 388 :on-message (lambda (websocket frame)
369 389 (push frame filter-frames)
370 390 (when callback (funcall callback)))
@@ -409,7 +429,7 @@
409 429 (should (equal err-list nil)))))
410 430
411 431 (ert-deftest websocket-send ()
412   - (let ((ws (websocket "test")))
  432 + (let ((ws (websocket-test-websocket)))
413 433 (flet ((websocket-ensure-connected (websocket))
414 434 (websocket-openp (websocket) t)
415 435 (process-send-string (conn string)))
56 websocket.el
@@ -49,19 +49,26 @@
49 49 :documentation
50 50 "The state of the connection, either CONNECTING, OPEN or CLOSE"
51 51 :protection :public)
52   - (server-extensions :type list
53   - :documentation "The list of extensions the server supports"
54   - :protection :protected)
  52 + (client-extensions :type list
  53 + :documentation "The list of the extensions the client supports"
  54 + :protection :public)
  55 + (requested-extensions :initarg :requested-extensions
  56 + :type list
  57 + :documentation "The list of client-requested extensions"
  58 + :protection :public)
  59 + (extensions :type list
  60 + :documentation "The list of extensions in use"
  61 + :protection :public)
55 62 (url :initarg :url
56 63 :type string
57 64 :documentation "The url this websocket is connecting to."
58 65 :protection :protected)
59 66 (protocol :initarg :protocol
60   - :type string
  67 + :type (or null string)
61 68 :websocket "The protocol requested by the client"
62 69 :protection :protected)
63 70 (conn :initarg :conn
64   - :type processp
  71 + :type process
65 72 :documentation "The connection process for this websocket."
66 73 :protection :private)
67 74 (accept-string :initarg :accept-string
@@ -102,6 +109,11 @@ Putting all client initialization logic here is recommended.")
102 109 (defmethod websocket-on-close ((ws websocket))
103 110 "Called when the websocket connection has been closed.")
104 111
  112 +(defmethod websocket-private-initialize ((ws websocket) conn accept-string)
  113 + "Method to initialize private variables, which cannot be set in the constructor."
  114 + (set-slot-value ws 'conn conn)
  115 + (set-slot-value ws 'accept-string accept-string))
  116 +
105 117 (defmethod websocket-debug ((ws websocket) msg &rest args)
106 118 "In the WEBSOCKET's debug buffer, send MSG, with format ARGS."
107 119 (when websocket-debug
@@ -131,8 +143,8 @@ This will raise an error if the frame is illegal."
131 143 "Check WEBSOCKET and return non-nil if it is open, and either
132 144 connecting or open."
133 145 (and websocket
134   - (not (eq 'close (websocket-ready-state websocket)))
135   - (eq 'open (process-status (websocket-conn websocket)))))
  146 + (not (eq 'close (slot-value websocket 'ready-state)))
  147 + (eq 'open (process-status (slot-value websocket 'conn)))))
136 148
137 149 (defmethod websocket-close ((websocket websocket))
138 150 "Close WEBSOCKET and erase all the old websocket data."
@@ -148,15 +160,16 @@ connecting or open."
148 160
149 161 (defmethod websocket-ensure-connected ((websocket websocket))
150 162 "If the WEBSOCKET connection is closed, open it."
151   - (unless (and (websocket-conn websocket)
152   - (ecase (process-status (websocket-conn websocket))
  163 + (unless (and (slot-value websocket 'conn)
  164 + (ecase (process-status (slot-value websocket 'conn))
153 165 ((run open listen) t)
154 166 ((stop exit signal closed connect failed nil) nil)))
155 167 (websocket-close websocket)
156 168 (websocket-open (object-class websocket)
157 169 (websocket-url websocket)
158   - :protocol (websocket-protocol websocket)
159   - :extensions (websocket-extensions websocket))))
  170 + :protocol (slot-value websocket 'protocol)
  171 + ;; We just re-open with the set of agreed upon extensions.
  172 + :extensions (slot-value websocket 'extensions))))
160 173
161 174 (defmethod websocket-outer-filter ((websocket websocket) output)
162 175 "Filter the WEBSOCKET server's OUTPUT.
@@ -180,7 +193,7 @@ connection is invalid, the connection will be closed."
180 193 (error
181 194 (websocket-close websocket)
182 195 (error err)))
183   - (set-slot-value 'ready-state 'open)
  196 + (set-slot-value websocket 'ready-state 'open)
184 197 (condition-case err (websocket-on-open websocket)
185 198 (error (websocket-error websocket
186 199 "Got error from the on-open function: %s"
@@ -235,12 +248,12 @@ of populating the list of server extensions to WEBSOCKET."
235 248 (dolist (ext extensions)
236 249 (when (not (member
237 250 (first (split-string ext "; ?"))
238   - (slot-value websocket 'extensions)))
  251 + (slot-value websocket 'requested-extensions)))
239 252 (add-to-list 'extra-extensions (first (split-string ext "; ?")))))
240 253 (when extra-extensions
241 254 (error "Non-requested extensions returned by server: %s"
242 255 extra-extensions)))
243   - (set-slot-value websocket 'server-extensions extensions)))
  256 + (set-slot-value websocket 'extensions extensions)))
244 257 ;; return true
245 258 t)
246 259
@@ -492,11 +505,11 @@ variable `websocket-debug' to t."
492 505 (error "Not implemented yet")
493 506 (error "Unknown protocol"))))
494 507 (websocket (make-instance websocket-class
495   - :conn conn
496 508 :url url
497   - :protocol protocol
498   - :extensions (mapcar 'car extensions)
499   - :accept-string (websocket-calculate-accept key))))
  509 + :requested-extensions extensions
  510 + :protocol protocol))
  511 + (accept-string (websocket-calculate-accept key)))
  512 + (websocket-private-initialize websocket conn accept-string)
500 513 (process-put conn :websocket websocket)
501 514 (set-process-filter conn
502 515 (lambda (process output)
@@ -508,7 +521,7 @@ variable `websocket-debug' to t."
508 521 (let ((websocket (process-get process :websocket)))
509 522 (websocket-debug websocket
510 523 "State change to %s" change)
511   - (unless (eq 'closed (websocket-ready-state websocket))
  524 + (unless (eq 'closed (slot-value websocket 'ready-state))
512 525 (condition-case err (websocket-on-close websocket)
513 526 (error (websocket-error
514 527 websocket
@@ -519,7 +532,7 @@ variable `websocket-debug' to t."
519 532 (let ((path (url-filename url-struct)))
520 533 (if (> (length path) 0) path "/"))))
521 534 (websocket-debug websocket "Sending handshake, key: %s, acceptance: %s"
522   - key (websocket-accept-string websocket))
  535 + key accept-string)
523 536 (process-send-string conn
524 537 (websocket-create-headers url key protocol extensions))
525 538 (websocket-debug websocket "Websocket opened")
@@ -553,8 +566,7 @@ These are defined as in `websocket-open'."
553 566
554 567 (defun websocket-get-debug-buffer-create (websocket)
555 568 "Get or create the buffer corresponding to WEBSOCKET."
556   - (get-buffer-create (format " *websocket %s debug*"
557   - (websocket-url websocket))))
  569 + (get-buffer-create (format " *websocket %s debug*" (slot-value websocket 'url))))
558 570
559 571 (defun websocket-error (websocket msg &rest args)
560 572 "Report error message MSG."

0 comments on commit b7707e6

Please sign in to comment.
Something went wrong with that request. Please try again.