Skip to content

Commit 40cfc08

Browse files
committed
Merge pull request #2 from VitorTrin/PSPDFKit-labs-master
2 parents 58088d6 + abea6bc commit 40cfc08

File tree

10 files changed

+371
-200
lines changed

10 files changed

+371
-200
lines changed

.formatter.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[
2+
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
3+
]

.travis.yml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
language: elixir
22
elixir:
3-
- 1.7.4
4-
- 1.6.6
3+
- 1.10
4+
- 1.9
5+
- 1.8
6+
- 1.7
7+
- 1.6
58
otp_release:
69
- 20.0
710
- 21.0
11+
- 22.0
812
sudo: false # to use faster container based build environment
913
env:
1014
- MIX_ENV=test
@@ -14,7 +18,10 @@ install:
1418
- mix compile
1519
script:
1620
- mix test
21+
# https://hexdocs.pm/elixir/compatibility-and-deprecations.html#compatibility-between-elixir-and-erlang-otp
1722
matrix:
1823
exclude:
24+
- otp_release: 22.0
25+
elixir: 1.6
1926
- otp_release: 20.0
20-
elixir: 1.3.4
27+
elixir: 1.10

README.md

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ Add bypass to your list of dependencies in mix.exs:
1313

1414
```elixir
1515
def deps do
16-
[{:bypass, "~> 1.0", only: :test}]
16+
[
17+
{:bypass, "~> 1.0", only: :test}
18+
]
1719
end
1820
```
1921

@@ -28,7 +30,7 @@ Bypass supports Elixir 1.6 and OTP 20 and up. It works with Cowboy 1 and 2.
2830
Start Bypass in your `test/test_helper.exs` file to make it available in tests:
2931

3032
```elixir
31-
ExUnit.start
33+
ExUnit.start()
3234
Application.ensure_all_started(:bypass)
3335
```
3436

@@ -52,23 +54,23 @@ You can take any of the following approaches:
5254
Must be called at least once.
5355

5456
```elixir
55-
Bypass.expect bypass, fn conn ->
56-
assert "/1.1/statuses/update.json" == conn.request_path
57-
assert "POST" == conn.method
58-
Plug.Conn.resp(conn, 429, ~s<{"errors": [{"code": 88, "message": "Rate limit exceeded"}]}>)
59-
end
57+
Bypass.expect(bypass, fn conn ->
58+
assert "/1.1/statuses/update.json" == conn.request_path
59+
assert "POST" == conn.method
60+
Plug.Conn.resp(conn, 429, ~s<{"errors": [{"code": 88, "message": "Rate limit exceeded"}]}>)
61+
end)
6062
```
6163

6264
#### expect_once/2 (bypass_instance, function)
6365

6466
Must be called exactly once.
6567

6668
```elixir
67-
Bypass.expect_once bypass, fn conn ->
68-
assert "/1.1/statuses/update.json" == conn.request_path
69-
assert "POST" == conn.method
70-
Plug.Conn.resp(conn, 429, ~s<{"errors": [{"code": 88, "message": "Rate limit exceeded"}]}>)
71-
end
69+
Bypass.expect_once(bypass, fn conn ->
70+
assert "/1.1/statuses/update.json" == conn.request_path
71+
assert "POST" == conn.method
72+
Plug.Conn.resp(conn, 429, ~s<{"errors": [{"code": 88, "message": "Rate limit exceeded"}]}>)
73+
end)
7274
```
7375

7476
#### expect/4 (bypass_instance, method, path, function)
@@ -80,10 +82,10 @@ Must be called at least once.
8082
`path` is the endpoint.
8183

8284
```elixir
83-
Bypass.expect bypass, "POST", "/1.1/statuses/update.json", fn conn ->
84-
Agent.get_and_update(AgentModule, fn step_no -> {step_no, step_no+1} end)
85-
Plug.Conn.resp(conn, 429, ~s<{"errors": [{"code": 88, "message": "Rate limit exceeded"}]}>)
86-
end
85+
Bypass.expect(bypass, "POST", "/1.1/statuses/update.json", fn conn ->
86+
Agent.get_and_update(AgentModule, fn step_no -> {step_no, step_no + 1} end)
87+
Plug.Conn.resp(conn, 429, ~s<{"errors": [{"code": 88, "message": "Rate limit exceeded"}]}>)
88+
end)
8789
```
8890

