summaryrefslogtreecommitdiff
path: root/test/farside_test.exs
blob: b036fdca5f328da8cc8717bd585b6a5506e389e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
defmodule FarsideTest do

  use ExUnit.Case
  use Plug.Test

  alias Farside.Router

  @opts Router.init([])

  def test_conn(path) do
    :timer.sleep(1000)

    :get
    |> conn(path, "")
    |> Router.call(@opts)
  end

  test "throttle" do
    first_conn =
      :get
      |> conn("/", "")
      |> Router.call(@opts)

    first_redirect = elem(List.last(first_conn.resp_headers), 1)

    throttled_conn =
      :get
      |> conn("/", "")
      |> Router.call(@opts)

    throttled_redirect = elem(List.last(first_conn.resp_headers), 1)

    assert throttled_conn.state == :sent
    assert throttled_redirect == first_redirect
  end

  test "/" do
    conn = test_conn("/")
    assert conn.state == :sent
    assert conn.status == 200
  end

  test "/:service" do
    services_json = Application.fetch_env!(:farside, :services_json)
    {:ok, file} = File.read(services_json)
    {:ok, service_list} = Jason.decode(file)

    service_names =
      Enum.map(
        service_list,
        fn service -> service["type"] end
      )

    IO.puts("")

    service_names |>
    Enum.filter(fn service_name -> service_name != "nitter" end) |> 
    Enum.map(fn service_name ->
      conn = test_conn("/#{service_name}")
      first_redirect = elem(List.last(conn.resp_headers), 1)

      IO.puts("    /#{service_name} (#1) -- #{first_redirect}")
      assert conn.state == :set
      assert conn.status == 302

      conn = test_conn("/#{service_name}")
      second_redirect = elem(List.last(conn.resp_headers), 1)

      IO.puts("    /#{service_name} (#2) -- #{second_redirect}")
      assert conn.state == :set
      assert conn.status == 302
      assert first_redirect != second_redirect
    end)
  end

  test "/https://..." do
    parent_service = "https://www.youtube.com"
    parent_path = "watch?v=dQw4w9WgXcQ"
    conn = test_conn("/#{parent_service}/#{parent_path}")

    redirect = elem(List.last(conn.resp_headers), 1)

    IO.puts("")
    IO.puts("    /#{parent_service}/#{parent_path}")
    IO.puts("    redirected to")
    IO.puts("    #{redirect}")

    assert conn.state == :set
    assert conn.status == 302
    assert redirect =~ parent_path
    assert !(redirect =~ parent_service)
  end
end