GUIDES
GUIDESDOCS

If you only have one maru server in your project, maru can find the root automatic, you can use Maru.Test like this:

defmodule API do
  use Maru.Router

  get do
    text(conn, "hello")
  end
end

defmodule APITest do
  use ExUnit.Case
  use Maru.Test

  test "/" do
    assert "hello" = get("/") |> text_response
  end
end

You should appoint the root router module if you have more than one, for example:

# when you have such lines in your config file
# config, :maru, API1, http: [port: 8080]
# config, :maru, API2, http: [port: 8081]

defmodule API1 do
  use Maru.Router

  get do
    text(conn, "hello1")
  end
end

defmodule API1Test do
  use ExUnit.Case
  use Maru.Test, root: API1

  test "/" do
    assert "hello1" = get("/") |> text_response
  end
end

defmodule API2 do
  use Maru.Router, root: API2

  get do
    text(conn, "hello2")
  end
end

defmodule API2Test do
  use ExUnit.Case
  use Maru.Test, root: API2

  test "/" do
    assert "hello2" = get("/") |> text_response
  end
end

If a router is mounted to another one, all plugs in top router are effective for router.

defmodule Middleware do
  def init([]), do: []
  def call(conn, []) do
    conn |> Plug.Conn.put_private(:maru_plug_test, true)
  end
end

defmodule API do
  use Maru.Router
  plug Middleware
  
  prefix :api
  mount API.Mounted
end

defmodule API.Mounted do
  use Maru.Router

  get :test do
    text(conn, "hello")
  end
end

# ---- TEST CODE ----
defmodule API.MountedTest do
  use ExUnit.Case
  use Maru.Test
  
  test "/api/test" do
    private = get("/api/test").private
    assert private.maru_plug_test
  end
end

If your router include multiple versions, here is an example:

# config :maru, API, [versioning: [using: :param, parameter: "v"]]

defmodule API do
  use Maru.Router

  version "v1" do
    get do
      text(conn, "hello")
    end	
  end
  
  version "v2" do
    get do
      json(conn, %{hello: :world}
    end	
  end
end

# ---- TEST CODE ----
defmodule APITest do
  use ExUnit.Case
  use Maru.Test

  test "/ v1" do
    assert "hello" = get("/?v=v1") |> text_response
  end

  test "/ v2" do
    assert %{"hello" => "world"} = get("/?v=v2") |> json_response
  end
end