Skip to content

Commit

Permalink
Add decoding for close messages
Browse files Browse the repository at this point in the history
  • Loading branch information
madtrick committed Apr 9, 2012
1 parent 5d1cb50 commit 40afd1e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/wsecli_message.erl
Original file line number Diff line number Diff line change
Expand Up @@ -128,21 +128,18 @@ build_message(Message, Frames) ->
Message#message{type = pong, payload = Payload}
end.

build_payload_from_frames(close, [Frame]) ->
case Frame#frame.payload of
<<>> -> {undefined, undefined};
<<Status:16, Reason/binary>> -> {Status, binary_to_list(Reason)}
end;

build_payload_from_frames(binary, Frames) ->
concatenate_payload_from_frames(Frames);

build_payload_from_frames(text, Frames) ->
Payload = concatenate_payload_from_frames(Frames),
binary_to_list(Payload);

build_payload_from_frames(close, Frames) ->
Payload = concatenate_payload_from_frames(Frames),
case Payload of
<<>> ->
undefined;
<<StatusCode:16, Body/binary>> ->
{StatusCode, Body}
end.
binary_to_list(Payload).

concatenate_payload_from_frames(Frames) ->
concatenate_payload_from_frames(Frames, <<>>).
Expand Down
35 changes: 35 additions & 0 deletions test/spec/wsecli_message_spec.erl
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,41 @@ spec() ->

assert_that(Message#message.type, is(pong))
end)
end),
describe("close", fun() ->
it("should return a message with type close", fun() ->
FakeMessage = get_binary_frame(1, 0, 0, 0, 8, 0, 0, 0, <<>>),

[Message] = wsecli_message:decode(FakeMessage),

assert_that(Message#message.type, is(close))
end),
describe("with payload", fun() ->
it("should return the payload a a tuple {Status, Reason}", fun()->
Status = 1004,
Reason = list_to_binary("A tomar por saco"),
Payload = <<Status:16, Reason/binary>>,
PayloadLen = byte_size(Payload),
FakeMessage = get_binary_frame(1, 0, 0, 0, 8, 0, PayloadLen, 0, Payload),

[Message] = wsecli_message:decode(FakeMessage),
{St, Re} = Message#message.payload,

assert_that(St, is(Status)),
assert_that(Re, is("A tomar por saco"))
end)
end),
describe("without payload", fun() ->
it("should return the payload as a tuple {undefined, undefined}", fun() ->
FakeMessage = get_binary_frame(1, 0, 0, 0, 8, 0, 0, 0, <<>>),

[Message] = wsecli_message:decode(FakeMessage),
{Status, Reason} = Message#message.payload,

assert_that(Status, is(undefined)),
assert_that(Reason, is(undefined))
end)
end)
end)
end)
end)
Expand Down

0 comments on commit 40afd1e

Please sign in to comment.