Skip to content

Commit

Permalink
add test cases for upgrade failures
Browse files Browse the repository at this point in the history
  • Loading branch information
the-mikedavis committed Feb 27, 2022
1 parent ca0148f commit 94401ba
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
14 changes: 14 additions & 0 deletions test/fixtures/forbidden_handler.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule ForbiddenHandler do
@moduledoc """
A Cowboy HTTP handler that serves a GET request in the test suite
and returns a 403 status code.
See https://http.cat/403 :)
"""

def init(req, state) do
req = :cowboy_req.reply(403, %{"content_type" => "text/plain"}, "Forbidden.", req)

{:ok, req, state}
end
end
3 changes: 2 additions & 1 deletion test/fixtures/test_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ defmodule TestServer do
{:_,
[
{'/', WebsocketHandler, []},
{'/http_get', HttpHandler, []}
{'/http_get', HttpHandler, []},
{'/forbidden', ForbiddenHandler, []}
]}
])

Expand Down
53 changes: 52 additions & 1 deletion test/mint/web_socket_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Mint.WebSocketTest do
use ExUnit.Case, async: true

alias Mint.{HTTP1, HTTP2, WebSocket}
alias Mint.{HTTP1, HTTP2, WebSocket, WebSocket.UpgradeFailureError}

setup_all do
# a cowboy test server used by the HTTP/2 tests
Expand Down Expand Up @@ -93,6 +93,33 @@ defmodule Mint.WebSocketTest do
end
end

describe "given a passive HTTP/1 connection to the local cowboy server" do
setup do
{:ok, conn} = HTTP1.connect(:http, "localhost", 7070, mode: :passive)
[conn: conn]
end

test "a response code other than 101 gives a UpgradeFailureError", %{conn: conn} do
{:ok, conn, ref} = WebSocket.upgrade(:ws, conn, "/forbidden", [])

{:ok, conn,
[
{:status, ^ref, status},
{:headers, ^ref, resp_headers},
{:data, ^ref, data},
{:done, ^ref}
]} = WebSocket.recv(conn, 0, 5_000)

assert status == 403
assert data == "Forbidden."

assert {:error, _conn, %UpgradeFailureError{} = reason} =
WebSocket.new(conn, ref, status, resp_headers, mode: :passive)

assert UpgradeFailureError.message(reason) =~ "status code 403"
end
end

describe "given an HTTP/2 WebSocket connection to an echo server" do
setup do
{:ok, conn} = HTTP2.connect(:http, "localhost", 7070)
Expand Down Expand Up @@ -144,6 +171,7 @@ defmodule Mint.WebSocketTest do
{:ok, _conn} = HTTP2.close(conn)
end

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

Expand Down Expand Up @@ -173,6 +201,29 @@ defmodule Mint.WebSocketTest do

{:ok, _conn} = HTTP2.close(conn)
end

@tag :http2
test "a response code outside the 200..299 range gives a UpgradeFailureError", %{conn: conn} do
{:ok, conn, ref} = WebSocket.upgrade(:ws, conn, "/forbidden", [])

assert_receive message

{:ok, conn,
[
{:status, ^ref, status},
{:headers, ^ref, resp_headers},
{:data, ^ref, data},
{:done, ^ref}
]} = WebSocket.stream(conn, message)

assert status == 403
assert data == "Forbidden."

assert {:error, _conn, %UpgradeFailureError{} = reason} =
WebSocket.new(conn, ref, status, resp_headers, mode: :passive)

assert UpgradeFailureError.message(reason) =~ "status code 403"
end
end

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

0 comments on commit 94401ba

Please sign in to comment.