From ed599bd4dfb802e6d2b73db668cfcde80beb79b5 Mon Sep 17 00:00:00 2001 From: Bryan Joseph Date: Thu, 9 Apr 2015 19:31:59 -0500 Subject: [PATCH] Updated tests to use put_req_header --- mix.exs | 2 +- mix.lock | 4 ++-- test/plug_jwt_router_test.exs | 20 ++++++++++++++++---- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/mix.exs b/mix.exs index 4bc2b38..4dc97c5 100644 --- a/mix.exs +++ b/mix.exs @@ -16,7 +16,7 @@ defmodule PlugJwt.Mixfile do defp deps do [ - {:joken, "~> 0.11"}, + {:joken, "~> 0.12"}, {:plug, ">= 0.7.0"}, {:cowboy, "~> 1.0.0"}, {:jsx, "~> 2.1.1", only: :test} diff --git a/mix.lock b/mix.lock index 06825e0..899b8c4 100644 --- a/mix.lock +++ b/mix.lock @@ -1,8 +1,8 @@ %{"cowboy": {:hex, :cowboy, "1.0.0"}, "cowlib": {:hex, :cowlib, "1.0.1"}, "jazz": {:package, "0.2.1"}, - "joken": {:hex, :joken, "0.11.0"}, + "joken": {:hex, :joken, "0.12.0"}, "jsx": {:hex, :jsx, "2.1.1"}, - "plug": {:hex, :plug, "0.11.1"}, + "plug": {:hex, :plug, "0.7.0"}, "ranch": {:hex, :ranch, "1.0.0"}, "timex": {:hex, :timex, "0.13.1"}} diff --git a/test/plug_jwt_router_test.exs b/test/plug_jwt_router_test.exs index 74163fa..b5dec1a 100644 --- a/test/plug_jwt_router_test.exs +++ b/test/plug_jwt_router_test.exs @@ -63,7 +63,10 @@ defmodule PlugJwtRouterTest do {:ok, token} = Joken.Token.encode("secret", TestJsx, payload) auth_header = "Bearer " <> token - conn = conn(:get, "/", [], headers: [{"authorization", auth_header}]) |> TestRouterPlug.call([]) + conn = conn(:get, "/", []) + |> put_req_header("authorization", auth_header) + |> TestRouterPlug.call([]) + assert conn.status == 200 assert conn.resp_body == "Hello Tester" assert conn.assigns.claims == %{admin: true, name: "John Doe", sub: 1234567890} @@ -74,7 +77,10 @@ defmodule PlugJwtRouterTest do {:ok, token} = Joken.Token.encode("test", TestJsx, payload) auth_header = "Bearer " <> token - conn = conn(:get, "/", [], headers: [{"authorization", auth_header}]) |> TestRouterPlugFromConfig.call([]) + conn = conn(:get, "/", []) + |> put_req_header("authorization", auth_header) + |> TestRouterPlugFromConfig.call([]) + assert conn.status == 200 assert conn.resp_body == "Hello Tester" assert conn.assigns.claims == %{admin: true, name: "John Doe", sub: 1234567890} @@ -82,14 +88,20 @@ defmodule PlugJwtRouterTest do test "Send 401 when invalid token sent" do incorrect_credentials = "Bearer " <> "Not a token" - conn = conn(:get, "/", [], headers: [{"authorization", incorrect_credentials}]) |> TestRouterPlug.call([]) + conn = conn(:get, "/", []) + |> put_req_header("authorization", incorrect_credentials) + |> TestRouterPlug.call([]) + assert conn.status == 401 assert conn.resp_body == "{\"description\":\"Invalid JSON Web Token\",\"error\":\"Unauthorized\",\"status_code\":401}" end test "Send 401 when invalid token sent (settings from config)" do incorrect_credentials = "Bearer " <> "Not a token" - conn = conn(:get, "/", [], headers: [{"authorization", incorrect_credentials}]) |> TestRouterPlugFromConfig.call([]) + conn = conn(:get, "/", []) + |> put_req_header("authorization", incorrect_credentials) + |> TestRouterPlugFromConfig.call([]) + assert conn.status == 401 assert conn.resp_body == "{\"description\":\"Invalid JSON Web Token\",\"error\":\"Unauthorized\",\"status_code\":401}" end