0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 12:20:24 +00:00

Fix eventsource example

This commit is contained in:
Loïc Hoguin 2016-06-14 17:05:41 +02:00
parent 84fb85e2e4
commit 5003bae369
3 changed files with 29 additions and 5 deletions

View file

@ -190,6 +190,29 @@ do_echo_post(Transport, Protocol, Config) ->
{ok, <<"this is fun">>} = gun:await_body(ConnPid, Ref),
ok.
%% Eventsource.
eventsource(Config) ->
doc("Eventsource example."),
try
do_compile_and_start(eventsource),
do_eventsource(tcp, http, Config),
do_eventsource(tcp, http2, Config)
after
do_stop(eventsource)
end.
do_eventsource(Transport, Protocol, Config) ->
ConnPid = gun_open([{port, 8080}, {type, Transport}, {protocol, Protocol}|Config]),
Ref = gun:get(ConnPid, "/eventsource"),
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
{_, <<"text/event-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
%% Receive a few events.
{data, nofin, << "id: ", _/bits >>} = gun:await(ConnPid, Ref, 2000),
{data, nofin, << "id: ", _/bits >>} = gun:await(ConnPid, Ref, 2000),
{data, nofin, << "id: ", _/bits >>} = gun:await(ConnPid, Ref, 2000),
gun:close(ConnPid).
%% REST Hello World.
rest_hello_world(Config) ->