Skip to content

Commit

Permalink
Ping like a pro
Browse files Browse the repository at this point in the history
  • Loading branch information
madtrick committed Apr 5, 2012
1 parent dd9bc6c commit 5446934
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/wsecli_message.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ decode(Data, Message) ->
% Internal
%
-spec encode(Data::binary(), Type :: atom(), Acc ::list()) -> list().
encode(Data, ping, _Acc) ->
[frame(Data, [fin, {opcode, ping}])];

encode(<<Data:?FRAGMENT_SIZE/binary>>, Type, Acc) ->
[frame(Data, [fin, {opcode, Type}]) | Acc];

Expand Down
26 changes: 26 additions & 0 deletions test/spec/wsecli_message_spec.erl
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,32 @@ spec() ->
assert_that(Opcode2, is(0)),
assert_that(Opcode3, is(0))
end)
end),
describe("control messages", fun() ->
describe("ping", fun() ->
it("should return a list of one frame", fun() ->
[_Frame] = wsecli_message:encode([], ping)
end),
it("should return a ping frame", fun() ->
[Frame] = wsecli_message:encode([], ping),

<<Fin:1, Rsv:3, Opcode:4, _/binary>> = Frame,

assert_that(Fin, is(1)),
assert_that(Rsv, is(0)),
assert_that(Opcode, is(9))
end),
it("should attach application payload", fun() ->
[Frame] = wsecli_message:encode("1234", ping),

<<_Fin:1, _Rsv:3, _Opcode:4, 1:1, 4:7, _Mask:32, _Payload:4/binary>> = Frame
end),
it("should not fragment", fun() ->
[Frame] = wsecli_message:encode(crypto:rand_bytes(5000), ping),

<<1:1, 0:3, 9:4, 1:1, 126:7, 5000:16, _/binary>> = Frame
end)
end)
end)
end),
it("wsecli_message:control"),
Expand Down

0 comments on commit 5446934

Please sign in to comment.