diff --git a/README.md b/README.md index 955771d..a4cafed 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,8 @@ # Downstream - [![Travis](https://img.shields.io/travis/mpiercy827/downstream.svg)](https://travis-ci.org/mpiercy827/downstream) [![Hex.pm](https://img.shields.io/hexpm/v/downstream.svg)](https://hex.pm/packages/downstream/) - Downstream is a library for downloading files via HTTPoison response streaming. ## Installation @@ -102,6 +100,7 @@ iex(2)> Downstream.get("https://www.google.com/notfound", file) iex(3)> File.close(file) :ok ``` + ## Current Features - Stream downloads via HTTP GET requests @@ -143,3 +142,26 @@ any changes, run the following command: ```bash $ mix format ``` + +### Testing + +HTTPoison can be swapped for a mock using configs. + +In test_helpers.exs + +```elixir +Mox.defmock(AppName.MockHttp, for: HTTPoison.Base) +Application.put_env(:downstream, :http_client, AppName.MockHttp) +``` + +In \*\_test.exs + +```elixir +AppName.MockHttp +|> expect(:get!, fn "http://localhost/test", [], [stream_to: pid] -> + send(pid, %HTTPoison.AsyncChunk{chunk: content}) + send(pid, %HTTPoison.AsyncStatus{code: 200}) + send(pid, %HTTPoison.AsyncHeaders{headers: []}) + send(pid, %HTTPoison.AsyncEnd{}) +end) +``` diff --git a/lib/downstream.ex b/lib/downstream.ex index cdf487b..76c1fa8 100644 --- a/lib/downstream.ex +++ b/lib/downstream.ex @@ -28,7 +28,7 @@ defmodule Downstream do download_task = Task.async(Download, :stream, [io_device]) httpoison_options = Keyword.merge(http_options, stream_to: download_task.pid) - HTTPoison.get!(url, headers, httpoison_options) + http_client().get!(url, headers, httpoison_options) try do Task.await(download_task, timeout) @@ -67,7 +67,7 @@ defmodule Downstream do httpoison_options = Keyword.merge(http_options, stream_to: download_task.pid) - HTTPoison.post!(url, body, headers, httpoison_options) + http_client().post!(url, body, headers, httpoison_options) try do Task.await(download_task, timeout) @@ -88,4 +88,8 @@ defmodule Downstream do {:error, %Error{reason: reason}} -> raise Error, reason: reason end end + + def http_client() do + Application.get_env(:downstream, :http_client, HTTPoison) + end end