Skip to content

Commit

Permalink
Allow Appsignal.Plug.call/2 to be overridden
Browse files Browse the repository at this point in the history
Closes #463. The `defoverridable` call was dropped in
a7b434c when splitting the Plug
integration from the Phoenix integration.
  • Loading branch information
jeffkreeftmeijer committed Mar 14, 2019
1 parent ce7497d commit d0a6e22
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
2 changes: 2 additions & 0 deletions lib/appsignal/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ if Appsignal.plug?() do
conn -> Appsignal.Plug.finish_with_conn(transaction, conn)
end
end

defoverridable call: 2
end
end

Expand Down
47 changes: 42 additions & 5 deletions test/appsignal/plug_test.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
defmodule ModuleWithCall do
defmacro __using__(_) do
quote do
def call(%Plug.Conn{} = conn, _opts) do
Plug.Conn.assign(conn, :called?, true)
end

defoverridable call: 2
end
end
end

defmodule UsingAppsignalPlug do
def call(%Plug.Conn{private: %{phoenix_action: :exception}}, _opts) do
raise("Exception!")
Expand Down Expand Up @@ -29,13 +41,18 @@ defmodule UsingAppsignalPlug do
}
end

def call(%Plug.Conn{} = conn, _opts) do
conn |> Plug.Conn.assign(:called?, true)
end

defoverridable call: 2
use ModuleWithCall
use Appsignal.Plug
end

defmodule OverridingAppSignalPlug do
use ModuleWithCall
use Appsignal.Plug

def call(conn, opts) do
conn = super(conn, opts)
Plug.Conn.assign(conn, :overridden?, true)
end
end

defmodule Appsignal.PlugTest do
Expand Down Expand Up @@ -493,4 +510,24 @@ defmodule Appsignal.PlugTest do
end
end
end

describe "when overriding the AppSignal Plug" do
setup do
conn = OverridingAppSignalPlug.call(%Plug.Conn{}, %{})

[conn: conn]
end

test "starts a transaction", %{fake_transaction: fake_transaction} do
assert FakeTransaction.started_transaction?(fake_transaction)
end

test "calls super and returns the conn", %{conn: conn} do
assert conn.assigns[:called?]
end

test "calls the overridden call/2", %{conn: conn} do
assert conn.assigns[:overridden?]
end
end
end

0 comments on commit d0a6e22

Please sign in to comment.