Skip to content

Commit

Permalink
Increased walk reluctance and fixed unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kotva006 committed Nov 9, 2023
1 parent f82badb commit 6c5494a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
13 changes: 11 additions & 2 deletions apps/trip_plan/lib/trip_plan/api/open_trip_planner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule TripPlan.Api.OpenTripPlanner do
}
"""

root_url = params["root_url"] || pick_url(connection_opts)
root_url = Keyword.get(opts, :root_url, nil) || pick_url(connection_opts)
graphql_url = "#{root_url}/otp/routers/default/index/"

send_request(graphql_url, graph_ql_query, &parse_ql/1)
Expand Down Expand Up @@ -98,7 +98,9 @@ defmodule TripPlan.Api.OpenTripPlanner do
end

defp log_response(url, query) do
graphql_req = Req.new(base_url: url) |> AbsintheClient.attach()
graphql_req =
Req.new(base_url: url, headers: build_headers(config(:wiremock_proxy)))
|> AbsintheClient.attach()

{duration, response} =
:timer.tc(
Expand All @@ -115,6 +117,13 @@ defmodule TripPlan.Api.OpenTripPlanner do
response
end

defp build_headers("true") do
proxy_url = Application.get_env(:trip_plan, OpenTripPlanner)[:wiremock_proxy_url]
[{"X-WM-Proxy-Url", proxy_url}]
end

defp build_headers(_), do: []

defp status_text({:ok, %{status: code, body: body}}) do
string_body = Poison.encode!(body)
"status=#{code} content_length=#{byte_size(string_body)}"
Expand Down
9 changes: 7 additions & 2 deletions apps/trip_plan/lib/trip_plan/api/open_trip_planner/builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule TripPlan.Api.OpenTripPlanner.Builder do
"fromPlace" => from_string,
"toPlace" => to_string,
"transportModes" => default_mode_string,
"walkReluctance" => 5,
"walkReluctance" => 15,
"locale" => "\"en\""
})
end
Expand Down Expand Up @@ -64,13 +64,18 @@ defmodule TripPlan.Api.OpenTripPlanner.Builder do
end

defp do_build_params([{:optimize_for, :less_walking} | rest], acc) do
do_build_params(rest, Map.put(acc, "walkReluctance", 17))
do_build_params(rest, Map.put(acc, "walkReluctance", 27))
end

defp do_build_params([{:optimize_for, :fewest_transfers} | rest], acc) do
do_build_params(rest, Map.put(acc, "transferPenalty", 100))
end

# param is used for testing, ignore
defp do_build_params([{:root_url, _} | rest], acc) do
do_build_params(rest, acc)
end

defp do_build_params([option | _], _) do
{:error, {:bad_param, option}}
end
Expand Down
28 changes: 28 additions & 0 deletions apps/trip_plan/test/api/open_trip_planner_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule TripPlan.Api.OpenTripPlannerTest do
use ExUnit.Case, async: false
import TripPlan.Api.OpenTripPlanner
alias TripPlan.NamedPosition

import Plug.Conn, only: [send_resp: 3]

Expand Down Expand Up @@ -28,4 +29,31 @@ defmodule TripPlan.Api.OpenTripPlannerTest do
assert expected == actual
end
end

test "does not normally add wiremock headers", %{bypass: bypass, url: url} do
Bypass.expect(bypass, fn conn ->
assert List.keyfind(conn.req_headers, "x-wm-proxy-url", 0) == nil
send_resp(conn, 404, ~s({"body": {}}))
end)

connection_opts = [user_id: 1, force_otp1: false, force_otp2: false]
from = %NamedPosition{name: "a", latitude: "42.13", longitude: "12.12313"}
to = %NamedPosition{name: "b", latitude: "42.13", longitude: "12.12313"}
plan(from, to, connection_opts, root_url: url)
end

test "adds headers when WIREMOCK_PROXY=true", %{bypass: bypass, url: url} do
System.put_env("WIREMOCK_PROXY", "true")

Bypass.expect(bypass, fn conn ->
assert List.keyfind(conn.req_headers, "x-wm-proxy-url", 0) != nil
send_resp(conn, 404, ~s({"body": {}}))
end)

connection_opts = [user_id: 1, force_otp1: false, force_otp2: false]
from = %NamedPosition{name: "a", latitude: "42.13", longitude: "12.12313"}
to = %NamedPosition{name: "b", latitude: "42.13", longitude: "12.12313"}
plan(from, to, connection_opts, root_url: url)
System.delete_env("WIREMOCK_PROXY")
end
end

0 comments on commit 6c5494a

Please sign in to comment.