Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F116039
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
6 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/lib/tesla/middleware/compression.ex b/lib/tesla/middleware/compression.ex
index 6c355c5..a4f5fd1 100644
--- a/lib/tesla/middleware/compression.ex
+++ b/lib/tesla/middleware/compression.ex
@@ -1,92 +1,94 @@
defmodule Tesla.Middleware.Compression do
@behaviour Tesla.Middleware
@moduledoc """
Compress requests and decompress responses.
Supports "gizp" and "deflate" encodings using erlang's built-in `:zlib` module.
### Example usage
```
defmodule MyClient do
use Tesla
plug Tesla.Middleware.Compression, format: "gzip"
end
```
### Options
- `:format` - request compression format, `"gzip"` (default) or `"defalte"`
"""
def call(env, next, opts) do
env
|> compress(opts)
|> Tesla.run(next)
|> decompress()
end
defp compressable?(body), do: is_binary(body)
@doc """
Compress request, used by `Tesla.Middleware.CompressRequest`
"""
def compress(env, opts) do
if compressable?(env.body) do
format = Keyword.get(opts || [], :format, "gzip")
env
- |> Map.update!(:body, &compress_body(&1, format))
+ |> Tesla.put_body(compress_body(env.body, format))
|> Tesla.put_headers([{"content-encoding", format}])
else
env
end
end
defp compress_body(body, "gzip"), do: :zlib.gzip(body)
defp compress_body(body, "deflate"), do: :zlib.zip(body)
@doc """
Decompress response, used by `Tesla.Middleware.DecompressResponse`
"""
+ def decompress({:ok, env}), do: {:ok, decompress(env)}
+ def decompress({:error, reasonn}), do: {:error, reasonn}
def decompress(env) do
env
- |> Map.update!(:body, &decompress_body(&1, Tesla.get_header(env, "content-encoding")))
+ |> Tesla.put_body(decompress_body(env.body, Tesla.get_header(env, "content-encoding")))
end
defp decompress_body(<<31, 139, 8, _::binary>> = body, "gzip"), do: :zlib.gunzip(body)
defp decompress_body(body, "deflate"), do: :zlib.unzip(body)
defp decompress_body(body, _content_encoding), do: body
end
defmodule Tesla.Middleware.CompressRequest do
@behaviour Tesla.Middleware
@moduledoc """
Only compress request.
See `Tesla.Middleware.Compression` for options.
"""
def call(env, next, opts) do
env
|> Tesla.Middleware.Compression.compress(opts)
|> Tesla.run(next)
end
end
defmodule Tesla.Middleware.DecompressResponse do
@behaviour Tesla.Middleware
@moduledoc """
Only decompress response.
See `Tesla.Middleware.Compression` for options.
"""
def call(env, next, _opts) do
env
|> Tesla.run(next)
|> Tesla.Middleware.Compression.decompress()
end
end
diff --git a/test/tesla/middleware/compression_test.exs b/test/tesla/middleware/compression_test.exs
index 69ca55a..6454e91 100644
--- a/test/tesla/middleware/compression_test.exs
+++ b/test/tesla/middleware/compression_test.exs
@@ -1,102 +1,107 @@
defmodule Tesla.Middleware.CompressionTest do
use ExUnit.Case
defmodule CompressionGzipRequestClient do
use Tesla
plug Tesla.Middleware.Compression
adapter fn env ->
{status, headers, body} =
case env.url do
"/" ->
{200, [{"content-type", "text/plain"}], :zlib.gunzip(env.body)}
end
- %{env | status: status, headers: headers, body: body}
+ {:ok, %{env | status: status, headers: headers, body: body}}
end
end
test "compress request body (gzip)" do
- assert CompressionGzipRequestClient.post("/", "compress request").body == "compress request"
+ assert {:ok, env} = CompressionGzipRequestClient.post("/", "compress request")
+ assert env.body == "compress request"
end
defmodule CompressionDeflateRequestClient do
use Tesla
plug Tesla.Middleware.Compression, format: "deflate"
adapter fn env ->
{status, headers, body} =
case env.url do
"/" ->
{200, [{"content-type", "text/plain"}], :zlib.unzip(env.body)}
end
- %{env | status: status, headers: headers, body: body}
+ {:ok, %{env | status: status, headers: headers, body: body}}
end
end
test "compress request body (deflate)" do
- assert CompressionDeflateRequestClient.post("/", "compress request").body ==
- "compress request"
+ assert {:ok, env} = CompressionDeflateRequestClient.post("/", "compress request")
+ assert env.body == "compress request"
end
defmodule CompressionResponseClient do
use Tesla
plug Tesla.Middleware.Compression
adapter fn env ->
{status, headers, body} =
case env.url do
"/response-gzip" ->
{200, [{"content-type", "text/plain"}, {"content-encoding", "gzip"}],
:zlib.gzip("decompressed gzip")}
"/response-deflate" ->
{200, [{"content-type", "text/plain"}, {"content-encoding", "deflate"}],
:zlib.zip("decompressed deflate")}
"/response-identity" ->
{200, [{"content-type", "text/plain"}, {"content-encoding", "identity"}], "unchanged"}
end
- %{env | status: status, headers: headers, body: body}
+ {:ok, %{env | status: status, headers: headers, body: body}}
end
end
test "decompress response body (gzip)" do
- assert CompressionResponseClient.get("/response-gzip").body == "decompressed gzip"
+ assert {:ok, env} = CompressionResponseClient.get("/response-gzip")
+ assert env.body == "decompressed gzip"
end
test "decompress response body (deflate)" do
- assert CompressionResponseClient.get("/response-deflate").body == "decompressed deflate"
+ assert {:ok, env} = CompressionResponseClient.get("/response-deflate")
+ assert env.body == "decompressed deflate"
end
test "return unchanged response for unsupported content-encoding" do
- assert CompressionResponseClient.get("/response-identity").body == "unchanged"
+ assert {:ok, env} = CompressionResponseClient.get("/response-identity")
+ assert env.body == "unchanged"
end
defmodule CompressRequestDecompressResponseClient do
use Tesla
plug Tesla.Middleware.CompressRequest
plug Tesla.Middleware.DecompressResponse
adapter fn env ->
{status, headers, body} =
case env.url do
"/" ->
{200, [{"content-type", "text/plain"}, {"content-encoding", "gzip"}], env.body}
end
- %{env | status: status, headers: headers, body: body}
+ {:ok, %{env | status: status, headers: headers, body: body}}
end
end
test "CompressRequest / DecompressResponse work without options" do
alias CompressRequestDecompressResponseClient, as: CRDRClient
- assert CRDRClient.post("/", "foo bar").body == "foo bar"
+ assert {:ok, env} = CRDRClient.post("/", "foo bar")
+ assert env.body == "foo bar"
end
end
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sat, Nov 30, 3:41 AM (1 d, 18 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41359
Default Alt Text
(6 KB)
Attached To
Mode
R28 tesla
Attached
Detach File
Event Timeline
Log In to Comment