Hello friends, Imagine you want to download a tar
file from hex.pm
website.
I have this function and do not want to install req hex plugin
. as you see I find the "contents.tar.gz"
and it is the binary of file.
@spec download(download_type, pkg) :: okey_return | error_return
def download(:hex, %{app: app, tag: tag_name}) when not is_nil(tag_name) do
case build_url("https://repo.hex.pm/tarballs/#{app}-#{tag_name}.tar") do
%Req.Response{status: 200, body: body} ->
converted = Map.new(body, fn {key, value} -> {to_string(key), value} end)
{:ok, converted["contents.tar.gz"]}
_ ->
mix_global_err()
end
end
And downloader function:
defp build_url(location) do
[base_url: location]
|> Keyword.merge(Application.get_env(:mishka_installer, :downloader_req_options, []))
|> Keyword.merge(Application.get_env(:mishka_installer, :proxy, []))
|> Req.request!()
end
Now I want to mock it, so this is the place I can not be able to fix.
One of my function test is what does not work:
body = [
{~c"VERSION", "3"},
{~c"CHECKSUM", "1D5EC32825E5AF1B5CF58933F4C918909BC79E4391EBE2B9315B02705145B18F"},
{~c"metadata.config", "{<<\"app\">>,<<\"mishka_installer\">>}.\n{<<\"build_tools\">>"},
{~c"contents.tar.gz", <<31, 139, 8, 0, 0, 0>>}
]
body_iolist_to_binary =
Enum.reduce(body, "", fn {key, value}, acc -> acc <> "#{key}:#{value}\n" end)
|> :erlang.iolist_to_binary()
Req.Test.expect(Downloader, fn conn ->
conn
|> Plug.Conn.put_resp_content_type("application/octet-stream")
|> Plug.Conn.send_resp(200, body_iolist_to_binary)
end)
If I put body
directly I have error!! I have no idea how can simulate the hex output.
It should be noted my function works as well, my problem is located in testing this
Thank you in advance