8991
#### expect_once/4 (bypass_instance, method, path, function)
@@ -95,10 +97,10 @@ Must be called exactly once.
9597
`path` is the endpoint.
9698

9799
```elixir
98-
Bypass.expect_once bypass, "POST", "/1.1/statuses/update.json", fn conn ->
99-
Agent.get_and_update(AgentModule, fn step_no -> {step_no, step_no+1} end)
100-
Plug.Conn.resp(conn, 429, ~s<{"errors": [{"code": 88, "message": "Rate limit exceeded"}]}>)
101-
end
100+
Bypass.expect_once(bypass, "POST", "/1.1/statuses/update.json", fn conn ->
101+
Agent.get_and_update(AgentModule, fn step_no -> {step_no, step_no + 1} end)
102+
Plug.Conn.resp(conn, 429, ~s<{"errors": [{"code": 88, "message": "Rate limit exceeded"}]}>)
103+
end)
102104
```
103105

104106
#### stub/4 (bypass_instance, method, path, function)
@@ -110,10 +112,10 @@ May be called none or more times.
110112
`path` is the endpoint.
111113

112114
```elixir
113-
Bypass.stub bypass, "POST", "/1.1/statuses/update.json", fn conn ->
114-
Agent.get_and_update(AgentModule, fn step_no -> {step_no, step_no+1} end)
115-
Plug.Conn.resp(conn, 429, ~s<{"errors": [{"code": 88, "message": "Rate limit exceeded"}]}>)
116-
end
115+
Bypass.stub(bypass, "POST", "/1.1/statuses/update.json", fn conn ->
116+
Agent.get_and_update(AgentModule, fn step_no -> {step_no, step_no + 1} end)
117+
Plug.Conn.resp(conn, 429, ~s<{"errors": [{"code": 88, "message": "Rate limit exceeded"}]}>)
118+
end)
117119
```
118120

119121
### Example
@@ -126,23 +128,25 @@ defmodule TwitterClientTest do
126128
use ExUnit.Case, async: true
127129

128130
setup do
129-
bypass = Bypass.open
131+
bypass = Bypass.open()
130132
{:ok, bypass: bypass}
131133
end
132134

133135
test "client can handle an error response", %{bypass: bypass} do
134-
Bypass.expect_once bypass, "POST", "/1.1/statuses/update.json", fn conn ->
136+
Bypass.expect_once(bypass, "POST", "/1.1/statuses/update.json", fn conn ->
135137
Plug.Conn.resp(conn, 429, ~s<{"errors": [{"code": 88, "message": "Rate limit exceeded"}]}>)
136-
end
138+
end)
139+
137140
{:ok, client} = TwitterClient.start_link(url: endpoint_url(bypass.port))
138141
assert {:error, :rate_limited} == TwitterClient.post_tweet(client, "Elixir is awesome!")
139142
end
140143

141144
test "client can recover from server downtime", %{bypass: bypass} do
142-
Bypass.expect bypass, fn conn ->
145+
Bypass.expect(bypass, fn conn ->
143146
# We don't care about `request_path` or `method` for this test.
144147
Plug.Conn.resp(conn, 200, "")
145-
end
148+
end)
149+
146150
{:ok, client} = TwitterClient.start_link(url: endpoint_url(bypass.port))
147151

148152
assert :ok == TwitterClient.post_tweet(client, "Elixir is awesome!")
@@ -198,7 +202,7 @@ defmodule TwitterClientSpec do
198202
use ESpec, async: true
199203

200204
before do
201-
bypass = Bypass.open
205+
bypass = Bypass.open()
202206
{:shared, bypass: bypass}
203207
end
204208

@@ -207,9 +211,10 @@ defmodule TwitterClientSpec do
207211
end
208212

