|
| 1 | +defmodule Phoenix.Sync.Plug.CorsHeadersTest do |
| 2 | + use ExUnit.Case, async: true |
| 3 | + |
| 4 | + alias Phoenix.Sync.Plug.CORS |
| 5 | + |
| 6 | + import Plug.Test |
| 7 | + |
| 8 | + test "electric headers are up-to-date with current electric" do |
| 9 | + # in test we always have electric as a dependency so we can test |
| 10 | + # that our vendored headers are up-to-date |
| 11 | + assert CORS.electric_headers() == Electric.Shapes.Api.Response.electric_headers() |
| 12 | + end |
| 13 | + |
| 14 | + test "adds access-control-expose-headers header to response" do |
| 15 | + resp = |
| 16 | + conn(:get, "/sync/bananas") |
| 17 | + |> CORS.call(%{}) |
| 18 | + |
| 19 | + [expose] = Plug.Conn.get_resp_header(resp, "access-control-expose-headers") |
| 20 | + |
| 21 | + for header <- CORS.electric_headers() do |
| 22 | + assert String.contains?(expose, header) |
| 23 | + end |
| 24 | + |
| 25 | + assert String.contains?(expose, "transfer-encoding") |
| 26 | + end |
| 27 | + |
| 28 | + test "adds access-control-allow-origin header to response" do |
| 29 | + resp = |
| 30 | + conn(:get, "/v1/shape") |
| 31 | + |> CORS.call(%{}) |
| 32 | + |
| 33 | + ["*"] = Plug.Conn.get_resp_header(resp, "access-control-allow-origin") |
| 34 | + |
| 35 | + resp = |
| 36 | + conn(:get, "/large/noise") |
| 37 | + |> Plug.Conn.put_req_header("origin", "https://example.com") |
| 38 | + |> CORS.call(%{}) |
| 39 | + |
| 40 | + ["https://example.com"] = Plug.Conn.get_resp_header(resp, "access-control-allow-origin") |
| 41 | + end |
| 42 | + |
| 43 | + test "adds access-control-allow-methods header to response" do |
| 44 | + resp = |
| 45 | + conn(:get, "/my-shape") |
| 46 | + |> CORS.call(%{}) |
| 47 | + |
| 48 | + ["GET, POST, PUT, DELETE, OPTIONS"] = |
| 49 | + Plug.Conn.get_resp_header(resp, "access-control-allow-methods") |
| 50 | + end |
| 51 | +end |
0 commit comments