Skip to content

Commit

Permalink
Implement no-op support for Plug.Conn.Adapter.upgrade/3
Browse files Browse the repository at this point in the history
  • Loading branch information
mtrudel committed Oct 4, 2022
1 parent f97c0b8 commit ba10747
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/bandit/http1/adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ defmodule Bandit.HTTP1.Adapter do
@impl Plug.Conn.Adapter
def inform(_req, _status, _headers), do: {:error, :not_supported}

@impl Plug.Conn.Adapter
def upgrade(_req, _upgrade, _opts), do: {:error, :not_supported}

@impl Plug.Conn.Adapter
def push(_req, _path, _headers), do: {:error, :not_supported}

Expand Down
3 changes: 3 additions & 0 deletions lib/bandit/http2/adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ defmodule Bandit.HTTP2.Adapter do
GenServer.call(adapter.connection, {:send_headers, adapter.stream_id, headers, false})
end

@impl Plug.Conn.Adapter
def upgrade(_req, _upgrade, _opts), do: {:error, :not_supported}

@impl Plug.Conn.Adapter
def push(_adapter, _path, _headers), do: {:error, :not_supported}

Expand Down
17 changes: 17 additions & 0 deletions test/bandit/http1/request_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,23 @@ defmodule HTTP1RequestTest do
end
end

describe "upgrade handling" do
test "does not update the conn or send any data on unsupported upgrades", context do
{:ok, response} =
Finch.build(:get, context[:base] <> "/upgrade_unsupported", [{"connection", "close"}])
|> Finch.request(context[:finch_name])

assert response.status == 200
assert response.body == "Not supported"
end

def upgrade_unsupported(conn) do
conn
|> upgrade_adapter(:unsupported, [])
|> send_resp(200, "Not supported")
end
end

test "does not do anything special with EXIT messages from abnormally terminating spwaned processes",
context do
errors =
Expand Down
17 changes: 17 additions & 0 deletions test/bandit/http2/plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,23 @@ defmodule HTTP2PlugTest do
|> elem(1)
end

describe "upgrade handling" do
test "does not update the conn or send any data on unsupported upgrades", context do
{:ok, response} =
Finch.build(:get, context[:base] <> "/upgrade_unsupported")
|> Finch.request(context[:finch_name])

assert response.status == 200
assert response.body == "Not supported"
end

def upgrade_unsupported(conn) do
conn
|> upgrade_adapter(:unsupported, [])
|> send_resp(200, "Not supported")
end
end

test "raises a Plug.Conn.NotSentError if nothing was set in the conn", context do
errors =
capture_log(fn ->
Expand Down

0 comments on commit ba10747

Please sign in to comment.