- Add 17 missing @spec annotations (layouts, error_json, error_html, skewt_svg) - Move 12+ nested alias/import/require to module top level - Add phx-change/id attributes to 11 raw HTML <form> tags - Remove 4 unused LiveView assigns (:bounds, :data_provider) - Add 3 missing doctest references (HrrrNativeClient, BulkFetch, Accounts) - Break 2 long lines (path_compute.ex:382) - Strengthen weak test assertions (is_binary→byte_size, is_list→!=[]) - Replace Module.concat with Module.safe_concat (2 occurrences) - Replace length/1 > 0 with list != [] (9 occurrences) - Remove no-op assert true, fix no-assertion tests Remaining: 24 socket.assigns introspection warnings (deliberate test pattern for observable behavior testing), 1 formatter-resistant long line, 3 app-code usage warnings.
60 lines
2.1 KiB
Elixir
60 lines
2.1 KiB
Elixir
defmodule MicrowavepropWeb.ContactMapControllerTest do
|
|
use MicrowavepropWeb.ConnCase, async: false
|
|
|
|
alias Microwaveprop.Cache
|
|
|
|
setup do
|
|
# The controller caches a gzipped payload under a module-level key —
|
|
# clear it between tests so gzip vs identity requests don't reuse
|
|
# each other's output.
|
|
Cache.invalidate({MicrowavepropWeb.ContactMapController, :gzipped_payload})
|
|
:ok
|
|
end
|
|
|
|
describe "GET /api/contacts/map" do
|
|
test "serves plain JSON when the client does not accept gzip", %{conn: conn} do
|
|
conn = get(conn, ~p"/api/contacts/map")
|
|
|
|
assert conn.status == 200
|
|
assert get_resp_header(conn, "content-type") == ["application/json; charset=utf-8"]
|
|
assert get_resp_header(conn, "content-encoding") == []
|
|
assert [cache_control] = get_resp_header(conn, "cache-control")
|
|
assert cache_control =~ "max-age=60"
|
|
|
|
# Body must be parseable JSON; on an empty dev/test DB that's a
|
|
# list (possibly empty) of contact tuples.
|
|
assert {:ok, parsed} = Jason.decode(conn.resp_body)
|
|
assert match?(x when is_list(x), parsed)
|
|
end
|
|
|
|
test "emits gzip-encoded body when the client advertises gzip support", %{conn: conn} do
|
|
conn =
|
|
conn
|
|
|> put_req_header("accept-encoding", "gzip, deflate")
|
|
|> get(~p"/api/contacts/map")
|
|
|
|
assert conn.status == 200
|
|
assert get_resp_header(conn, "content-encoding") == ["gzip"]
|
|
|
|
# The gzipped body decompresses to valid JSON.
|
|
decompressed = :zlib.gunzip(conn.resp_body)
|
|
assert {:ok, _} = Jason.decode(decompressed)
|
|
end
|
|
|
|
test "caches the gzipped payload across requests", %{conn: conn} do
|
|
conn1 =
|
|
conn
|
|
|> put_req_header("accept-encoding", "gzip")
|
|
|> get(~p"/api/contacts/map")
|
|
|
|
conn2 =
|
|
build_conn()
|
|
|> put_req_header("accept-encoding", "gzip")
|
|
|> get(~p"/api/contacts/map")
|
|
|
|
# Byte-identical bodies prove the second hit came from the cache,
|
|
# not a fresh Radio.contact_map_payload/0 + gzip round-trip.
|
|
assert conn1.resp_body == conn2.resp_body
|
|
end
|
|
end
|
|
end
|