Skip to content

Instantly share code, notes, and snippets.

@RadekMolenda
Created February 26, 2016 20:11
Show Gist options
  • Save RadekMolenda/169981d4954cefcd0b6c to your computer and use it in GitHub Desktop.
Save RadekMolenda/169981d4954cefcd0b6c to your computer and use it in GitHub Desktop.
defmodule BasicAuthExercise.PageControllerTest do
use BasicAuthExercise.ConnCase
def with_valid_authorization_header(conn) do
conn
|> put_req_header("authorization", "Basic dXNlcjpzZWNyZXQ=")
end
def with_invalid_authorization_header(conn) do
conn
|> put_req_header("authorization", "Basic I like turtles")
end
test "GET / without authorization header should throw 401", %{conn: conn} do
conn = get conn, "/"
assert response(conn, 401) == "unauthorized"
assert get_resp_header(conn, "www-authenticate") == ["Basic realm=\"Thou Shalt not pass\""]
end
test "GET / with correct authorization should be OK", %{conn: conn} do
conn = conn
|> with_valid_authorization_header()
|> get("/")
assert html_response(conn, 200) =~ "Welcome to Phoenix!"
end
test "GET / with incorrect authorization should throw 401", %{conn: conn} do
conn = conn
|> with_invalid_authorization_header()
|> get("/")
assert response(conn, 401) == "unauthorized"
assert get_resp_header(conn, "www-authenticate") == ["Basic realm=\"Thou Shalt not pass\""]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment