-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests that passes data through Plug.Parsers (#16)
* test: add tests that passes data through Plug.Parsers This also adds tests for `identity` content encoding and for situation when there is no content encoding header at all. * fix: handle duplicated headers
- Loading branch information
Showing
12 changed files
with
418 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# Used by "mix format" | ||
[ | ||
import_deps: [:stream_data], | ||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
defmodule PlugCaisson.IdentityTest do | ||
use ExUnit.Case, async: true | ||
use Plug.Test | ||
use ExUnitProperties | ||
|
||
import TestUtils | ||
|
||
@content_type "identity" | ||
|
||
defp compress(data), do: data | ||
|
||
test "simple test" do | ||
raw = "Chrzęszczyrzewoszyckie chrząszcze chrobotliwie chrzeszczą w haszczach" | ||
data = compress(raw) | ||
conn = post(data, @content_type) | ||
|
||
assert raw == Enum.join(body_stream(conn)) | ||
end | ||
|
||
test "partial inflation" do | ||
raw = "Chrzęszczyrzewoszyckie chrząszcze chrobotliwie chrzeszczą w haszczach" | ||
|
||
data = compress(raw) | ||
conn = post(data, @content_type) | ||
|
||
assert raw == Enum.join(body_stream(conn, length: 10)) | ||
end | ||
|
||
property "data is returned as is" do | ||
check all raw <- binary() do | ||
data = compress(raw) | ||
conn = post(data, @content_type) | ||
|
||
assert raw == Enum.join(body_stream(conn)) | ||
end | ||
end | ||
|
||
property "length can be set to custom value" do | ||
check all raw <- binary(), | ||
length <- positive_integer() do | ||
data = compress(raw) | ||
conn = post(data, @content_type) | ||
|
||
assert raw == Enum.join(body_stream(conn, length: length)) | ||
end | ||
end | ||
|
||
test "Plug.Parser" do | ||
pipeline = | ||
pipeline([ | ||
{Plug.Parsers, | ||
parsers: [:json], json_decoder: Jason, body_reader: {PlugCaisson, :read_body, []}}, | ||
{&echo_plug/2, encoder: &Jason.encode!/1} | ||
]) | ||
|
||
value = for _i <- 1..2000, do: %{"hello" => "world"} | ||
payload = compress(Jason.encode!(value)) | ||
|
||
assert {200, _, body} = | ||
post({"application/json", payload}, @content_type) | ||
|> pipeline.() | ||
|> sent_resp() | ||
|
||
assert {:ok, %{"_json" => [%{"hello" => "world"} | _]}} = Jason.decode(body) | ||
end | ||
|
||
describe "corpus tests" do | ||
for {path, content} <- corpus() do | ||
test "#{path}" do | ||
raw = unquote(content) | ||
data = compress(raw) | ||
conn = post(data, @content_type) | ||
|
||
assert raw == Enum.join(body_stream(conn)) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
defmodule PlugCaisson.NoHeaderTest do | ||
use ExUnit.Case, async: true | ||
use Plug.Test | ||
use ExUnitProperties | ||
|
||
import TestUtils | ||
|
||
@content_type nil | ||
|
||
defp compress(data), do: data | ||
|
||
test "simple test" do | ||
raw = "Chrzęszczyrzewoszyckie chrząszcze chrobotliwie chrzeszczą w haszczach" | ||
data = compress(raw) | ||
conn = post(data, @content_type) | ||
|
||
assert raw == Enum.join(body_stream(conn)) | ||
end | ||
|
||
test "partial inflation" do | ||
raw = "Chrzęszczyrzewoszyckie chrząszcze chrobotliwie chrzeszczą w haszczach" | ||
|
||
data = compress(raw) | ||
conn = post(data, @content_type) | ||
|
||
assert raw == Enum.join(body_stream(conn, length: 10)) | ||
end | ||
|
||
property "data is returned as is" do | ||
check all raw <- binary() do | ||
data = compress(raw) | ||
conn = post(data, @content_type) | ||
|
||
assert raw == Enum.join(body_stream(conn)) | ||
end | ||
end | ||
|
||
property "length can be set to custom value" do | ||
check all raw <- binary(), | ||
length <- positive_integer() do | ||
data = compress(raw) | ||
conn = post(data, @content_type) | ||
|
||
assert raw == Enum.join(body_stream(conn, length: length)) | ||
end | ||
end | ||
|
||
test "Plug.Parser" do | ||
pipeline = | ||
pipeline([ | ||
{Plug.Parsers, | ||
parsers: [:json], json_decoder: Jason, body_reader: {PlugCaisson, :read_body, []}}, | ||
{&echo_plug/2, encoder: &Jason.encode!/1} | ||
]) | ||
|
||
value = for _i <- 1..2000, do: %{"hello" => "world"} | ||
payload = compress(Jason.encode!(value)) | ||
|
||
assert {200, _, body} = | ||
post({"application/json", payload}, @content_type) | ||
|> pipeline.() | ||
|> sent_resp() | ||
|
||
assert {:ok, %{"_json" => [%{"hello" => "world"} | _]}} = Jason.decode(body) | ||
end | ||
|
||
describe "corpus tests" do | ||
for {path, content} <- corpus() do | ||
test "#{path}" do | ||
raw = unquote(content) | ||
data = compress(raw) | ||
conn = post(data, @content_type) | ||
|
||
assert raw == Enum.join(body_stream(conn)) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.