Why do tests fail when forcing ssl? And how do I fix it?

On a basic Phoenix app, say with the auto generated tests for a blog, mix test runs all 19 tests fine. When forcing ssl using force_ssl: [rewrite_on: [:x_forwarded_proto]] 9 tests fail.
For example

test disconnected and connected render (Phoenix151Web.PageLiveTest)
     test/phoenix151_web/live/page_live_test.exs:6
     ** (MatchError) no match of right hand side value: {:error, {:redirect, %{to: "https://localhost/"}}}
     code: {:ok, page_live, disconnected_html} = live(conn, "/")
     stacktrace:
       test/phoenix151_web/live/page_live_test.exs:7: (test)

Or

test update blog renders errors when data is invalid (Phoenix151Web.BlogControllerTest)
     test/phoenix151_web/controllers/blog_controller_test.exs:66
     ** (RuntimeError) expected response with status 200, got: 307, with body:
     ""
     code: assert html_response(conn, 200) =~ "Edit Blog"
     stacktrace:
       (phoenix 1.5.1) lib/phoenix/test/conn_test.ex:369: Phoenix.ConnTest.response/2
       (phoenix 1.5.1) lib/phoenix/test/conn_test.ex:383: Phoenix.ConnTest.html_response/2
       test/phoenix151_web/controllers/blog_controller_test.exs:68: (test)

I can’t find any documentation or examples on how to change the setup (or the tests) so the tests won’t fail. Any suggestions?

Try moving the force_ssl config to prod.exs.

1 Like

Now that was obvious. Thanks!

1 Like