|
| 1 | +defmodule JolaDevWeb.Plugs.OGImageTest do |
| 2 | + use JolaDevWeb.ConnCase, async: true |
| 3 | + alias JolaDevWeb.Plugs.OGImage |
| 4 | + |
| 5 | + describe "call/2" do |
| 6 | + test "serves a PNG for a static slug", %{conn: conn} do |
| 7 | + conn = |
| 8 | + conn |
| 9 | + |> Map.put(:request_path, "/images/og/home.png") |
| 10 | + |> OGImage.call([]) |
| 11 | + |
| 12 | + assert conn.status == 200 |
| 13 | + assert get_resp_header(conn, "content-type") == ["image/png; charset=utf-8"] |
| 14 | + assert <<137, "PNG\r\n", 26, "\n", _rest::binary>> = conn.resp_body |
| 15 | + assert conn.halted |
| 16 | + end |
| 17 | + |
| 18 | + test "serves a PNG for a known post slug", %{conn: conn} do |
| 19 | + post = List.first(JolaDev.Blog.all_posts()) |
| 20 | + |
| 21 | + conn = |
| 22 | + conn |
| 23 | + |> Map.put(:request_path, "/images/og/posts/#{post.id}.png") |
| 24 | + |> OGImage.call([]) |
| 25 | + |
| 26 | + assert conn.status == 200 |
| 27 | + assert <<137, "PNG\r\n", 26, "\n", _rest::binary>> = conn.resp_body |
| 28 | + end |
| 29 | + |
| 30 | + test "serves a PNG for a known tag slug", %{conn: conn} do |
| 31 | + tag = List.first(JolaDev.Blog.all_tags()) |
| 32 | + |
| 33 | + conn = |
| 34 | + conn |
| 35 | + |> Map.put(:request_path, "/images/og/posts/tag/#{tag}.png") |
| 36 | + |> OGImage.call([]) |
| 37 | + |
| 38 | + assert conn.status == 200 |
| 39 | + assert <<137, "PNG\r\n", 26, "\n", _rest::binary>> = conn.resp_body |
| 40 | + end |
| 41 | + |
| 42 | + test "falls through for an unknown OG slug", %{conn: conn} do |
| 43 | + original = Map.put(conn, :request_path, "/images/og/no-such-page.png") |
| 44 | + |
| 45 | + result = OGImage.call(original, []) |
| 46 | + |
| 47 | + assert result == original |
| 48 | + refute result.halted |
| 49 | + end |
| 50 | + |
| 51 | + test "passes through for paths outside /images/og/", %{conn: conn} do |
| 52 | + original = Map.put(conn, :request_path, "/posts/running-local-models-on-m4") |
| 53 | + |
| 54 | + result = OGImage.call(original, []) |
| 55 | + |
| 56 | + assert result == original |
| 57 | + refute result.halted |
| 58 | + end |
| 59 | + end |
| 60 | +end |
0 commit comments