-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmbta_v3_api_test.exs
More file actions
112 lines (97 loc) · 2.91 KB
/
mbta_v3_api_test.exs
File metadata and controls
112 lines (97 loc) · 2.91 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
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
defmodule MBTAV3APITest do
use ExUnit.Case, async: true
import Mox
import Test.Support.Helpers
alias MBTAV3API.JsonApi
setup _ do
reassign_env(:mobile_app_backend, :base_url, "")
reassign_env(:mobile_app_backend, :api_key, "")
:ok
end
setup :verify_on_exit!
describe "get_json/1" do
@tag :capture_log
test "normal responses return a JsonApi struct" do
expect(
MobileAppBackend.HTTPMock,
:request,
fn %Req.Request{url: %URI{path: "/normal_response"}} ->
{:ok, Req.Response.json(%{data: []})}
end
)
response = MBTAV3API.get_json("/normal_response")
assert %JsonApi{} = response
refute response.data == %{}
end
@tag :capture_log
test "encodes the URL" do
expect(
MobileAppBackend.HTTPMock,
:request,
fn %Req.Request{url: %URI{path: "/normal%20response"}} ->
{:ok, Req.Response.json(%{data: []})}
end
)
response = MBTAV3API.get_json("/normal response")
assert %JsonApi{} = response
refute response.data == %{}
end
@tag :capture_log
test "missing endpoints return an error" do
expect(
MobileAppBackend.HTTPMock,
:request,
fn %Req.Request{url: %URI{path: "/missing"}} ->
{:ok,
Req.Response.new(status: 404)
|> Req.Response.json(%{errors: [%{code: :not_found}]})}
end
)
response = MBTAV3API.get_json("/missing")
assert {:error, [%JsonApi.Error{code: "not_found"}]} = response
end
@tag :capture_log
test "can't connect returns an error" do
expect(
MobileAppBackend.HTTPMock,
:request,
fn %Req.Request{url: %URI{path: "/cant_connect"}} ->
{:error, %Mint.TransportError{reason: :nxdomain}}
end
)
response = MBTAV3API.get_json("/cant_connect")
assert {:error, %{reason: _}} = response
end
@tag :capture_log
test "passes an API key if present" do
expect(
MobileAppBackend.HTTPMock,
:request,
fn %Req.Request{
url: %URI{path: "/with_api_key"},
headers: headers,
options: %{params: [other: "value"]}
} ->
assert {"x-api-key", "test_key"} in headers
{:ok, Req.Response.json(%{data: []})}
end
)
%JsonApi{} = MBTAV3API.get_json("/with_api_key", [other: "value"], api_key: "test_key")
end
@tag :capture_log
test "does not pass an API key if not set" do
expect(
MobileAppBackend.HTTPMock,
:request,
fn %Req.Request{
url: %URI{path: "/without_api_key"},
headers: headers
} ->
refute Enum.any?(headers, &(elem(&1, 0) == "x-api-key"))
{:ok, Req.Response.json(%{data: []})}
end
)
%JsonApi{} = MBTAV3API.get_json("/without_api_key", [], api_key: nil)
end
end
end