-
Notifications
You must be signed in to change notification settings - Fork 4
/
httpoison_mock.ex
130 lines (116 loc) · 3.15 KB
/
httpoison_mock.ex
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
defmodule ElixirAuthGithub.HTTPoisonMock do
@moduledoc """
This is a set up to **mock** (stub) our API requests to the GitHub API
so that we can test all of our code in ElixirAuthGithub.
These are just functions that pattern match on the entries
and return things in the way we expect,
so that we can check the pipeline in `ElixirAuthGithub.github_auth/1`
"""
@doc """
`get/3` stubs the HTTPoison get! function when parameters match test vars.
"""
@valid_body %{
access_token: "12345",
login: "test_user",
name: "Testy McTestface",
email: "test@gmail.com",
avatar_url: "https://avatars3.githubusercontent.com/u/10835816",
id: "19"
}
@body_email_nil %{
access_token: "12345",
login: "test_user",
name: "Testy McTestface",
email: nil,
avatar_url: "https://avatars3.githubusercontent.com/u/10835816",
id: "28"
}
@emails [
%{
"email" => "octocat@github.com",
"verified" => true,
"primary" => false,
"visibility" => "private"
},
%{
"email" => "private_email@gmail.com",
"verified" => true,
"primary" => true,
"visibility" => "private"
}
]
def get!(url, headers \\ [], options \\ [])
def get!(
"https://api.github.com/user",
[
{"User-Agent", "ElixirAuthGithub"},
{"Authorization", "token 123"}
],
_options
) do
%{body: "{\"error\": \"test error\"}"}
end
def get!(
"https://api.github.com/user",
[
{"User-Agent", "ElixirAuthGithub"},
{"Authorization", "token 42"}
],
_options
) do
%{body: Jason.encode!(@body_email_nil)}
end
# user emails
def get!(
"https://api.github.com/user/emails",
[
{"User-Agent", "ElixirAuthGithub"},
{"Authorization", "token 42"}
],
_options
) do
%{body: Jason.encode!(@emails)}
end
def get!(_url, _headers, _options) do
%{body: Jason.encode!(@valid_body)}
end
@doc """
`post/3` stubs the HTTPoison post! function when parameters match test vars.
"""
def post!(url, body, headers \\ [], options \\ [])
def post!(
"https://github.com/login/oauth/access_token?client_id=TEST_ID&client_secret=TEST_SECRET&code=1234",
_body,
_headers,
_options
) do
%{body: "error=error"}
end
def post!(
"https://github.com/login/oauth/access_token?client_id=TEST_ID&client_secret=TEST_SECRET&code=123",
_body,
_headers,
_options
) do
%{body: "access_token=123&scope=user"}
end
def post!(
"https://github.com/login/oauth/access_token?client_id=TEST_ID&client_secret=TEST_SECRET&code=42",
_body,
_headers,
_options
) do
%{body: "access_token=42&scope=user"}
end
def post!(
"https://github.com/login/oauth/access_token?client_id=TEST_ID&client_secret=TEST_SECRET&code=12345",
_body,
_headers,
_options
) do
%{body: "access_token=12345&scope=user"}
end
def post!(_url, _body, _headers, _options) do
%{body: "access_token=123456&scope=user"}
end
end