From 52ceddf4e24d2826b2328eb16d551b55d3f4b39b Mon Sep 17 00:00:00 2001 From: Yordis Prieto Lazo Date: Thu, 10 Aug 2023 16:07:09 -0400 Subject: [PATCH] feat: improve decompression middleware closes #598 --- lib/tesla/middleware.ex | 4 ++-- lib/tesla/middleware/compression.ex | 21 +++++++++++++++++++-- lib/tesla/middleware/json.ex | 4 ++++ mix.exs | 3 ++- test/support/test_support.ex | 7 +++++++ test/tesla/middleware/compression_test.exs | 12 ++++++++---- 6 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 test/support/test_support.ex diff --git a/lib/tesla/middleware.ex b/lib/tesla/middleware.ex index 660c26ae..4650d3e5 100644 --- a/lib/tesla/middleware.ex +++ b/lib/tesla/middleware.ex @@ -16,11 +16,11 @@ defmodule Tesla.Middleware do Tesla.client([{Tesla.Middleware.BaseUrl, "https://example.com"}]) ## Ordering - + The order in which middleware is defined matters. Note that the order when _sending_ the request matches the order the middleware was defined in, but the order when _receiving_ the response is reversed. - + For example, `Tesla.Middleware.DecompressResponse` must come _after_ `Tesla.Middleware.JSON`, otherwise the response isn't decompressed before it reaches the JSON parser. diff --git a/lib/tesla/middleware/compression.ex b/lib/tesla/middleware/compression.ex index c2cb24ad..d45aabf6 100644 --- a/lib/tesla/middleware/compression.ex +++ b/lib/tesla/middleware/compression.ex @@ -61,13 +61,30 @@ defmodule Tesla.Middleware.Compression do def decompress({:error, reason}), do: {:error, reason} def decompress(env) do + case decompress_body(env.body, Tesla.get_header(env, "content-encoding")) do + nil -> + env + + body -> + put_decompressed_body(env, body) + end + end + + defp put_decompressed_body(env, body) do + content_length = + body + |> byte_size() + |> to_string() + env - |> Tesla.put_body(decompress_body(env.body, Tesla.get_header(env, "content-encoding"))) + |> Tesla.put_body(body) + |> Tesla.delete_header("content-encoding") + |> Tesla.put_header("content-length", content_length) 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 + defp decompress_body(_body, _content_encoding), do: nil end defmodule Tesla.Middleware.CompressRequest do diff --git a/lib/tesla/middleware/json.ex b/lib/tesla/middleware/json.ex index 61095b93..b0e481f8 100644 --- a/lib/tesla/middleware/json.ex +++ b/lib/tesla/middleware/json.ex @@ -164,6 +164,8 @@ defmodule Tesla.Middleware.DecodeJson do """ @moduledoc since: "1.8.0" + @behaviour Tesla.Middleware + @impl Tesla.Middleware def call(env, next, opts) do opts = opts || [] @@ -180,6 +182,8 @@ defmodule Tesla.Middleware.EncodeJson do """ @moduledoc since: "1.8.0" + @behaviour Tesla.Middleware + @impl Tesla.Middleware def call(env, next, opts) do opts = opts || [] diff --git a/mix.exs b/mix.exs index 74e2641b..5a7d931c 100644 --- a/mix.exs +++ b/mix.exs @@ -20,7 +20,8 @@ defmodule Tesla.Mixfile do plt_add_apps: [:mix, :inets, :idna, :ssl_verify_fun, :ex_unit], plt_add_deps: :project ], - docs: docs() + docs: docs(), + preferred_cli_env: [coveralls: :test, "coveralls.html": :test] ] end diff --git a/test/support/test_support.ex b/test/support/test_support.ex new file mode 100644 index 00000000..cdd0f17d --- /dev/null +++ b/test/support/test_support.ex @@ -0,0 +1,7 @@ +defmodule TestSupport do + def gzip_headers(env) do + env.headers + |> Enum.map_join("|", fn {key, value} -> "#{key}: #{value}" end) + |> :zlib.gzip() + end +end diff --git a/test/tesla/middleware/compression_test.exs b/test/tesla/middleware/compression_test.exs index a82ecfe7..4d78d044 100644 --- a/test/tesla/middleware/compression_test.exs +++ b/test/tesla/middleware/compression_test.exs @@ -69,6 +69,7 @@ defmodule Tesla.Middleware.CompressionTest do test "decompress response body (gzip)" do assert {:ok, env} = CompressionResponseClient.get("/response-gzip") + assert env.headers == [{"content-type", "text/plain"}, {"content-length", "17"}] assert env.body == "decompressed gzip" end @@ -80,6 +81,7 @@ defmodule Tesla.Middleware.CompressionTest do test "return unchanged response for unsupported content-encoding" do assert {:ok, env} = CompressionResponseClient.get("/response-identity") assert env.body == "unchanged" + assert env.headers == [{"content-type", "text/plain"}, {"content-encoding", "identity"}] end defmodule CompressRequestDecompressResponseClient do @@ -114,7 +116,8 @@ defmodule Tesla.Middleware.CompressionTest do {status, headers, body} = case env.url do "/" -> - {200, [{"content-type", "text/plain"}, {"content-encoding", "gzip"}], env.headers} + {200, [{"content-type", "text/plain"}, {"content-encoding", "gzip"}], + TestSupport.gzip_headers(env)} end {:ok, %{env | status: status, headers: headers, body: body}} @@ -123,7 +126,7 @@ defmodule Tesla.Middleware.CompressionTest do test "Compression headers" do assert {:ok, env} = CompressionHeadersClient.get("/") - assert env.body == [{"accept-encoding", "gzip, deflate"}] + assert env.body == "accept-encoding: gzip, deflate" end defmodule DecompressResponseHeadersClient do @@ -135,7 +138,8 @@ defmodule Tesla.Middleware.CompressionTest do {status, headers, body} = case env.url do "/" -> - {200, [{"content-type", "text/plain"}, {"content-encoding", "gzip"}], env.headers} + {200, [{"content-type", "text/plain"}, {"content-encoding", "gzip"}], + TestSupport.gzip_headers(env)} end {:ok, %{env | status: status, headers: headers, body: body}} @@ -144,7 +148,7 @@ defmodule Tesla.Middleware.CompressionTest do test "Decompress response headers" do assert {:ok, env} = DecompressResponseHeadersClient.get("/") - assert env.body == [{"accept-encoding", "gzip, deflate"}] + assert env.body == "accept-encoding: gzip, deflate" end defmodule CompressRequestHeadersClient do