aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--doc/src/manual/gun.await.asciidoc8
-rw-r--r--doc/src/manual/gun.await_body.asciidoc1
-rw-r--r--doc/src/manual/gun.await_up.asciidoc1
-rw-r--r--src/gun.erl7
-rw-r--r--test/ws_SUITE.erl19
5 files changed, 32 insertions, 4 deletions
diff --git a/doc/src/manual/gun.await.asciidoc b/doc/src/manual/gun.await.asciidoc
index 09c244b..0af6cee 100644
--- a/doc/src/manual/gun.await.asciidoc
+++ b/doc/src/manual/gun.await.asciidoc
@@ -69,6 +69,8 @@ Result :: {inform, Status, Headers}
| {data, IsFin, Data}
| {trailers, Trailers}
| {push, NewStreamRef, Method, URI, Headers}
+ | {upgrade, Protocols, Headers}
+ | {ws, Frame}
| {error, Reason}
Reason :: {stream_error | connection_error | down, any()}
@@ -79,14 +81,18 @@ Because the messages and returned tuples are equivalent,
please refer to the manual pages for each message for
further information:
-* link:man:gun_push(3)[gun_push(3)] - Server-initiated push
* link:man:gun_inform(3)[gun_inform(3)] - Informational response
* link:man:gun_response(3)[gun_response(3)] - Response
* link:man:gun_data(3)[gun_data(3)] - Response body
* link:man:gun_trailers(3)[gun_trailers(3)] - Response trailers
+* link:man:gun_push(3)[gun_push(3)] - Server-initiated push
+* link:man:gun_upgrade(3)[gun_upgrade(3)] - Successful protocol upgrade
+* link:man:gun_ws(3)[gun_ws(3)] - Websocket frame
== Changelog
+* *2.0*: `upgrade` and `ws` tuples can now be returned.
+* *2.0*: The error tuple type now includes the type of error.
* *1.0*: Function introduced.
== Examples
diff --git a/doc/src/manual/gun.await_body.asciidoc b/doc/src/manual/gun.await_body.asciidoc
index 0505112..c2c7133 100644
--- a/doc/src/manual/gun.await_body.asciidoc
+++ b/doc/src/manual/gun.await_body.asciidoc
@@ -61,6 +61,7 @@ may also be returned when a timeout or an error occur.
== Changelog
+* *2.0*: The error tuple type now includes the type of error.
* *1.0*: Function introduced.
== Examples
diff --git a/doc/src/manual/gun.await_up.asciidoc b/doc/src/manual/gun.await_up.asciidoc
index 2639943..dfd4da9 100644
--- a/doc/src/manual/gun.await_up.asciidoc
+++ b/doc/src/manual/gun.await_up.asciidoc
@@ -54,6 +54,7 @@ may also be returned when a timeout or an error occur.
== Changelog
+* *2.0*: The error tuple type now includes the type of error.
* *1.0*: Function introduced.
== Examples
diff --git a/src/gun.erl b/src/gun.erl
index b1f493b..7376a33 100644
--- a/src/gun.erl
+++ b/src/gun.erl
@@ -494,6 +494,8 @@ connect(ServerPid, Destination, Headers, ReqOpts) ->
| {data, fin | nofin, binary()}
| {trailers, resp_headers()}
| {push, reference(), binary(), binary(), resp_headers()}
+ | {upgrade, [binary()], resp_headers()}
+ | {ws, ws_frame()} %% @todo Excluding ping/pong, for now.
| {error, {stream_error | connection_error | down, any()} | timeout}.
-spec await(pid(), reference()) -> await_result().
@@ -512,7 +514,6 @@ await(ServerPid, StreamRef, Timeout) ->
demonitor(MRef, [flush]),
Res.
-%% @todo Add gun_upgrade and gun_ws?
-spec await(pid(), reference(), timeout(), reference()) -> await_result().
await(ServerPid, StreamRef, Timeout, MRef) ->
receive
@@ -526,6 +527,10 @@ await(ServerPid, StreamRef, Timeout, MRef) ->
{trailers, Trailers};
{gun_push, ServerPid, StreamRef, NewStreamRef, Method, URI, Headers} ->
{push, NewStreamRef, Method, URI, Headers};
+ {gun_upgrade, ServerPid, StreamRef, Protocols, Headers} ->
+ {upgrade, Protocols, Headers};
+ {gun_ws, ServerPid, StreamRef, Frame} ->
+ {ws, Frame};
{gun_error, ServerPid, StreamRef, Reason} ->
{error, {stream_error, Reason}};
{gun_error, ServerPid, Reason} ->
diff --git a/test/ws_SUITE.erl b/test/ws_SUITE.erl
index 8cb6bd1..1a48b83 100644
--- a/test/ws_SUITE.erl
+++ b/test/ws_SUITE.erl
@@ -28,8 +28,12 @@ groups() ->
[{ws, [], ct_helper:all(?MODULE)}].
init_per_suite(Config) ->
+ Routes = [
+ {"/", ws_echo, []},
+ {"/reject", ws_reject_h, []}
+ ],
{ok, _} = cowboy:start_clear(ws, [], #{env => #{
- dispatch => cowboy_router:compile([{'_', [{"/", ws_reject_h, []}]}])
+ dispatch => cowboy_router:compile([{'_', Routes}])
}}),
Port = ranch:get_port(ws),
[{port, Port}|Config].
@@ -39,11 +43,22 @@ end_per_suite(_) ->
%% Tests.
+await(Config) ->
+ doc("Ensure gun:await/2 can be used to receive Websocket frames."),
+ {ok, ConnPid} = gun:open("localhost", config(port, Config)),
+ {ok, _} = gun:await_up(ConnPid),
+ StreamRef = gun:ws_upgrade(ConnPid, "/", []),
+ {upgrade, [<<"websocket">>], _} = gun:await(ConnPid, StreamRef),
+ Frame = {text, <<"Hello!">>},
+ gun:ws_send(ConnPid, Frame),
+ {ws, Frame} = gun:await(ConnPid, StreamRef),
+ gun:close(ConnPid).
+
reject_upgrade(Config) ->
doc("Ensure Websocket connections can be rejected."),
{ok, ConnPid} = gun:open("localhost", config(port, Config)),
{ok, _} = gun:await_up(ConnPid),
- StreamRef = gun:ws_upgrade(ConnPid, "/", []),
+ StreamRef = gun:ws_upgrade(ConnPid, "/reject", []),
receive
{gun_response, ConnPid, StreamRef, nofin, 400, _} ->
{ok, <<"Upgrade rejected">>} = gun:await_body(ConnPid, StreamRef, 1000),