Permalink
Browse files

Check frame validity, and test it as part of websocket-send

  • Loading branch information...
1 parent 0f92169 commit d2e427207aefd210b5b16905ecb17fc77192f483 @ahyatt committed May 27, 2012
Showing with 29 additions and 1 deletion.
  1. +18 −0 websocket-test.el
  2. +11 −1 websocket.el
View
@@ -289,3 +289,21 @@
(should (equal filter-frames (list foo-frame bar-frame)))
(should (equal err-list nil)))))
+(ert-deftest websocket-send ()
+ (let ((ws (make-websocket :conn t :url t :filter t :close-callback t
+ :accept-string t)))
+ (flet ((websocket-ensure-connected (websocket))
+ (websocket-openp (websocket) t)
+ (process-send-string (conn string)))
+ ;; Just make sure there is no error.
+ (websocket-send ws (make-websocket-frame :opcode 'ping
+ :completep t)))
+ (should-error (websocket-send ws
+ (make-websocket-frame :opcode 'text )))
+ (should-error (websocket-send ws
+ (make-websocket-frame :opcode 'close
+ :payload "bye!"
+ :completep t)))
+ (should-error (websocket-send ws
+ (make-websocket-frame :opcode :close)))))
+
View
@@ -379,8 +379,18 @@ is no more output or the connection closes."
(websocket-send websocket (make-websocket-frame :opcode 'text :payload text
:completep t)))
+(defun websocket-check (frame)
+ "Check FRAME for correctness, returning true if correct."
+ (and (equal (not (memq (websocket-frame-opcode frame)
+ '(continuation text binary)))
+ (and (not (websocket-frame-payload frame))
+ (websocket-frame-completep frame)))))
+
(defun websocket-send (websocket frame)
- "To the WEBSOCKET server, send the FRAME."
+ "To the WEBSOCKET server, send the FRAME.
+This will raise an error if the frame is illegal."
+ (unless (websocket-check frame)
+ (error "Cannot send illegal frame to websocket"))
(websocket-debug websocket "Sending frame, opcode: %s payload: %s"
(websocket-frame-opcode frame)
(websocket-frame-payload frame))

0 comments on commit d2e4272

Please sign in to comment.