Skip to content

Commit

Permalink
test: add tests for :encodings option
Browse files Browse the repository at this point in the history
  • Loading branch information
hauleth committed Sep 11, 2021
1 parent 413ecda commit f8e6047
Show file tree
Hide file tree
Showing 3 changed files with 254 additions and 27 deletions.
1 change: 1 addition & 0 deletions test/fixtures/static.txt.br
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BROTLIED HELLO
1 change: 1 addition & 0 deletions test/fixtures/static.txt.zst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ZSTANDARDED HELLO
279 changes: 252 additions & 27 deletions test/plug/static_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@ defmodule Plug.StaticTest do
use ExUnit.Case, async: true
use Plug.Test

defmodule MyPlug do
use Plug.Builder

plug Plug.Static,
at: "/public",
from: Path.expand("..", __DIR__),
gzip: true,
headers: %{"x-custom" => "x-value"},
content_types: %{"manifest-file" => "application/vnd.manifest+json"}

plug :passthrough

defp passthrough(conn, _), do: Plug.Conn.send_resp(conn, 404, "Passthrough")
@default_opts [
at: "/public",
from: Path.expand("..", __DIR__),
headers: %{"x-custom" => "x-value"},
content_types: %{"manifest-file" => "application/vnd.manifest+json"}
]

defp call(conn, opts \\ []) do
opts =
@default_opts
|> Keyword.merge(opts)
|> Plug.Static.init()

with %Plug.Conn{halted: false} = conn <- Plug.Static.call(conn, opts),
do: passthrough(conn)
end

defp call(conn), do: MyPlug.call(conn, [])
defp passthrough(conn), do: Plug.Conn.send_resp(conn, 404, "Passthrough")

test "serves the file" do
conn = call(conn(:get, "/public/fixtures/static.txt"))
Expand Down Expand Up @@ -55,7 +57,7 @@ defmodule Plug.StaticTest do
conn =
conn(:get, "/public/fixtures/static.txt", nil)
|> put_req_header("if-none-match", etag)
|> call
|> call(gzip: true)

assert conn.status == 304
assert conn.resp_body == ""
Expand All @@ -72,7 +74,7 @@ defmodule Plug.StaticTest do
conn =
conn(:get, "/public/fixtures/static.txt", [])
|> put_req_header("accept-encoding", "gzip")
|> call
|> call(gzip: true)

assert conn.status == 200
assert conn.resp_body == "GZIPPED HELLO"
Expand All @@ -89,7 +91,7 @@ defmodule Plug.StaticTest do
conn(:get, "/public/fixtures/static.txt", [])
|> put_req_header("accept-encoding", "gzip")
|> put_req_header("if-none-match", etag)
|> call
|> call(gzip: true)

assert conn.status == 304
assert conn.resp_body == ""
Expand Down Expand Up @@ -160,7 +162,7 @@ defmodule Plug.StaticTest do
headers: %{"x-custom" => "x-value"}
]

conn = Plug.Static.call(conn(:get, "/public/fixtures/static.txt"), Plug.Static.init(opts))
conn = call(conn(:get, "/public/fixtures/static.txt"), opts)

assert conn.status == 200
assert get_resp_header(conn, "cache-control") == ["max-age=0, private, must-revalidate"]
Expand Down Expand Up @@ -244,7 +246,7 @@ defmodule Plug.StaticTest do
conn =
conn(:get, "/public/fixtures/static.txt", [])
|> put_req_header("accept-encoding", "gzip")
|> call
|> call(gzip: true)

assert conn.status == 200
assert conn.resp_body == "GZIPPED HELLO"
Expand All @@ -256,7 +258,7 @@ defmodule Plug.StaticTest do
conn(:get, "/public/fixtures/static.txt", [])
|> put_req_header("accept-encoding", "*")
|> put_resp_header("vary", "Whatever")
|> call
|> call(gzip: true)

assert conn.status == 200
assert conn.resp_body == "GZIPPED HELLO"
Expand All @@ -269,7 +271,7 @@ defmodule Plug.StaticTest do
conn =
conn(:get, "/public/fixtures/static%20with%20spaces.txt", [])
|> put_req_header("accept-encoding", "gzip")
|> call
|> call(gzip: true)

assert conn.status == 200
assert conn.resp_body == "SPACES"
Expand Down Expand Up @@ -433,7 +435,7 @@ defmodule Plug.StaticTest do
assert conn.resp_body == ""
assert get_resp_header(conn, "cache-control") == ["public"]
assert get_resp_header(conn, "x-custom") == []
assert get_resp_header(conn, "vary") == ["Accept-Encoding"]
assert get_resp_header(conn, "vary") == []

assert get_resp_header(conn, "content-type") == []
assert get_resp_header(conn, "content-encoding") == []
Expand All @@ -450,7 +452,7 @@ defmodule Plug.StaticTest do
conn =
conn(:get, "/public/fixtures/static.txt")
|> put_req_header("range", "bytes=0-1")
|> Plug.Static.call(Plug.Static.init(opts))
|> call(opts)

