Skip to content

Commit

Permalink
Correctif sur la disponibilité de la seule ressource hébergée sur Goo…
Browse files Browse the repository at this point in the history
…gle Drive (#4127)

* Add a note about #3464

* Implement hot-fix for Google Drive hosted content (#4122)

* Fix credo warning

* Mix format

* Tests pour redirection http de GDrive

---------

Co-authored-by: Frédéric Menou <[email protected]>
  • Loading branch information
thbar and ptitfred authored Aug 19, 2024
1 parent a73c308 commit f82e834
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
18 changes: 17 additions & 1 deletion apps/transport/lib/transport/availability_checker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ defmodule Transport.AvailabilityChecker do
# https://github.com/edgurgel/httpoison/issues/171#issuecomment-244029927
# https://github.com/etalab/transport-site/issues/3463
{:error, %HTTPoison.Error{reason: {:invalid_redirection, _}}} ->
# NOTE: this does not actually verifies that the target is available at the moment!
true

_ ->
Expand All @@ -49,7 +50,22 @@ defmodule Transport.AvailabilityChecker do
end

def available?(format, url, false) when is_binary(url) do
case http_client().head(url, [], follow_redirect: true) do
options = [follow_redirect: true]
# Hot-fix for https://github.com/etalab/transport-site/issues/4122
# Least intrusive fix, only pass `force_redirection` to `HTTPoison` if the param value is true, else
# do not specify it, to avoid side-effect.
# See: https://github.com/benoitc/hackney/blob/eca5fbb1ff2d84facefb2a633e00f6ca16e7ddfd/src/hackney_stream.erl#L173
# Google Drive content (1 instance at time of writing) returns a 303, and by default `hackney` only allows
# POST method for this, but here HEAD/GET are supported and required. By using `force_redirection` in `hackney`
# options, this indicated `hackney` that the redirect should still occur.
options =
if URI.parse(url).host == "drive.google.com" do
options |> Keyword.merge(hackney: [force_redirect: true])
else
options
end

case http_client().head(url, [], options) do
# See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#successful_responses
# Other 2xx status codes don't seem appropriate here
{:ok, %Response{status_code: 200}} ->
Expand Down
8 changes: 8 additions & 0 deletions apps/transport/test/transport/availability_checker_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ defmodule Transport.AvailabilityCheckerTest do
test_fallback_to_stream(403)
end

test "redirection for Google Drive" do
expect(Transport.HTTPoison.Mock, :head, fn _url, [], [follow_redirect: true, hackney: [force_redirect: true]] ->
{:ok, %HTTPoison.Response{status_code: 200}}
end)

assert AvailabilityChecker.available?("GTFS", "https://drive.google.com/test_url")
end

defp mock_head_with_status(status_code) do
expect(Transport.HTTPoison.Mock, :head, fn _url, [], [follow_redirect: true] ->
{:ok, %HTTPoison.Response{status_code: status_code}}
Expand Down

0 comments on commit f82e834

Please sign in to comment.