Skip to content

Commit

Permalink
decode_body: Improve tar detection
Browse files Browse the repository at this point in the history
Detect when content-type is set to (arguably invalid)
`application/octet-stream; charset-utf8` and when there is none.

Closes #375
  • Loading branch information
wojtekmach committed Jun 24, 2024
1 parent db53ac4 commit e893132
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/req/steps.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1954,22 +1954,25 @@ defmodule Req.Steps do
end

defp format(request, response) do
# TODO: remove ` || ` when we require Elixir v1.13
path = request.url.path || ""

case Req.Response.get_header(response, "content-type") do
[content_type | _] ->
# TODO: remove ` || ` when we require Elixir v1.13
path = request.url.path || ""

case extensions(content_type, path) do
[ext | _] -> ext
[] -> nil
end

[] ->
[]
case extensions("application/octet-stream", path) do
[ext | _] -> ext
[] -> nil
end
end
end

defp extensions("application/octet-stream", path) do
defp extensions("application/octet-stream" <> _, path) do
if tgz?(path) do
["tgz"]
else
Expand Down
22 changes: 22 additions & 0 deletions test/req/steps_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,28 @@ defmodule Req.StepsTest do
assert Req.get!(plug: plug, url: "/foo.tar").body == files
end

test "tar (path, content type with charset utf8)" do
files = [{~c"foo.txt", "bar"}]

plug = fn conn ->
conn
|> Plug.Conn.put_resp_content_type("application/octet-stream")
|> Plug.Conn.send_resp(200, create_tar(files))
end

assert Req.get!(plug: plug, url: "/foo.tar").body == files
end

test "tar (path, no content-type)" do
files = [{~c"foo.txt", "bar"}]

plug = fn conn ->
Plug.Conn.send_resp(conn, 200, create_tar(files))
end

assert Req.get!(plug: plug, url: "/foo.tar.gz").body == files
end

test "tar.gz (path)" do
files = [{~c"foo.txt", "bar"}]

Expand Down

0 comments on commit e893132

Please sign in to comment.