diff --git a/lib/sender_web/hls_plug.ex b/lib/sender_web/hls_plug.ex index 3a66693..8b9cd19 100644 --- a/lib/sender_web/hls_plug.ex +++ b/lib/sender_web/hls_plug.ex @@ -8,6 +8,11 @@ defmodule TribeOne.TribesPlugin.SenderWeb.HLSPlug do def init(opts), do: opts + def call(%{method: method, path_info: ["sender", "hls" | path_segments]} = conn, _opts) + when method in ["GET", "HEAD"] do + serve(conn, path_segments) + end + def call( %{method: method, path_info: ["tribe-one-sender", "hls" | path_segments]} = conn, _opts diff --git a/test/sender/hls_plug_test.exs b/test/sender/hls_plug_test.exs new file mode 100644 index 0000000..ff96d08 --- /dev/null +++ b/test/sender/hls_plug_test.exs @@ -0,0 +1,58 @@ +defmodule TribeOne.TribesPlugin.Sender.HLSPlugTest do + use ExUnit.Case, async: false + + import Plug.Conn + import Plug.Test + + alias TribeOne.TribesPlugin.Sender.{HLS, Stats} + alias TribeOne.TribesPlugin.SenderWeb.HLSPlug + + setup do + if Process.whereis(Stats) == nil do + start_supervised!(Stats) + end + + :ok = Stats.reset() + + root = + Path.join(System.tmp_dir!(), "sender-hls-plug-test-#{System.unique_integer([:positive])}") + + path = Path.join([root, "streams", "stream", "generation"]) + File.mkdir_p!(path) + File.write!(Path.join(path, "media.m3u8"), "#EXTM3U\n") + + previous = Application.get_env(:sender, HLS, []) + Application.put_env(:sender, HLS, Keyword.put(previous, :spool_root, root)) + + on_exit(fn -> + Application.put_env(:sender, HLS, previous) + File.rm_rf!(root) + end) + + :ok + end + + test "serves the current public sender HLS path" do + conn = + :get + |> conn("/sender/hls/streams/stream/generation/media.m3u8") + |> HLSPlug.call([]) + + assert conn.halted + assert conn.status == 200 + assert conn.resp_body == "#EXTM3U\n" + assert [content_type] = get_resp_header(conn, "content-type") + assert content_type =~ "application/vnd.apple.mpegurl" + end + + test "keeps the legacy plugin slug HLS path working" do + conn = + :get + |> conn("/tribe-one-sender/hls/streams/stream/generation/media.m3u8") + |> HLSPlug.call([]) + + assert conn.halted + assert conn.status == 200 + assert conn.resp_body == "#EXTM3U\n" + end +end