Skip to content

Commit

Permalink
Merge pull request #40 from keathley/raise-exception-if-using-relativ…
Browse files Browse the repository at this point in the history
…e-route-and-no-base-url

Raise exception is there is no base_url set
  • Loading branch information
keathley committed Apr 24, 2016
2 parents 5e3efd7 + f63dcd9 commit def5b14
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
18 changes: 18 additions & 0 deletions lib/wallaby/exceptions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,21 @@ end
defmodule Wallaby.BadMetadata do
defexception [:message]
end

defmodule Wallaby.NoBaseUrl do
defexception [:message]

def exception(relative_path) do
msg = """
You called visit with #{relative_path}, but did not set a base_url.
Set this in config/test.exs or in test/test_helper.exs:
Application.put_env(:wallaby, :base_url, "http://localhost:4001")
If using Phoenix, you can use the url from your endpoint:
Application.put_env(:wallaby, :base_url, YourApplication.Endpoint.url)
"""
%__MODULE__{message: msg}
end
end
23 changes: 19 additions & 4 deletions lib/wallaby/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,23 @@ defmodule Wallaby.Session do

@doc """
Changes the current page to the provided route.
Routes are relative to any base url provided.
Relative paths are appended to the provided base_url.
Absolute paths do not use the base_url.
"""
@spec visit(t, String.t) :: t

def visit(session, path) do
Driver.visit(session, request_url(path))
uri = URI.parse(path)

cond do
uri.host == nil && String.length(base_url) == 0 ->
raise Wallaby.NoBaseUrl, path
uri.host ->
Driver.visit(session, path)
true ->
Driver.visit(session, request_url(path))
end

session
end

Expand Down Expand Up @@ -142,7 +153,11 @@ defmodule Wallaby.Session do
session
end

defp request_url(url) do
(Application.get_env(:wallaby, :base_url) || "") <> url
def request_url(path) do
base_url <> path
end

defp base_url do
Application.get_env(:wallaby, :base_url) || ""
end
end
15 changes: 15 additions & 0 deletions test/wallaby/session_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ defmodule Wallaby.SessionTest do
Application.put_env(:wallaby, :base_url, nil)
end

test "visit/2 with a relative url and no base url raises exception", %{session: session, server: server} do
assert_raise(Wallaby.NoBaseUrl, fn ->
Application.put_env(:wallaby, :base_url, nil)
session
|> visit("/page_1.html")
end)
end

test "visit/2 with an absolute path does not use the base url", %{session: session, server: server} do
session
|> visit(server.base_url <> "/page_1.html")

assert has_css?(session, "#visible")
end

test "taking screenshots", %{session: session, server: server} do
node =
session
Expand Down

0 comments on commit def5b14

Please sign in to comment.