@@ -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