Created
July 23, 2024 10:17
-
-
Save jounimakela/075e9d4ce4baea8eec892f7d1d369cc5 to your computer and use it in GitHub Desktop.
`Phoenix.ConnTest.get/2` a path with only query parameters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Application.put_env(:sample, SamplePhoenix.Endpoint, | |
http: [ip: {0, 0, 0, 0}, port: 4000], | |
server: true, | |
secret_key_base: String.duplicate("a", 32) | |
) | |
Mix.install([ | |
{:plug_cowboy, "2.7.1"}, | |
{:phoenix, "1.7.14"}, | |
# {:phoenix, git: "https://github.com/phoenixframework/phoenix.git", branch: "main"} | |
]) | |
defmodule SamplePhoenix.SampleController do | |
use Phoenix.Controller | |
def index(conn, _) do | |
send_resp(conn, 200, "Main page") | |
end | |
def page(conn, _) do | |
send_resp(conn, 200, "Another page") | |
end | |
end | |
defmodule Router do | |
use Phoenix.Router | |
scope "/", SamplePhoenix do | |
get("/", SampleController, :index) | |
get("/page", SampleController, :page) | |
# Prevent a horrible error because ErrorView is missing | |
get("/favicon.ico", SampleController, :index) | |
end | |
end | |
defmodule SamplePhoenix.Endpoint do | |
use Phoenix.Endpoint, otp_app: :sample | |
plug(Router) | |
end | |
# Uncomment to test in a browser | |
# | |
# {:ok, _} = Supervisor.start_link([SamplePhoenix.Endpoint], strategy: :one_for_one) | |
# Process.sleep(:infinity) | |
Logger.configure(level: :debug) | |
ExUnit.start() | |
defmodule SampleControllerTest do | |
use ExUnit.Case | |
import Phoenix.ConnTest | |
@endpoint SamplePhoenix.Endpoint | |
setup do | |
start_supervised!(SamplePhoenix.Endpoint) | |
:ok | |
end | |
test "it works" do | |
conn = get(build_conn(), "/page") | |
assert response(conn, 200) =~ "Another page" | |
assert conn.request_path == "/page" | |
# Since conn.request_path is "/page", we expect "?lang=en" to direct to | |
# "/page?lang=en" | |
conn = get(conn, "?lang=en") | |
assert response(conn, 200) =~ "Another page" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment