Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/maru/response.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ defmodule Maru.Response do
|> Plug.Conn.halt()
end

@doc """
Make gzipped json format response.
"""
def json_gzipped(%Plug.Conn{} = conn, data) do
json_library = Maru.json_library()

gzipped = data
|> json_library.encode_to_iodata!
|> :zlib.gzip

conn
|> Plug.Conn.put_resp_content_type("application/json")
|> Plug.Conn.put_resp_header("content-encoding", "gzip")
|> Plug.Conn.send_resp(conn.status || 200, gzipped)
|> Plug.Conn.halt()
end

@doc """
Make html format response.
"""
Expand Down
9 changes: 8 additions & 1 deletion lib/maru/test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ defmodule Maru.Test do
Get response from conn as json.
"""
def json_response(conn) do
Jason.decode!(conn.resp_body)
case Plug.Conn.get_resp_header(conn, "content-encoding") do
["gzip"] ->
conn.resp_body
|> :zlib.gunzip
|> Jason.decode!
[] ->
Jason.decode!(conn.resp_body)
end
end
end
21 changes: 19 additions & 2 deletions test/test_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,38 @@ defmodule Maru.TestTest do
post do
json(conn, params)
end

params do
requires :foo
end

put do
json_gzipped(conn, params)
end
end

defmodule TestTest3 do
use Maru.Test, server: Maru.TestTest.TestServer3

def test do
def test_post do
build_conn()
|> Plug.Conn.put_req_header("content-type", "application/json")
|> put_body_or_params(~s({"foo":"bar"}))
|> post("/")
|> json_response
end

def test_put do
build_conn()
|> Plug.Conn.put_req_header("content-type", "application/json")
|> put_body_or_params(~s({"foo":"bar"}))
|> put("/")
|> json_response
end
end

assert %{"foo" => "bar"} = TestTest3.test()
assert %{"foo" => "bar"} = TestTest3.test_post()
assert %{"foo" => "bar"} = TestTest3.test_put()
end

test "mounted" do
Expand Down