Skip to content

Commit

Permalink
Add tests for frame parsing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
andreas committed Jan 5, 2019
1 parent e3d7a1e commit 8382d0e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/websocket.ml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ module Opcode = struct
let to_int = code
let of_int = of_code
let of_int_exn = of_code_exn

let pp_hum fmt t =
Format.fprintf fmt "%d" (to_int t)
end

module Close_code = struct
Expand Down
4 changes: 4 additions & 0 deletions lib/websocket.mli
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ module Opcode : sig

val of_int : int -> t option
val of_int_exn : int -> t

val pp_hum : Format.formatter -> t -> unit
end

module Close_code : sig
Expand Down Expand Up @@ -71,6 +73,8 @@ module Frame : sig

val mask_exn : t -> int32

val length : t -> int

val payload_length : t -> int
val with_payload : t -> f:(Bigstring.t -> off:int -> len:int -> 'a) -> 'a

Expand Down
9 changes: 9 additions & 0 deletions lib_test/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(executable
(libraries websocketaf alcotest)
(name test_websocketaf))

(alias
(name runtest)
(package websocketaf)
(deps (:test test_websocketaf.exe))
(action (run %{test} -v)))
48 changes: 48 additions & 0 deletions lib_test/test_websocketaf.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module Websocket = struct
open Websocketaf.Websocket

module Testable = struct
let opcode = Alcotest.testable Opcode.pp_hum (=)
end

let parse_frame serialized_frame =
match Angstrom.parse_string Frame.parse serialized_frame with
| Ok frame -> frame
| Error err -> Alcotest.fail err

let test_parsing_ping_frame () =
let frame = parse_frame "\137\128\000\000\046\216" in
Alcotest.check Testable.opcode "opcode" `Ping (Frame.opcode frame);
Alcotest.(check bool) "has mask" true (Frame.has_mask frame);
Alcotest.(check int32) "mask" 11992l (Frame.mask_exn frame);
Alcotest.(check int) "payload_length" (Frame.payload_length frame) 0;
Alcotest.(check int) "length" (Frame.length frame) 6

let test_parsing_close_frame () =
let frame = parse_frame "\136\000" in
Alcotest.check Testable.opcode "opcode" `Connection_close (Frame.opcode frame);
Alcotest.(check int) "payload_length" (Frame.payload_length frame) 0;
Alcotest.(check int) "length" (Frame.length frame) 2

let test_parsing_text_frame () =
let frame = parse_frame "\129\139\086\057\046\216\103\011\029\236\099\015\025\224\111\009\036" in
Alcotest.check Testable.opcode "opcode" `Text (Frame.opcode frame);
Alcotest.(check bool) "has mask" true (Frame.has_mask frame);
Alcotest.(check int32) "mask" 1446588120l (Frame.mask_exn frame);
Alcotest.(check int) "payload_length" (Frame.payload_length frame) 11;
Alcotest.(check int) "length" (Frame.length frame) 17;
Frame.unmask frame;
let payload = Bytes.to_string (Frame.copy_payload_bytes frame) in
Alcotest.(check string) "payload" "1234567890\n" payload

let tests =
[ "parsing ping frame", `Quick, test_parsing_ping_frame
; "parsing close frame", `Quick, test_parsing_close_frame
; "parsing text frame", `Quick, test_parsing_text_frame
]
end

let () =
Alcotest.run "websocketaf unit tests"
[ "websocket", Websocket.tests
]

0 comments on commit 8382d0e

Please sign in to comment.