Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/plug/conn.ex
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ defmodule Plug.Conn do
alias Plug.Conn
@epoch {{1970, 1, 1}, {0, 0, 0}}
@already_sent {:plug_conn, :sent}
@unsent [:unset, :set, :set_chunked, :set_file]
@unsent [:unset, :set, :set_upgrade, :set_chunked, :set_file]

@doc """
Assigns a value to a key in the connection.
Expand Down Expand Up @@ -1471,10 +1471,11 @@ defmodule Plug.Conn do
@spec upgrade_adapter(t, atom, term) :: t
def upgrade_adapter(%Conn{adapter: {adapter, payload}, state: state} = conn, protocol, args)
when state in @unsent do
conn = run_before_send(%{conn | status: 101}, :set_upgrade)

case adapter.upgrade(payload, protocol, args) do
{:ok, payload} ->
conn = run_before_send(conn, :upgraded)
%{conn | adapter: {adapter, payload}}
%{conn | state: :upgraded, adapter: {adapter, payload}}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should set status: 101 here for phoenixframework/phoenix#6741.


{:error, :not_supported} ->
raise ArgumentError, "upgrade to #{protocol} not supported by #{inspect(adapter)}"
Expand Down
12 changes: 11 additions & 1 deletion test/plug/conn_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -510,9 +510,19 @@ defmodule Plug.ConnTest do
test "upgrade_adapter/3 runs before_send callbacks" do
conn =
conn(:get, "/foo")
|> register_before_send(&assign(&1, :ran_before_send, true))
|> register_before_send(fn conn ->
send(self(), {:state, conn.state})
assert conn.status == 101

conn
|> put_resp_header("x-test", "UPGRADE")
|> assign(:ran_before_send, true)
end)
|> upgrade_adapter(:supported, opt: :supported)

assert_receive {:state, :set_upgrade}

assert {"x-test", "UPGRADE"} in conn.resp_headers
assert conn.assigns[:ran_before_send] == true
end

Expand Down