Skip to content

Commit

Permalink
test: improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
c4710n committed Dec 24, 2023
1 parent 991d96b commit ab68a1a
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions test/plug_probe_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,29 @@ defmodule PlugProbeTest do

defmodule DefaultPipeline do
use Plug.Router

plug PlugProbe
plug :match
plug :dispatch
match _, do: send_resp(conn, 200, "match")
match _, do: send_resp(conn, 200, "end-of-pipeline")
end

defmodule CustomPathPipeline do
use Plug.Router
plug PlugProbe, path: "/custom-probe"
plug :match
plug :dispatch
match _, do: send_resp(conn, 200, "match")
end

defmodule HaltedPipeline do
use Plug.Router
plug PlugProbe
plug PlugProbe, path: "/custom-probe"
plug :match
plug :dispatch
plug :body_after

defp body_after(conn, _opts), do: %{conn | resp_body: "after"}

match _, do: send_resp(conn, 200, "match")
match _, do: send_resp(conn, 200, "end-of-pipeline")
end

defmodule JsonPipeline do
use Plug.Router

plug PlugProbe, json: true
plug :match
plug :dispatch
match _, do: send_resp(conn, 200, "match")
match _, do: send_resp(conn, 200, "end-of-pipeline")
end

describe "check requests with method:" do
Expand All @@ -54,7 +45,7 @@ defmodule PlugProbeTest do
test "only GET and HEAD requests work" do
Enum.each([:post, :put, :delete, :options, :foo], fn method ->
conn = conn(method, "/probe") |> DefaultPipeline.call([])
assert conn.resp_body == "match"
assert conn.resp_body == "end-of-pipeline"
end)
end
end
Expand Down Expand Up @@ -89,19 +80,26 @@ defmodule PlugProbeTest do

describe "check halted requests:" do
test "the request is halted after matching" do
conn = conn(:get, "/probe") |> HaltedPipeline.call([])
conn = conn(:get, "/probe") |> DefaultPipeline.call([])
assert conn.status == 200
assert conn.resp_body == "OK"
end

test "only matching requests are halted" do
conn = conn(:get, "/passthrough") |> DefaultPipeline.call([])
assert conn.status == 200
assert conn.resp_body == "match"
assert conn.resp_body == "end-of-pipeline"

conn = conn(:get, "/passthrough") |> CustomPathPipeline.call([])
assert conn.status == 200
assert conn.resp_body == "match"
assert conn.resp_body == "end-of-pipeline"
end
end

test "forwarded requests is supported" do
conn = %{path_info: ["upstream" | rest]} = conn(:get, "/upstream/probe")
conn = Plug.forward(conn, rest, DefaultPipeline, [])
assert conn.status == 200
assert conn.resp_body == "OK"
end
end

0 comments on commit ab68a1a

Please sign in to comment.