Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
```
8 changes: 6 additions & 2 deletions lib/downstream.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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