Skip to content

Commit 612a289

Browse files
Add Plug.Static tests
* Add test that asserts Plug.Static.call/2 serves files from the app directory using a :from with an application name and directory. * Add test that asserts Plug.Static.init/1 preserves a two-tuple :from with an application name and directory. * Add test that asserts Plug.Static.init/1 transforms an application name :from to a two-tuple :from with the application name and a directory.
1 parent 90590ab commit 612a289

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

test/plug/static_test.exs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,29 @@ defmodule Plug.StaticTest do
313313
assert get_resp_header(conn, "vary") == ["Accept-Encoding"]
314314
end
315315

316+
test "call/2 serves files from the app directory using a :from with an application name and directory" do
317+
app_dir = Application.app_dir(:plug)
318+
fixture = Path.join(app_dir, "tmp.txt")
319+
320+
try do
321+
:ok = File.write!(fixture, "ABCD")
322+
323+
opts = [
324+
at: "priv/static",
325+
from: {:plug, "."}
326+
]
327+
328+
conn =
329+
conn(:get, "/priv/static/tmp.txt")
330+
|> call(opts)
331+
332+
assert conn.status == 200
333+
assert conn.resp_body == "ABCD"
334+
after
335+
:ok = File.rm!(fixture)
336+
end
337+
end
338+
316339
test "raises an exception if :from isn't a binary or an atom" do
317340
assert_raise ArgumentError, fn ->
318341
defmodule ExceptionPlug do
@@ -843,4 +866,16 @@ defmodule Plug.StaticTest do
843866

844867
assert conn.status == 200
845868
end
869+
870+
test "init/1 preserves a two-tuple :from with an application name and directory" do
871+
assert %{from: {:foo, "foo/bar/foobar"}} =
872+
Plug.Static.init(
873+
at: "/",
874+
from: {:foo, "foo/bar/foobar"}
875+
)
876+
end
877+
878+
test "init/1 transforms an application name :from to a two-tuple :from with the application name and a directory" do
879+
assert %{from: {:foo, "priv/static"}} = Plug.Static.init(at: "/", from: :foo)
880+
end
846881
end

0 commit comments

Comments
 (0)