From ab68a1aae19f43a0c4dd8cd6dff70bb3def6aefb Mon Sep 17 00:00:00 2001 From: c4710n Date: Sun, 24 Dec 2023 14:49:09 +0800 Subject: [PATCH] test: improve tests --- test/plug_probe_test.exs | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/test/plug_probe_test.exs b/test/plug_probe_test.exs index e1aecbe..040d075 100644 --- a/test/plug_probe_test.exs +++ b/test/plug_probe_test.exs @@ -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 @@ -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 @@ -89,7 +80,7 @@ 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 @@ -97,11 +88,18 @@ defmodule PlugProbeTest do 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