-
Notifications
You must be signed in to change notification settings - Fork 132
/
adapter_finch_test.exs
151 lines (130 loc) · 5.45 KB
/
adapter_finch_test.exs
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
defmodule ExVCR.Adapter.FinchTest do
use ExUnit.Case, async: true
use ExVCR.Mock, adapter: ExVCR.Adapter.Finch
@port 35008
setup_all do
HttpServer.start(path: "/server", port: @port, response: "test_response")
on_exit fn ->
HttpServer.stop(@port)
end
:ok
end
test "passthrough works when CurrentRecorder has an initial state" do
if ExVCR.Application.global_mock_enabled?() do
ExVCR.Actor.CurrentRecorder.default_state()
|> ExVCR.Actor.CurrentRecorder.set()
end
url = "http://localhost:#{@port}/server"
{:ok, response} = Finch.build(:get, url) |> Finch.request(ExVCRFinch)
assert response.status == 200
end
test "passthrough works after cassette has been used" do
url = "http://localhost:#{@port}/server"
use_cassette "finch_get_localhost" do
{:ok, response} = Finch.build(:get, url) |> Finch.request(ExVCRFinch)
assert response.status == 200
end
{:ok, response} = Finch.build(:get, url) |> Finch.request(ExVCRFinch)
assert response.status == 200
end
test "example single request" do
use_cassette "example_finch" do
{:ok, response} = Finch.build(:get, "http://example.com") |> Finch.request(ExVCRFinch)
assert response.status == 200
assert Map.new(response.headers)["content-type"] == "text/html; charset=UTF-8"
assert response.body =~ ~r/Example Domain/
end
end
test "example multiple requests" do
use_cassette "example_finch_multiple" do
{:ok, response} = Finch.build(:get, "http://example.com") |> Finch.request(ExVCRFinch)
assert response.status == 200
assert response.body =~ ~r/Example Domain/
{:ok, response} = Finch.build(:get, "http://example.com/2") |> Finch.request(ExVCRFinch)
assert response.status == 404
assert response.body =~ ~r/Example Domain/
end
end
test "single request with error" do
use_cassette "error_finch" do
{:error, response} = Finch.build(:get, "http://invalid_url") |> Finch.request(ExVCRFinch)
assert response == %Mint.TransportError{reason: :nxdomain}
end
end
test "request with HTTPError" do
use_cassette "finch_httperror", custom: true do
{:error, response} = Finch.build(:get, "http://example.com/") |> Finch.request(ExVCRFinch)
assert response == %Mint.HTTPError{module: Mint.HTTP2, reason: :too_many_concurrent_requests}
end
end
test "request with generic timeout error" do
use_cassette "finch_generic_timeout_error", custom: true do
{:error, response} = Finch.build(:get, "http://example.com/") |> Finch.request(ExVCRFinch)
assert response == %{reason: :timeout}
end
end
test "request with generic string error" do
use_cassette "finch_generic_string_error", custom: true do
{:error, response} = Finch.build(:get, "http://example.com/") |> Finch.request(ExVCRFinch)
assert response == %{reason: "some made up error which could happen, in theory"}
end
end
test "request with tuple error" do
use_cassette "finch_tuple_transport_error", custom: true do
{:error, response} = Finch.build(:get, "http://example.com/") |> Finch.request(ExVCRFinch)
assert response == %Mint.TransportError{reason: {:bad_alpn_protocol, "h3"}}
end
end
test "post method" do
use_cassette "finch_post" do
assert_response Finch.build(:post, "http://httpbin.org/post", [], "test") |> Finch.request(ExVCRFinch)
end
end
test "put method" do
use_cassette "finch_put" do
assert_response Finch.build(:put, "http://httpbin.org/put", [], "test") |> Finch.request(ExVCRFinch, receive_timeout: 10_000)
end
end
test "patch method" do
use_cassette "finch_patch" do
assert_response Finch.build(:patch, "http://httpbin.org/patch", [], "test") |> Finch.request(ExVCRFinch)
end
end
test "delete method" do
use_cassette "finch_delete" do
assert_response Finch.build(:delete, "http://httpbin.org/delete") |> Finch.request(ExVCRFinch, receive_timeout: 10_000)
end
end
test "get fails with timeout" do
use_cassette "finch_get_timeout" do
{:error, error} = Finch.build(:get, "http://example.com") |> Finch.request(ExVCRFinch, receive_timeout: 1)
assert error == %Mint.TransportError{reason: :timeout}
end
end
test "using recorded cassete, but requesting with different url should return error" do
use_cassette "example_finch_different" do
{:ok, response} = Finch.build(:get, "http://example.com") |> Finch.request(ExVCRFinch)
assert response.status == 200
assert response.body =~ ~r/Example Domain/
end
use_cassette "example_finch_different" do
assert_raise ExVCR.RequestNotMatchError, ~r/different_from_original/, fn ->
{:ok, _response} = Finch.build(:get, "http://example.com/different_from_original") |> Finch.request(ExVCRFinch)
end
end
end
test "stub request works for Finch" do
use_cassette :stub, [url: "http://example.com/", body: "Stub Response", status_code: 200] do
{:ok, response} = Finch.build(:get, "http://example.com") |> Finch.request(ExVCRFinch)
assert response.body =~ ~r/Stub Response/
assert Map.new(response.headers)["content-type"] == "text/html"
assert response.status == 200
end
end
defp assert_response({:ok, response}, function \\ nil) do
assert response.status in 200..299
assert Map.new(response.headers)["connection"] == "keep-alive"
assert is_binary(response.body)
unless function == nil, do: function.(response)
end
end