assert conn.status == 206
assert conn.resp_body == "HE"
Expand All @@ -468,7 +470,7 @@ defmodule Plug.StaticTest do
conn(:get, "/public/fixtures/static.txt", nil)
|> put_req_header("range", "bytes=0-1")
|> put_req_header("if-none-match", etag)
|> Plug.Static.call(Plug.Static.init(opts))
|> call(opts)

assert conn.status == 304
assert conn.resp_body == ""
Expand Down Expand Up @@ -506,7 +508,7 @@ defmodule Plug.StaticTest do
conn =
conn(:get, "/public/fixtures/static.txt")
|> put_req_header("range", "bytes=0-1")
|> Plug.Static.call(Plug.Static.init(opts))
|> call(opts)

assert conn.status == 206
assert conn.resp_body == "HE"
Expand All @@ -518,6 +520,229 @@ defmodule Plug.StaticTest do
end
end

describe "encodings" do
test "serves file if no accept-encoding header" do
conn =
conn(:get, "/public/fixtures/static.txt", [])
|> call(
encodings: [
{"br", ".br"},
{"gzip", ".gz"}
]
)

assert conn.status == 200
assert conn.resp_body == "HELLO"
assert get_resp_header(conn, "content-encoding") == []
assert get_resp_header(conn, "vary") == ["Accept-Encoding"]
assert get_resp_header(conn, "x-custom") == ["x-value"]
end

test "serves file if unknown value in accept-encoding header" do
conn =
conn(:get, "/public/fixtures/static.txt", [])
|> put_req_header("accept-encoding", "zstd")
|> call(
encodings: [
{"br", ".br"},
{"gzip", ".gz"}
]
)

assert conn.status == 200
assert conn.resp_body == "HELLO"
assert get_resp_header(conn, "content-encoding") == []
assert get_resp_header(conn, "vary") == ["Accept-Encoding"]
assert get_resp_header(conn, "x-custom") == ["x-value"]
end

test "serves first known extension" do
conn =
conn(:get, "/public/fixtures/static.txt", [])
|> put_req_header("accept-encoding", "*")
|> call(
encodings: [
{"zstd", ".zst"},
{"br", ".br"},
{"gzip", ".gz"}
]
)

assert conn.status == 200
assert conn.resp_body == "ZSTANDARDED HELLO"
assert get_resp_header(conn, "content-encoding") == ["zstd"]
assert get_resp_header(conn, "vary") == ["Accept-Encoding"]
assert get_resp_header(conn, "x-custom") == ["x-value"]

req =
conn(:get, "/public/fixtures/static.txt", [])
|> put_req_header("accept-encoding", "zstd,br,gzip")

conn =
call(req,
encodings: [
{"zstd", ".zst"},
{"br", ".br"},
{"gzip", ".gz"}
]
)

assert conn.status == 200
assert conn.resp_body == "ZSTANDARDED HELLO"
assert get_resp_header(conn, "content-encoding") == ["zstd"]
assert get_resp_header(conn, "vary") == ["Accept-Encoding"]
assert get_resp_header(conn, "x-custom") == ["x-value"]

conn =
call(req,
encodings: [
{"br", ".br"},
{"gzip", ".gz"}
]
)

assert conn.status == 200
assert conn.resp_body == "BROTLIED HELLO"
assert get_resp_header(conn, "content-encoding") == ["br"]
assert get_resp_header(conn, "vary") == ["Accept-Encoding"]
assert get_resp_header(conn, "x-custom") == ["x-value"]

conn =
call(req,
encodings: [
{"gzip", ".gz"}
]
)

assert conn.status == 200
assert conn.resp_body == "GZIPPED HELLO"
assert get_resp_header(conn, "content-encoding") == ["gzip"]
assert get_resp_header(conn, "vary") == ["Accept-Encoding"]
assert get_resp_header(conn, "x-custom") == ["x-value"]

conn = call(req, encodings: [])

assert conn.status == 200
assert conn.resp_body == "HELLO"
assert get_resp_header(conn, "content-encoding") == []
assert get_resp_header(conn, "vary") == []
assert get_resp_header(conn, "x-custom") == ["x-value"]
end

test "serves gzipped file" do
conn =
conn(:get, "/public/fixtures/static.txt", [])
|> put_req_header("accept-encoding", "gzip")
|> call(encodings: [{"gzip", ".gz"}])

assert conn.status == 200
assert conn.resp_body == "GZIPPED HELLO"
assert get_resp_header(conn, "content-encoding") == ["gzip"]
assert get_resp_header(conn, "vary") == ["Accept-Encoding"]
assert get_resp_header(conn, "x-custom") == ["x-value"]

