Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
105 lines (93 loc) · 4.01 KB

File metadata and controls

105 lines (93 loc) · 4.01 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
defmodule CodeCorps.GitHub.SuccessAPI do
@moduledoc ~S"""
A mocked github API layer which returns a default successful response for all
GitHub API requests.
All tests in the test environment use this module as a mock for GitHub API
requests by default.
If certain tests explicitly depend on the data returned by GitHub, they can be
mocked individually using the `CodeCorps.GitHub.TestHelpers.with_mock_api`
macro.
As support for new GitHub endpoints is added, defaults for those endpoints
should be added here.
To assert a request has been made to GitHub as a result as an action, the
`assert_received` test helper can be used:
```
assert_received({:get, "https://api.github.com/user", body, headers, options})
```
"""
import CodeCorps.GitHub.TestHelpers
defmodule UnhandledGitHubEndpointError do
defexception message: "You have a GitHub API endpoint that's unhandled in tests."
end
defmodule GitHubMockResponseError do
defexception message: "There was a problem in building a response for your mocked GitHub API."
end
def request(method, url, body, headers, options) do
send(self(), {method, url, body, headers, options})
with {:ok, body} = get_body(method, url, body, headers, options) |> Poison.encode,
{:ok, code} = method |> success_code()
do
response = %HTTPoison.Response{body: body, request_url: url, status_code: code}
{:ok, response}
end
end
defp get_body(:head, _, _, _, _), do: ""
defp get_body(:post, "https://github.com/login/oauth/access_token", _, _, _) do
%{"access_token" => "foo_auth_token"}
end
defp get_body(method, "https://api.github.com/" <> endpoint, body, headers, options) do
get_body(method, endpoint |> String.split("/"), body, headers, options)
end
defp get_body(:get, ["user"], _, _, _), do: load_endpoint_fixture("user")
defp get_body(_method, ["installation", "repositories"], _, _, _) do
load_endpoint_fixture("installation_repositories")
end
defp get_body(:post, ["installations", _id, "access_tokens"], _, _, _) do
%{
"token" => "v1.1f699f1069f60xxx",
"expires_at" => Timex.now() |> Timex.shift(hours: 1) |> DateTime.to_iso8601
}
end
defp get_body(:get, ["repos", _owner, _repo, "issues", "comments"], _, _, _) do
load_endpoint_fixture("issues_comments")
end
defp get_body(:get, ["repos", _owner, _repo, "issues", _number], _, _, _) do
load_endpoint_fixture("issue")
end
defp get_body(:post, ["repos", _owner, _repo, "issues"], _, _, _) do
load_endpoint_fixture("issue")
end
defp get_body(:patch, ["repos", _owner, _repo, "issues", _number], _, _, _) do
load_endpoint_fixture("issue")
end
defp get_body(:post, ["repos", _owner, _repo, "issues", _number, "comments"], _, _, _) do
load_endpoint_fixture("issue_comment")
end
defp get_body(:patch, ["repos", _owner, _repo, "issues", "comments", _id], _, _, _) do
load_endpoint_fixture("issue_comment")
end
defp get_body(:get, ["repos", _owner, _repo, "issues"], _, _, _) do
load_endpoint_fixture("issues")
end
defp get_body(:get, ["repos", _owner, _repo, "pulls"], _, _, _) do
load_endpoint_fixture("pulls")
end
defp get_body(:get, ["repos", _owner, _repo, "pulls", _number], _, _, _) do
load_endpoint_fixture("pull_request")
end
defp get_body(method, endpoint, _, _, _) when is_binary(endpoint) do
raise UnhandledGitHubEndpointError, message: "You have an unhandled :#{method} request to #{endpoint}"
end
defp get_body(method, uri_parts, _, _, _) when is_list uri_parts do
endpoint = uri_parts |> Enum.join("/")
raise UnhandledGitHubEndpointError, message: "You have an unhandled API :#{method} request to #{endpoint}"
end
@spec success_code(atom) :: integer
defp success_code(:get), do: {:ok, 200}
defp success_code(:post), do: {:ok, 201}
defp success_code(:patch), do: {:ok, 202}
defp success_code(:put), do: {:ok, 202}
defp success_code(:delete), do: {:ok, 204}
defp success_code(:head), do: {:ok, 204}
defp success_code(_), do: {:error, :unexpected_code}
end
Morty Proxy This is a proxified and sanitized view of the page, visit original site.