Skip to content

Commit

Permalink
remove support for routes with parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
notslang committed Jun 14, 2023
1 parent 7034e0b commit 31f8880
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 93 deletions.
56 changes: 8 additions & 48 deletions lib/bypass/instance.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ defmodule Bypass.Instance do
use GenServer, restart: :transient

import Bypass.Utils
import Plug.Router.Utils, only: [build_path_match: 1]

def start_link(opts \\ []) do
GenServer.start_link(__MODULE__, [opts])
Expand Down Expand Up @@ -135,7 +134,6 @@ defmodule Bypass.Instance do
route,
new_route(
fun,
path,
case expect do
:expect -> :once_or_more
:expect_once -> :once
Expand Down Expand Up @@ -276,46 +274,17 @@ defmodule Bypass.Instance do
end

defp route_info(method, path, %{expectations: expectations} = _state) do
segments = build_path_match(path) |> elem(1)

route =
expectations
|> Enum.reduce_while(
{:any, :any, %{}},
fn
{{^method, path_pattern}, %{path_parts: path_parts}}, acc ->
case match_route(segments, path_parts) do
{true, params} -> {:halt, {method, path_pattern, params}}
{false, _} -> {:cont, acc}
end

_, acc ->
{:cont, acc}
end
)

{route, Map.get(expectations, route)}
end

defp match_route(path, route) when length(path) == length(route) do
path
|> Enum.zip(route)
|> Enum.reduce_while(
{true, %{}},
fn
{value, {param, _, _}}, {_, params} ->
{:cont, {true, Map.put(params, Atom.to_string(param), value)}}

{segment, segment}, acc ->
{:cont, acc}
case Map.get(expectations, {method, path}, :no_expectations) do
:no_expectations ->
{:any, :any}

_, _ ->
{:halt, {false, nil}}
_ ->
{method, path}
end
)
end

defp match_route(_, _), do: {false, nil}
{route, Map.get(expectations, route)}
end

defp do_up(port, ref) do
plug_opts = [bypass_instance: self()]
Expand Down Expand Up @@ -398,25 +367,16 @@ defmodule Bypass.Instance do
|> length
end

defp new_route(fun, path_parts, expected) when is_list(path_parts) do
defp new_route(fun, expected) do
%{
fun: fun,
expected: expected,
path_parts: path_parts,
retained_plugs: %{},
results: [],
request_count: 0
}
end

defp new_route(fun, :any, expected) do
new_route(fun, [], expected)
end

defp new_route(fun, path, expected) do
new_route(fun, build_path_match(path) |> elem(1), expected)
end

defp cowboy_opts(port, ref, socket) do
[ref: ref, port: port, transport_options: [num_acceptors: 5, socket: socket]]
end
Expand Down
4 changes: 1 addition & 3 deletions lib/bypass/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ defmodule Bypass.Plug do

@impl true
def call(%{method: method, request_path: request_path} = conn, pid) do
{method, path, path_params} = Bypass.Instance.call(pid, {:get_route, method, request_path})
route = {method, path}
conn = Plug.Conn.fetch_query_params(%{conn | params: path_params})
route = Bypass.Instance.call(pid, {:get_route, method, request_path})

case Bypass.Instance.call(pid, {:get_expect_fun, route}) do
{:ok, ref, fun} ->
Expand Down
42 changes: 0 additions & 42 deletions test/bypass_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -346,48 +346,6 @@ defmodule BypassTest do
end)
end

test "Bypass.stub/4 does not raise if request with parameters is made" do
:stub |> specific_route_with_params
end

test "Bypass.expect/4 can be used to define a specific route with parameters" do
:expect |> specific_route_with_params
end

test "Bypass.expect_once/4 can be used to define a specific route with parameters" do
:expect_once |> specific_route_with_params
end

defp specific_route_with_params(expect_fun) do
bypass = Bypass.open()
method = "POST"
pattern = "/this/:resource/get/:id"
path = "/this/my_resource/get/1234"

apply(Bypass, expect_fun, [
bypass,
method,
pattern,
fn conn ->
assert conn.method == method
assert conn.request_path == path

assert conn.params == %{
"resource" => "my_resource",
"id" => "1234",
"q_param_1" => "a",
"q_param_2" => "b"
}

Plug.Conn.send_resp(conn, 200, "")
end
])

capture_log(fn ->
assert {:ok, 200, ""} = request(bypass.port, path <> "?q_param_1=a&q_param_2=b")
end)
end

test "All routes to a Bypass.expect/4 call must be called" do
:expect |> all_routes_must_be_called
end
Expand Down

0 comments on commit 31f8880

Please sign in to comment.