Skip to content

Commit

Permalink
feat: Add API return error when content type is invalid. #69
Browse files Browse the repository at this point in the history
  • Loading branch information
LuchoTurtle committed Jul 17, 2023
1 parent ab7fda5 commit 40e5455
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
23 changes: 23 additions & 0 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,15 @@ defmodule AppWeb.APITest do
}
}

# image with invalid content type
@invalid_content_type_image %{
"" => %Plug.Upload{
content_type: "image/xyz",
filename: "phoenix.xyz",
path: [:code.priv_dir(:app), "static", "images", "phoenix.xyz"] |> Path.join()
}
}

test "upload succeeds (happy path)", %{conn: conn} do
conn = post(conn, ~p"/api/images", @create_attrs)

Expand Down Expand Up @@ -538,6 +547,14 @@ defmodule AppWeb.APITest do
}
end

test "image file with invalid content type should return appropriate error", %{conn: conn} do
conn = post(conn, ~p"/api/images", @invalid_content_type_image)

assert Map.get(Jason.decode!(response(conn, 400)), "errors") == %{
"detail" => "Error uploading file. The content type of the uploaded file is not valid."
}
end

test "file with invalid binary data type and extension should return error. ", %{conn: conn} do
conn = post(conn, ~p"/api/images", @empty_image)

Expand Down Expand Up @@ -733,6 +750,12 @@ the possible returning values from `upload/1`.
"Error uploading file. The contents of the uploaded file may be empty or invalid."
})

{:error, :invalid_extension} ->
render(conn |> put_status(400), %{
body:
"Error uploading file. The content type of the uploaded file is not valid."
})

_ ->
render(conn |> put_status(400), %{
body: "There was an error uploading the file. Please try again later."
Expand Down
6 changes: 6 additions & 0 deletions lib/app_web/controllers/api_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ defmodule AppWeb.ApiController do
{:error, :failure_read} ->
render(conn |> put_status(400), %{body: "Error uploading file. Failure reading file."})

{:error, :invalid_extension} ->
render(conn |> put_status(400), %{
body:
"Error uploading file. The content type of the uploaded file is not valid."
})

{:error, :invalid_cid} ->
render(conn |> put_status(400), %{
body:
Expand Down
17 changes: 17 additions & 0 deletions test/app_web/api_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ defmodule AppWeb.APITest do
}
}

# image with invalid content type
@invalid_content_type_image %{
"" => %Plug.Upload{
content_type: "image/xyz",
filename: "phoenix.xyz",
path: [:code.priv_dir(:app), "static", "images", "phoenix.xyz"] |> Path.join()
}
}

test "upload succeeds (happy path)", %{conn: conn} do
conn = post(conn, ~p"/api/images", @create_attrs)

Expand Down Expand Up @@ -124,6 +133,14 @@ defmodule AppWeb.APITest do
}
end

test "image file with invalid content type should return appropriate error", %{conn: conn} do
conn = post(conn, ~p"/api/images", @invalid_content_type_image)

assert Map.get(Jason.decode!(response(conn, 400)), "errors") == %{
"detail" => "Error uploading file. The content type of the uploaded file is not valid."
}
end

test "file with invalid binary data type and extension should return error. ", %{conn: conn} do
conn = post(conn, ~p"/api/images", @empty_image)

Expand Down

0 comments on commit 40e5455

Please sign in to comment.