|
1 | 1 | defmodule Mint.WebSocketTest do |
2 | 2 | use ExUnit.Case, async: true |
3 | 3 |
|
4 | | - alias Mint.{HTTP1, HTTP2, WebSocket} |
| 4 | + alias Mint.{HTTP1, HTTP2, WebSocket, WebSocket.UpgradeFailureError} |
5 | 5 |
|
6 | 6 | setup_all do |
7 | 7 | # a cowboy test server used by the HTTP/2 tests |
@@ -93,6 +93,33 @@ defmodule Mint.WebSocketTest do |
93 | 93 | end |
94 | 94 | end |
95 | 95 |
|
| 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 | + |
96 | 123 | describe "given an HTTP/2 WebSocket connection to an echo server" do |
97 | 124 | setup do |
98 | 125 | {:ok, conn} = HTTP2.connect(:http, "localhost", 7070) |
@@ -144,6 +171,7 @@ defmodule Mint.WebSocketTest do |
144 | 171 | {:ok, _conn} = HTTP2.close(conn) |
145 | 172 | end |
146 | 173 |
|
| 174 | + @tag :http2 |
147 | 175 | test "we can multiplex WebSocket and HTTP traffic", c do |
148 | 176 | websocket_ref = c.ref |
149 | 177 |
|
@@ -173,6 +201,29 @@ defmodule Mint.WebSocketTest do |
173 | 201 |
|
174 | 202 | {:ok, _conn} = HTTP2.close(conn) |
175 | 203 | 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 |
176 | 227 | end |
177 | 228 |
|
178 | 229 | # cowboy's WebSocket is a little weird here, is it sending SETTINGS frames and then |
|
0 commit comments