Skip to content

Commit 9dbd022

Browse files
authored
Merge pull request #81 from VitorTrin/redefine-expects
Allow redefining the expectations for a route
2 parents 877292c + 40cfc08 commit 9dbd022

File tree

2 files changed

+61
-20
lines changed

2 files changed

+61
-20
lines changed

lib/bypass/instance.ex

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -134,24 +134,18 @@ defmodule Bypass.Instance do
134134
route = {method, path}
135135

136136
updated_expectations =
137-
case Map.get(expectations, route, :none) do
138-
:none ->
139-
Map.put(
140-
expectations,
141-
route,
142-
new_route(
143-
fun,
144-
case expect do
145-
:expect -> :once_or_more
146-
:expect_once -> :once
147-
:stub -> :none_or_more
148-
end
149-
)
150-
)
151-
152-
_ ->
153-
raise "Route already installed for #{method}, #{path}"
154-
end
137+
Map.put(
138+
expectations,
139+
route,
140+
new_route(
141+
fun,
142+
case expect do
143+
:expect -> :once_or_more
144+
:expect_once -> :once
145+
:stub -> :none_or_more
146+
end
147+
)
148+
)
155149

156150
{:reply, :ok, %{state | expectations: updated_expectations}}
157151
end

test/bypass_test.exs

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ defmodule BypassTest do
1616
end
1717

1818
test "show ISSUE #51" do
19-
Enum.each(1..1000,
20-
fn (_) ->
19+
Enum.each(
20+
1..1000,
21+
fn _ ->
2122
bypass = %Bypass{} = Bypass.open(port: 8000)
2223

2324
Bypass.down(bypass)
@@ -447,6 +448,52 @@ defmodule BypassTest do
447448
end
448449
end
449450

451+
test "Bypass.expect/4 can be used to define a specific route and then redefined" do
452+
:expect |> specific_route_redefined
453+
end
454+
455+
test "Bypass.expect_once/4 can be used to define a specific route and then redefined" do
456+
:expect_once |> specific_route_redefined
457+
end
458+
459+
defp specific_route_redefined(expect_fun) do
460+
bypass = Bypass.open()
461+
method = "POST"
462+
path = "/this"
463+
464+
# one of Bypass.expect or Bypass.expect_once
465+
apply(Bypass, expect_fun, [
466+
bypass,
467+
method,
468+
path,
469+
fn conn ->
470+
assert conn.method == method
471+
assert conn.request_path == path
472+
Plug.Conn.send_resp(conn, 200, "")
473+
end
474+
])
475+
476+
capture_log(fn ->
477+
assert {:ok, 200, ""} = request(bypass.port, path)
478+
end)
479+
480+
# redefining the expect
481+
apply(Bypass, expect_fun, [
482+
bypass,
483+
method,
484+
path,
485+
fn conn ->
486+
assert conn.method == method
487+
assert conn.request_path == path
488+
Plug.Conn.send_resp(conn, 200, "other response")
489+
end
490+
])
491+
492+
capture_log(fn ->
493+
assert {:ok, 200, "other response"} = request(bypass.port, path)
494+
end)
495+
end
496+
450497
defp prepare_stubs do
451498
bypass = Bypass.open()
452499

0 commit comments

Comments
 (0)