Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Left trim optional spaces in event field values #182

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/shotgun.erl
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,15 @@ parse_event(EventBin) ->
FoldFun =
fun(Line, #{data := Data} = Event) ->
case Line of
<<"data: ", NewData/binary>> ->
Event#{data => <<Data/binary, NewData/binary, "\n">>};
<<"id: ", Id/binary>> ->
Event#{id => Id};
<<"event: ", EventName/binary>> ->
Event#{event => EventName};
<<"data:", NewData/binary>> ->
TrimmedNewData = binary_ltrim(NewData),
Event#{
data => <<Data/binary, TrimmedNewData/binary, "\n">>
};
<<"id:", Id/binary>> ->
Event#{id => binary_ltrim(Id)};
<<"event:", EventName/binary>> ->
Event#{event => binary_ltrim(EventName)};
<<_Comment/binary>> ->
Event
end
Expand Down Expand Up @@ -785,3 +788,10 @@ get_work(State) ->
append_work(Work, State) ->
#{pending_requests := PendingReqs} = State,
State#{pending_requests := queue:in(Work, PendingReqs)}.

%% @private
-spec binary_ltrim(binary()) -> binary().
binary_ltrim(<<32, Bin/binary>>) ->
binary_ltrim(Bin);
binary_ltrim(Bin) ->
Bin.
98 changes: 98 additions & 0 deletions test/shotgun_unit_SUITE.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
-module(shotgun_unit_SUITE).

-export([ all/0
, init_per_suite/1
, end_per_suite/1
, init_per_testcase/2
, end_per_testcase/2
]).

-export([ parse_event/1
, parse_event_optional_spaces/1
, parse_event_extra_fields/1
, parse_event_mixed_order/1
, parse_event_minimal/1
, parse_event_empty/1
, parse_event_multiline_data/1
]).

-include_lib("common_test/include/ct.hrl").

%%------------------------------------------------------------------------------
%% Common Test
%%------------------------------------------------------------------------------

-spec all() -> [atom()].
all() -> shotgun_test_utils:all(?MODULE).

-spec init_per_suite(shotgun_test_utils:config()) ->
shotgun_test_utils:config().
init_per_suite(Config) ->
Config.

-spec end_per_suite(shotgun_test_utils:config()) -> shotgun_test_utils:config().
end_per_suite(Config) ->
Config.

-spec init_per_testcase(atom(), shotgun_test_utils:config()) ->
shotgun_test_utils:config().
init_per_testcase(_, Config) ->
Config.

-spec end_per_testcase(atom(), shotgun_test_utils:config()) ->
shotgun_test_utils:config().
end_per_testcase(_, Config) ->
Config.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are all optional and they're not doing anything at all in this case. I would recommend just removing them.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right of course, removed in 3a243ff. Thanks.


%%------------------------------------------------------------------------------
%% Test Cases
%%------------------------------------------------------------------------------

-spec parse_event(shotgun_test_utils:config()) -> {comment, string()}.
parse_event(_Config) ->
EventBin = <<"data:foo\nid:bar\nevent:baz">>,
Expected = #{id => <<"bar">>, event => <<"baz">>, data => <<"foo\n">>},
Expected = shotgun:parse_event(EventBin),
{comment, ""}.

-spec parse_event_optional_spaces(shotgun_test_utils:config()) -> {comment, string()}.
parse_event_optional_spaces(_Config) ->
EventBin = <<"data: foo\nid: bar\nevent: baz">>,
Expected = #{id => <<"bar">>, event => <<"baz">>, data => <<"foo\n">>},
Expected = shotgun:parse_event(EventBin),
{comment, ""}.

-spec parse_event_extra_fields(shotgun_test_utils:config()) -> {comment, string()}.
parse_event_extra_fields(_Config) ->
EventBin = <<"data:foo\nid:bar\nevent:baz\nextra:should-be-dropped">>,
Expected = #{id => <<"bar">>, event => <<"baz">>, data => <<"foo\n">>},
Expected = shotgun:parse_event(EventBin),
{comment, ""}.

-spec parse_event_mixed_order(shotgun_test_utils:config()) -> {comment, string()}.
parse_event_mixed_order(_Config) ->
EventBin = <<"id:bar\nextra:should-be-dropped\ndata:foo\nevent:baz">>,
Expected = #{id => <<"bar">>, event => <<"baz">>, data => <<"foo\n">>},
Expected = shotgun:parse_event(EventBin),
{comment, ""}.

-spec parse_event_minimal(shotgun_test_utils:config()) -> {comment, string()}.
parse_event_minimal(_Config) ->
EventBin = <<"id:bar">>,
Expected = #{id => <<"bar">>, data => <<>>},
Expected = shotgun:parse_event(EventBin),
{comment, ""}.

-spec parse_event_empty(shotgun_test_utils:config()) -> {comment, string()}.
parse_event_empty(_Config) ->
EventBin = <<"">>,
Expected = #{data => <<>>},
Expected = shotgun:parse_event(EventBin),
{comment, ""}.

-spec parse_event_multiline_data(shotgun_test_utils:config()) -> {comment, string()}.
parse_event_multiline_data(_Config) ->
EventBin = <<"data:foo1\ndata:foo2\ndata:foo3\nid:bar\nevent:baz">>,
Expected = #{id => <<"bar">>, event => <<"baz">>, data => <<"foo1\nfoo2\nfoo3\n">>},
Expected = shotgun:parse_event(EventBin),
{comment, ""}.