dimanche 17 septembre 2017

Match Error when using Plug headers for testing

I've the following code in my test file.

  test "check basic_authentication" do
    conn = conn(:post, "/basicauth", %{})
    |> put_req_header("authorization", "Basic fjsalfjalkjfdslaj") #problem
    conn = MyApp.Router.call(conn, MyApp.Router.init([]))

    # Some assertions
    assert 1 == 1
  end

When I run mix test I get the following error:

  1) test check basic_authentication (AuthTest)
     test/auth/auth_test.exs:19
     ** (MatchError) no match of right hand side value: :error
     code: conn = MyApp.Router.call(conn, MyApp.Router.init([]))
     stacktrace:
       test/test_helper.exs:3: MyApp.Router.process_basic_auth/2
       test/test_helper.exs:1: MyApp.Router.plug_builder_call/2
       test/auth/auth_test.exs:21: (test)

If I remove the line containing put_req_header in my code, the request gets through but I need to set headers. I've also tried replacing put_req_header with Plug.Conn.put_req_header but to no avail.

This is my router file:

defmodule MyApp.Router do
  use Plug.Router    
  import Plug.Conn
  # import module containing myauth/2

  plug :match
  plug :dispatch

  post "/basicauth" do
    myauth(conn, conn.params)
  end

end

Why am I getting the error and how do I send request headers in tests?

Thank you for your help.

Aucun commentaire:

Enregistrer un commentaire