forked from code-corps/code-corps-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_case.ex
More file actions
51 lines (39 loc) · 1.16 KB
/
github_case.ex
File metadata and controls
51 lines (39 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
defmodule CodeCorps.GitHubCase do
@moduledoc ~S"""
For use in tests where Bypass is needed.
The module is mainly used through tags.
Example:
```
use CodeCorps.GitHubCase
@tag bypass: %{
"/path/to/request" => {status_code, data_to_respond_with},
"/path/to/other/request" => {status_code, data_to_respond_with}
# etc...
}
test "some test which makes a github api request" do
# run test code here, all github API requests will automatically respond
# as specified, if listed in the bypass tag
end
"""
alias CodeCorps.GitHub
use ExUnit.CaseTemplate
setup tags do
bypass = Bypass.open
GitHub.Bypass.setup(bypass)
case tags |> Map.get(:bypass) do
nil -> nil
bypass_data -> bypass |> setup_handling(bypass_data)
end
on_exit fn ->
GitHub.Bypass.teardown()
end
{:ok, bypass: bypass}
end
defp setup_handling(bypass, handler_data) do
bypass |> Bypass.expect(fn %Plug.Conn{request_path: path} = conn ->
{status, data} = handler_data |> Map.get(path)
response = data |> Poison.encode!
conn |> Plug.Conn.resp(status, response)
end)
end
end