Skip to content

Commit 94401ba

Browse files
committed
add test cases for upgrade failures
1 parent ca0148f commit 94401ba

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

test/fixtures/forbidden_handler.ex

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
defmodule ForbiddenHandler do
2+
@moduledoc """
3+
A Cowboy HTTP handler that serves a GET request in the test suite
4+
and returns a 403 status code.
5+
6+
See https://http.cat/403 :)
7+
"""
8+
9+
def init(req, state) do
10+
req = :cowboy_req.reply(403, %{"content_type" => "text/plain"}, "Forbidden.", req)
11+
12+
{:ok, req, state}
13+
end
14+
end

test/fixtures/test_server.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ defmodule TestServer do
1111
{:_,
1212
[
1313
{'/', WebsocketHandler, []},
14-
{'/http_get', HttpHandler, []}
14+
{'/http_get', HttpHandler, []},
15+
{'/forbidden', ForbiddenHandler, []}
1516
]}
1617
])
1718

test/mint/web_socket_test.exs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule Mint.WebSocketTest do
22
use ExUnit.Case, async: true
33

4-
alias Mint.{HTTP1, HTTP2, WebSocket}
4+
alias Mint.{HTTP1, HTTP2, WebSocket, WebSocket.UpgradeFailureError}
55

66
setup_all do
77
# a cowboy test server used by the HTTP/2 tests
@@ -93,6 +93,33 @@ defmodule Mint.WebSocketTest do
9393
end
9494
end
9595

96+
describe "given a passive HTTP/1 connection to the local cowboy server" do
97+
setup do
98+
{:ok, conn} = HTTP1.connect(:http, "localhost", 7070, mode: :passive)
99+
[conn: conn]
100+
end
101+
102+
test "a response code other than 101 gives a UpgradeFailureError", %{conn: conn} do
103+
{:ok, conn, ref} = WebSocket.upgrade(:ws, conn, "/forbidden", [])
104+
105+
{:ok, conn,
106+
[
107+
{:status, ^ref, status},
108+
{:headers, ^ref, resp_headers},
109+
{:data, ^ref, data},
110+
{:done, ^ref}
111+
]} = WebSocket.recv(conn, 0, 5_000)
112+
113+
assert status == 403
114+
assert data == "Forbidden."
115+
116+
assert {:error, _conn, %UpgradeFailureError{} = reason} =
117+
WebSocket.new(conn, ref, status, resp_headers, mode: :passive)
118+
119+
assert UpgradeFailureError.message(reason) =~ "status code 403"
120+
end
121+
end
122+
96123
describe "given an HTTP/2 WebSocket connection to an echo server" do
97124
setup do
98125
{:ok, conn} = HTTP2.connect(:http, "localhost", 7070)
@@ -144,6 +171,7 @@ defmodule Mint.WebSocketTest do
144171
{:ok, _conn} = HTTP2.close(conn)
145172
end
146173

174+
@tag :http2
147175
test "we can multiplex WebSocket and HTTP traffic", c do
148176
websocket_ref = c.ref
149177

@@ -173,6 +201,29 @@ defmodule Mint.WebSocketTest do
173201

174202
{:ok, _conn} = HTTP2.close(conn)
175203
end
204+
205+
@tag :http2
206+
test "a response code outside the 200..299 range gives a UpgradeFailureError", %{conn: conn} do
207+
{:ok, conn, ref} = WebSocket.upgrade(:ws, conn, "/forbidden", [])
208+
209+
assert_receive message
210+
211+
{:ok, conn,
212+
[
213+
{:status, ^ref, status},
214+
{:headers, ^ref, resp_headers},
215+
{:data, ^ref, data},
216+
{:done, ^ref}
217+
]} = WebSocket.stream(conn, message)
218+
219+
assert status == 403
220+
assert data == "Forbidden."
221+
222+
assert {:error, _conn, %UpgradeFailureError{} = reason} =
223+
WebSocket.new(conn, ref, status, resp_headers, mode: :passive)
224+
225+
assert UpgradeFailureError.message(reason) =~ "status code 403"
226+
end
176227
end
177228

178229
# cowboy's WebSocket is a little weird here, is it sending SETTINGS frames and then

0 commit comments

Comments
 (0)