Skip to content

Commit 58088d6

Browse files
committed
Allow redefining the expectations for a route
1 parent 0e44e65 commit 58088d6

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

lib/bypass/instance.ex

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,6 @@ defmodule Bypass.Instance do
116116
route = {method, path}
117117

118118
updated_expectations =
119-
case Map.get(expectations, route, :none) do
120-
:none ->
121119
Map.put(expectations, route, new_route(
122120
fun,
123121
case expect do
@@ -126,9 +124,6 @@ defmodule Bypass.Instance do
126124
:stub -> :none_or_more
127125
end
128126
))
129-
_ ->
130-
raise "Route already installed for #{method}, #{path}"
131-
end
132127

133128
{:reply, :ok, %{state | expectations: updated_expectations}}
134129
end

test/bypass_test.exs

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ defmodule BypassTest do
2424

2525
defp specify_port(port, expect_fun) do
2626
bypass = Bypass.open(port: port)
27-
27+
2828
# one of Bypass.expect or Bypass.expect_once
2929
apply(Bypass, expect_fun, [
3030
bypass,
@@ -33,7 +33,7 @@ defmodule BypassTest do
3333
Plug.Conn.send_resp(conn, 200, "")
3434
end
3535
])
36-
36+
3737
assert {:ok, 200, ""} = request(port)
3838
assert {:error, :eaddrinuse} == Bypass.open(port: port)
3939
end
@@ -403,10 +403,56 @@ defmodule BypassTest do
403403
end
404404
end
405405

406+
test "Bypass.expect/4 can be used to define a specific route and then redefined" do
407+
:expect |> specific_route_redefined
408+
end
409+
410+
test "Bypass.expect_once/4 can be used to define a specific route and then redefined" do
411+
:expect_once |> specific_route_redefined
412+
end
413+
414+
defp specific_route_redefined(expect_fun) do
415+
bypass = Bypass.open()
416+
method = "POST"
417+
path = "/this"
418+
419+
# one of Bypass.expect or Bypass.expect_once
420+
apply(Bypass, expect_fun, [
421+
bypass,
422+
method,
423+
path,
424+
fn conn ->
425+
assert conn.method == method
426+
assert conn.request_path == path
427+
Plug.Conn.send_resp(conn, 200, "")
428+
end
429+
])
430+
431+
capture_log(fn ->
432+
assert {:ok, 200, ""} = request(bypass.port, path)
433+
end)
434+
435+
# redefining the expect
436+
apply(Bypass, expect_fun, [
437+
bypass,
438+
method,
439+
path,
440+
fn conn ->
441+
assert conn.method == method
442+
assert conn.request_path == path
443+
Plug.Conn.send_resp(conn, 200, "other response")
444+
end
445+
])
446+
447+
capture_log(fn ->
448+
assert {:ok, 200, "other response"} = request(bypass.port, path)
449+
end)
450+
end
451+
406452

407453
defp prepare_stubs do
408454
bypass = Bypass.open
409-
455+
410456
Bypass.expect_once(bypass, fn conn ->
411457
Plug.Conn.send_resp(conn, 200, "")
412458
end)

0 commit comments

Comments
 (0)