zac

zac

How to test for presence of image asset

I’m having a hard time figuring out how to validate that image assets are being properly served. Here’s a simplified example (ultimately, I want this to work with dynamic assets, but for now trying with a static image):

  test "GET icons", %{conn: conn} do
    conn = get(conn, ~p"/images/Apple_Icon_Logo_one_color@180x180.png")
    assert response_content_type(conn, :file) =~ "image/png"
  done

But this results in a fail:

  1) test GET icons (BossSiteWeb.PageControllerTest)
     test/boss_site_web/controllers/page_controller_test.exs:13
     ** (RuntimeError) expected content-type for file, got: "image/png"
     code: assert response_content_type(conn, :file) =~ "image/png"
     stacktrace:
       (phoenix 1.7.10) lib/phoenix/test/conn_test.ex:319: Phoenix.ConnTest.response_content_type/2
       test/boss_site_web/controllers/page_controller_test.exs:15: (test)

My initial try was with:

assert response_content_type(conn, :"image/png")

That also fails:

     ** (RuntimeError) expected content-type for image/png, got: "image/png"

Having trouble deciphering what I can test for…

Marked As Solved

al2o3cr

al2o3cr

The code in response_content_type gives the error message you’re encountering if this function returns false:

https://github.com/phoenixframework/phoenix/blob/cdf0eb83d51584e046770d95c6aaf53fd4c4ac95/lib/phoenix/test/conn_test.ex#L326-L335

Here {part, subpart} will be {"image", "png"}, so you’ll need to pass a format that passes one of the three conditions:

  • matches the result of running MIME.extensions("image/png") - ["png"] on my system
  • is equal to subpart
  • is at the end of the subpart preceded by a +

The third condition doesn’t seem relevant, and the other two are the same. You should pass :png to response_content_type. You may not need the =~ part unless you’re doing something unusual:

assert response_content_type(conn, :png)

Where Next?

Popular in Questions Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31494 112
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement