mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 12:20:24 +00:00
Add test cases for cowboy_req:stream_events
This commit is contained in:
parent
faefb634de
commit
0fb68ec07f
2 changed files with 101 additions and 0 deletions
|
@ -246,6 +246,58 @@ do(<<"stream_body_content_length">>, Req0, Opts) ->
|
||||||
cowboy_req:stream_body(<<"Hello">>, nofin, Req),
|
cowboy_req:stream_body(<<"Hello">>, nofin, Req),
|
||||||
{ok, Req, Opts}
|
{ok, Req, Opts}
|
||||||
end;
|
end;
|
||||||
|
do(<<"stream_events">>, Req0, Opts) ->
|
||||||
|
case cowboy_req:binding(arg, Req0) of
|
||||||
|
%%<<"single">>
|
||||||
|
%%<<"list">>
|
||||||
|
<<"single">> ->
|
||||||
|
Req = cowboy_req:stream_reply(200,
|
||||||
|
#{<<"content-type">> => <<"text/event-stream">>},
|
||||||
|
Req0),
|
||||||
|
cowboy_req:stream_events(#{
|
||||||
|
event => <<"add_comment">>,
|
||||||
|
data => <<"Comment text.\nWith many lines.">>
|
||||||
|
}, fin, Req),
|
||||||
|
{ok, Req, Opts};
|
||||||
|
<<"list">> ->
|
||||||
|
Req = cowboy_req:stream_reply(200,
|
||||||
|
#{<<"content-type">> => <<"text/event-stream">>},
|
||||||
|
Req0),
|
||||||
|
cowboy_req:stream_events([
|
||||||
|
#{
|
||||||
|
event => <<"add_comment">>,
|
||||||
|
data => <<"Comment text.\nWith many lines.">>
|
||||||
|
},
|
||||||
|
#{
|
||||||
|
comment => <<"Set retry higher\nwith many lines also.">>,
|
||||||
|
retry => 10000
|
||||||
|
},
|
||||||
|
#{
|
||||||
|
id => <<"123">>,
|
||||||
|
event => <<"add_comment">>,
|
||||||
|
data => <<"Closing!">>
|
||||||
|
}
|
||||||
|
], fin, Req),
|
||||||
|
{ok, Req, Opts};
|
||||||
|
<<"multiple">> ->
|
||||||
|
Req = cowboy_req:stream_reply(200,
|
||||||
|
#{<<"content-type">> => <<"text/event-stream">>},
|
||||||
|
Req0),
|
||||||
|
cowboy_req:stream_events(#{
|
||||||
|
event => <<"add_comment">>,
|
||||||
|
data => <<"Comment text.\nWith many lines.">>
|
||||||
|
}, nofin, Req),
|
||||||
|
cowboy_req:stream_events(#{
|
||||||
|
comment => <<"Set retry higher\nwith many lines also.">>,
|
||||||
|
retry => 10000
|
||||||
|
}, nofin, Req),
|
||||||
|
cowboy_req:stream_events(#{
|
||||||
|
id => <<"123">>,
|
||||||
|
event => <<"add_comment">>,
|
||||||
|
data => <<"Closing!">>
|
||||||
|
}, fin, Req),
|
||||||
|
{ok, Req, Opts}
|
||||||
|
end;
|
||||||
do(<<"stream_trailers">>, Req0, Opts) ->
|
do(<<"stream_trailers">>, Req0, Opts) ->
|
||||||
case cowboy_req:binding(arg, Req0) of
|
case cowboy_req:binding(arg, Req0) of
|
||||||
<<"large">> ->
|
<<"large">> ->
|
||||||
|
|
|
@ -909,6 +909,55 @@ stream_body_content_length_nofin_error(Config) ->
|
||||||
%% @todo Crash when calling stream_body after calling reply.
|
%% @todo Crash when calling stream_body after calling reply.
|
||||||
%% @todo Crash when calling stream_body before calling stream_reply.
|
%% @todo Crash when calling stream_body before calling stream_reply.
|
||||||
|
|
||||||
|
stream_events_single(Config) ->
|
||||||
|
doc("Streamed event."),
|
||||||
|
{200, Headers, <<
|
||||||
|
"event: add_comment\n"
|
||||||
|
"data: Comment text.\n"
|
||||||
|
"data: With many lines.\n"
|
||||||
|
"\n"
|
||||||
|
>>} = do_get("/resp/stream_events/single", Config),
|
||||||
|
{_, <<"text/event-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
|
||||||
|
ok.
|
||||||
|
|
||||||
|
stream_events_list(Config) ->
|
||||||
|
doc("Streamed list of events."),
|
||||||
|
{200, Headers, <<
|
||||||
|
"event: add_comment\n"
|
||||||
|
"data: Comment text.\n"
|
||||||
|
"data: With many lines.\n"
|
||||||
|
"\n"
|
||||||
|
": Set retry higher\n"
|
||||||
|
": with many lines also.\n"
|
||||||
|
"retry: 10000\n"
|
||||||
|
"\n"
|
||||||
|
"id: 123\n"
|
||||||
|
"event: add_comment\n"
|
||||||
|
"data: Closing!\n"
|
||||||
|
"\n"
|
||||||
|
>>} = do_get("/resp/stream_events/list", Config),
|
||||||
|
{_, <<"text/event-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
|
||||||
|
ok.
|
||||||
|
|
||||||
|
stream_events_multiple(Config) ->
|
||||||
|
doc("Streamed events via multiple calls."),
|
||||||
|
{200, Headers, <<
|
||||||
|
"event: add_comment\n"
|
||||||
|
"data: Comment text.\n"
|
||||||
|
"data: With many lines.\n"
|
||||||
|
"\n"
|
||||||
|
": Set retry higher\n"
|
||||||
|
": with many lines also.\n"
|
||||||
|
"retry: 10000\n"
|
||||||
|
"\n"
|
||||||
|
"id: 123\n"
|
||||||
|
"event: add_comment\n"
|
||||||
|
"data: Closing!\n"
|
||||||
|
"\n"
|
||||||
|
>>} = do_get("/resp/stream_events/multiple", Config),
|
||||||
|
{_, <<"text/event-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
|
||||||
|
ok.
|
||||||
|
|
||||||
stream_trailers(Config) ->
|
stream_trailers(Config) ->
|
||||||
doc("Stream body followed by trailer headers."),
|
doc("Stream body followed by trailer headers."),
|
||||||
{200, RespHeaders, <<"Hello world!">>, [
|
{200, RespHeaders, <<"Hello world!">>, [
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue