I’m trying to use https://hexdocs.pm/plug/Plug.Test.html to write a test for a very simple Plug, that has to access a query parameters.
The plug can be reduced to this:
defmodule BarPlug do
def call(conn, _) do
conn_with_params = Conn.fetch_query_params(conn)
bar = conn_with_params.query_params["bar"]
Conn.assign(conn, :bar, bar)
end
end
And, naively following the doc, I’m trying to test it with:
defmodule BarPlug.Test do
use ExUnit.Case, async: true
use Plug.Test
test "Reads query params" do
conn = :get
|> conn("foo", "bar=10")
|> BarPlug.call([])
assert conn.assigns.bar == "10"
end
This fails, and Inspecting the conn, before or after the fetch_query_params
calls, the Conn
is the same:
%Plug.Conn{
adapter: {Plug.Adapters.Test.Conn, :...},
assigns: %{},
before_send: [],
body_params: %Plug.Conn.Unfetched{aspect: :body_params},
cookies: %Plug.Conn.Unfetched{aspect: :cookies},
halted: false,
host: "www.example.com",
method: "GET",
owner: #PID<0.1089.0>,
params: %{},
path_info: ["foo"],
path_params: %{},
peer: {{127, 0, 0, 1}, 111317},
port: 80,
private: %{},
query_params: %{},
query_string: "",
remote_ip: {127, 0, 0, 1},
req_cookies: %Plug.Conn.Unfetched{aspect: :cookies},
req_headers: [],
request_path: "foo",
resp_body: nil,
resp_cookies: %{},
resp_headers: [{"cache-control", "max-age=0, private, must-revalidate"}],
scheme: :http,
script_name: [],
secret_key_base: nil,
state: :unset,
status: nil
}
Now, using a full URL works, but it contradicts the doc:
test "Reads query params" do
conn = :get
|> conn("/foo?bar=10")
|> Locale.Plug.call([])
assert conn.assigns.bar == "10"
end
Before assuming that the doc is wrong somehow, am I missing something obvious here ?