209213
specify "the client can handle an error response" do
210-
Bypass.expect_once shared.bypass, "POST", "/1.1/statuses/update.json", fn conn ->
214+
Bypass.expect_once(shared.bypass, "POST", "/1.1/statuses/update.json", fn conn ->
211215
Plug.Conn.resp(conn, 429, ~s<{"errors": [{"code": 88, "message": "Rate limit exceeded"}]}>)
212-
end
216+
end)
217+
213218
{:ok, client} = TwitterClient.start_link(url: endpoint_url(shared.bypass.port))
214219
assert {:error, :rate_limited} == TwitterClient.post_tweet(client, "Elixir is awesome!")
215220
end

lib/bypass.ex

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ defmodule Bypass do
2020
handled and set expectations on the calls.
2121
"""
2222
def open(opts \\ []) do
23-
case Supervisor.start_child(Bypass.Supervisor, [opts]) do
23+
case DynamicSupervisor.start_child(Bypass.Supervisor, Bypass.Instance.child_spec(opts)) do
2424
{:ok, pid} ->
2525
port = Bypass.Instance.call(pid, :port)
26-
debug_log "Did open connection #{inspect pid} on port #{inspect port}"
26+
debug_log("Did open connection #{inspect(pid)} on port #{inspect(port)}")
2727
bypass = %Bypass{pid: pid, port: port}
2828
setup_framework_integration(test_framework(), bypass)
2929
bypass
30+
3031
other ->
3132
other
3233
end
3334
end
3435

35-
3636
# Raise an error if called with an unknown framework
3737
#
3838
defp setup_framework_integration(:ex_unit, bypass = %{pid: pid}) do
@@ -45,7 +45,6 @@ defmodule Bypass do
4545
# Entry point for more advanced ESpec configurations
4646
end
4747

48-
4948
@doc """
5049
Can be called to immediately verify if the declared request
5150
expectations have been met.
@@ -66,33 +65,39 @@ defmodule Bypass do
6665
end
6766
end
6867

69-
7068
defp do_verify_expectations(bypass_pid, error_module) do
7169
case Bypass.Instance.call(bypass_pid, :on_exit) do
7270
:ok ->
7371
:ok
72+
7473
:ok_call ->
7574
:ok
75+
7676
{:error, :too_many_requests, {:any, :any}} ->
7777
raise error_module, "Expected only one HTTP request for Bypass"
78+
7879
{:error, :too_many_requests, {method, path}} ->
7980
raise error_module, "Expected only one HTTP request for Bypass at #{method} #{path}"
81+
8082
{:error, :unexpected_request, {:any, :any}} ->
8183
raise error_module, "Bypass got an HTTP request but wasn't expecting one"
84+
8285
{:error, :unexpected_request, {method, path}} ->
8386
raise error_module,
84-
"Bypass got an HTTP request but wasn't expecting one at #{method} #{path}"
87+
"Bypass got an HTTP request but wasn't expecting one at #{method} #{path}"
88+
8589
{:error, :not_called, {:any, :any}} ->
8690
raise error_module, "No HTTP request arrived at Bypass"
91+
8792
{:error, :not_called, {method, path}} ->
8893
raise error_module,
89-
"No HTTP request arrived at Bypass at #{method} #{path}"
94+
"No HTTP request arrived at Bypass at #{method} #{path}"
95+
9096
{:exit, {class, reason, stacktrace}} ->
9197
:erlang.raise(class, reason, stacktrace)
9298
end
9399
end
94100

95-
96101
def up(%Bypass{pid: pid}),
97102
do: Bypass.Instance.call(pid, :up)
98103

@@ -117,7 +122,6 @@ defmodule Bypass do
117122
def pass(%Bypass{pid: pid}),
118123
do: Bypass.Instance.call(pid, :pass)
119124

120-
121125
defp test_framework do
122126
Application.get_env(:bypass, :test_framework, :ex_unit)
123127
end

lib/bypass/application.ex

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ defmodule Bypass.Application do
44
def start(_type, _args) do
55
import Supervisor.Spec, warn: false
66

7-
children = [
8-
worker(Bypass.Instance, [], restart: :transient)
9-
]
10-
11-
opts = [strategy: :simple_one_for_one, name: Bypass.Supervisor]
12-
Supervisor.start_link(children, opts)
7+
opts = [strategy: :one_for_one, name: Bypass.Supervisor]
8+
DynamicSupervisor.start_link(opts)
139
end
1410
end

0 commit comments

Comments
 (0)