Skip to content

Commit e893132

Browse files
committed
decode_body: Improve tar detection
Detect when content-type is set to (arguably invalid) `application/octet-stream; charset-utf8` and when there is none. Closes wojtekmach#375
1 parent db53ac4 commit e893132

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

lib/req/steps.ex

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,22 +1954,25 @@ defmodule Req.Steps do
19541954
end
19551955

19561956
defp format(request, response) do
1957+
# TODO: remove ` || ` when we require Elixir v1.13
1958+
path = request.url.path || ""
1959+
19571960
case Req.Response.get_header(response, "content-type") do
19581961
[content_type | _] ->
1959-
# TODO: remove ` || ` when we require Elixir v1.13
1960-
path = request.url.path || ""
1961-
19621962
case extensions(content_type, path) do
19631963
[ext | _] -> ext
19641964
[] -> nil
19651965
end
19661966

19671967
[] ->
1968-
[]
1968+
case extensions("application/octet-stream", path) do
1969+
[ext | _] -> ext
1970+
[] -> nil
1971+
end
19691972
end
19701973
end
19711974

1972-
defp extensions("application/octet-stream", path) do
1975+
defp extensions("application/octet-stream" <> _, path) do
19731976
if tgz?(path) do
19741977
["tgz"]
19751978
else

test/req/steps_test.exs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,28 @@ defmodule Req.StepsTest do
764764
assert Req.get!(plug: plug, url: "/foo.tar").body == files
765765
end
766766

767+
test "tar (path, content type with charset utf8)" do
768+
files = [{~c"foo.txt", "bar"}]
769+
770+
plug = fn conn ->
771+
conn
772+
|> Plug.Conn.put_resp_content_type("application/octet-stream")
773+
|> Plug.Conn.send_resp(200, create_tar(files))
774+
end
775+
776+
assert Req.get!(plug: plug, url: "/foo.tar").body == files
777+
end
778+
779+
test "tar (path, no content-type)" do
780+
files = [{~c"foo.txt", "bar"}]
781+
782+
plug = fn conn ->
783+
Plug.Conn.send_resp(conn, 200, create_tar(files))
784+
end
785+
786+
assert Req.get!(plug: plug, url: "/foo.tar.gz").body == files
787+
end
788+
767789
test "tar.gz (path)" do
768790
files = [{~c"foo.txt", "bar"}]
769791

0 commit comments

Comments
 (0)