conn =
conn(:get, "/public/fixtures/static.txt", [])
|> put_req_header("accept-encoding", "*")
|> put_resp_header("vary", "Whatever")
|> call(encodings: [{"gzip", ".gz"}])

assert conn.status == 200
assert conn.resp_body == "GZIPPED HELLO"
assert get_resp_header(conn, "content-encoding") == ["gzip"]
assert get_resp_header(conn, "vary") == ["Accept-Encoding", "Whatever"]
assert get_resp_header(conn, "x-custom") == ["x-value"]
end

test "serves brotlied file" do
conn =
conn(:get, "/public/fixtures/static.txt", [])
|> put_req_header("accept-encoding", "br")
|> call(encodings: [{"br", ".br"}])

assert conn.status == 200
assert conn.resp_body == "BROTLIED HELLO"
assert get_resp_header(conn, "content-encoding") == ["br"]
assert get_resp_header(conn, "vary") == ["Accept-Encoding"]
assert get_resp_header(conn, "x-custom") == ["x-value"]

conn =
conn(:get, "/public/fixtures/static.txt", [])
|> put_req_header("accept-encoding", "*")
|> put_resp_header("vary", "Whatever")
|> call(encodings: [{"br", ".br"}])

assert conn.status == 200
assert conn.resp_body == "BROTLIED HELLO"
assert get_resp_header(conn, "content-encoding") == ["br"]
assert get_resp_header(conn, "vary") == ["Accept-Encoding", "Whatever"]
assert get_resp_header(conn, "x-custom") == ["x-value"]
end

test "serves zstandarded file" do
conn =
conn(:get, "/public/fixtures/static.txt", [])
|> put_req_header("accept-encoding", "zstd")
|> call(encodings: [{"zstd", ".zst"}])

assert conn.status == 200
assert conn.resp_body == "ZSTANDARDED HELLO"
assert get_resp_header(conn, "content-encoding") == ["zstd"]
assert get_resp_header(conn, "vary") == ["Accept-Encoding"]
assert get_resp_header(conn, "x-custom") == ["x-value"]

conn =
conn(:get, "/public/fixtures/static.txt", [])
|> put_req_header("accept-encoding", "*")
|> put_resp_header("vary", "Whatever")
|> call(encodings: [{"zstd", ".zst"}])

assert conn.status == 200
assert conn.resp_body == "ZSTANDARDED HELLO"
assert get_resp_header(conn, "content-encoding") == ["zstd"]
assert get_resp_header(conn, "vary") == ["Accept-Encoding", "Whatever"]
assert get_resp_header(conn, "x-custom") == ["x-value"]
end

test "old options still work" do
assert Plug.Static.init(at: "/", from: "/foo", gzip: true) ==
Plug.Static.init(at: "/", from: "/foo", encodings: [{"gzip", ".gz"}])

assert Plug.Static.init(at: "/", from: "/foo", brotli: true) ==
Plug.Static.init(at: "/", from: "/foo", encodings: [{"br", ".br"}])

assert Plug.Static.init(at: "/", from: "/foo", brotli: true, gzip: true) ==
Plug.Static.init(
at: "/",
from: "/foo",
encodings: [{"br", ".br"}, {"gzip", ".gz"}]
)

assert Plug.Static.init(at: "/", from: "/foo", gzip: true, brotli: true) ==
Plug.Static.init(
at: "/",
from: "/foo",
encodings: [{"br", ".br"}, {"gzip", ".gz"}]
)
end

test "old options values are appended to the given encodings list" do
assert Plug.Static.init(at: "/", from: "/foo", encodings: [{"zstd", ".zst"}], gzip: true) ==
Plug.Static.init(
at: "/",
from: "/foo",
encodings: [{"zstd", ".zst"}, {"gzip", ".gz"}]
)

assert Plug.Static.init(at: "/", from: "/foo", encodings: [{"zstd", ".zst"}], brotli: true) ==
Plug.Static.init(
at: "/",
from: "/foo",
encodings: [{"zstd", ".zst"}, {"br", ".br"}]
)
end
end

defmodule FilterPlug do
use Plug.Builder

Expand Down Expand Up @@ -566,7 +791,7 @@ defmodule Plug.StaticTest do
headers: {HeaderGenerator, :generate, [{"x-custom", "x-value"}]}
]

conn = Plug.Static.call(conn(:get, "/public/fixtures/static.txt"), Plug.Static.init(opts))
conn = call(conn(:get, "/public/fixtures/static.txt"), opts)

assert conn.status == 200
assert get_resp_header(conn, "x-custom") == ["x-value"]
Expand All @@ -580,7 +805,7 @@ defmodule Plug.StaticTest do

conn =
conn(:get, "/public/fixtures/static.txt")
|> Plug.Static.call(Plug.Static.init(opts))
|> call(opts)

assert conn.status == 200
end
Expand Down

0 comments on commit f8e6047

Please sign in to comment.