Skip to content

Commit

Permalink
fix(V3Api): encode url for v3 api requests (#1773)
Browse files Browse the repository at this point in the history
* fix(V3Api): escape characters for URL

This makes it work properly when resource IDs contain spaces and such.

* tests: add filter for all services query

* tests(V3Api): test URL encoding
  • Loading branch information
thecristen authored Oct 18, 2023
1 parent 5a2cdde commit 70f8143
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion apps/stops/test/api_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ defmodule Stops.ApiTest do
"#{child_id} not found in parent.child_ids"
end

assert {:ok, %Stop{} = child} = by_gtfs_id("South Station-01")
assert {:ok, %Stop{} = child} = by_gtfs_id("NEC-2287-01")
assert child.name == "South Station"
assert child.type == :stop
assert child.platform_name == "Commuter Rail - Track 1"
Expand Down
4 changes: 2 additions & 2 deletions apps/stops/test/repo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule Stops.RepoTest do

describe "get_parent/1" do
test "returns the parent stop for a child stop" do
north_station_cr = get("North Station")
north_station_cr = get("BNT-0000-01")
assert north_station_cr.is_child? == true
assert north_station_cr.parent_id == "place-north"
assert %Stop{id: "place-north"} = get_parent(north_station_cr)
Expand All @@ -43,7 +43,7 @@ defmodule Stops.RepoTest do
end

test "takes ids" do
assert %Stop{id: "place-north"} = get_parent("North Station")
assert %Stop{id: "place-north"} = get_parent("BNT-0000-01")
assert %Stop{id: "place-north"} = get_parent("place-north")
end
end
Expand Down
2 changes: 1 addition & 1 deletion apps/v3_api/lib/v3_api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ defmodule V3Api do
use_cache?: true
)

url = base_url <> url
url = base_url <> URI.encode(url)
timeout = Keyword.fetch!(opts, :timeout)

{time, response} =
Expand Down
4 changes: 2 additions & 2 deletions apps/v3_api/test/services_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ defmodule V3Api.ServicesTest do
@opts ["page[limit]": 1, sort: "id"]

describe "all/1" do
test "gets all services" do
assert %JsonApi{data: [%JsonApi.Item{}]} = Services.all(@opts)
test "gets all services (for a given filter)" do
assert %JsonApi{data: [%JsonApi.Item{}]} = Services.all(Keyword.put(@opts, :route, "Red"))
end
end

Expand Down
11 changes: 11 additions & 0 deletions apps/v3_api/test/v3_api_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ defmodule V3ApiTest do
refute response.data == %{}
end

test "encodes the URL", %{bypass: bypass, url: url} do
Bypass.expect(bypass, fn conn ->
assert conn.request_path == "/normal%20response"
send_resp(conn, 200, ~s({"data": []}))
end)

response = V3Api.get_json("/normal response", [], base_url: url)
assert %JsonApi{} = response
refute response.data == %{}
end

test "does not add headers normally", %{bypass: bypass, url: url} do
Bypass.expect(bypass, fn conn ->
assert List.keyfind(conn.req_headers, "x-wm-proxy-url", 0) == nil
Expand Down

0 comments on commit 70f8143

Please sign in to comment.