Skip to content

Commit 220deed

Browse files
committed
feat: improve decompression middleware
closes #598
1 parent cfee71b commit 220deed

File tree

5 files changed

+32
-7
lines changed

5 files changed

+32
-7
lines changed

lib/tesla/middleware.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ defmodule Tesla.Middleware do
1616
Tesla.client([{Tesla.Middleware.BaseUrl, "https://example.com"}])
1717
1818
## Ordering
19-
19+
2020
The order in which middleware is defined matters. Note that the order when _sending_ the request
2121
matches the order the middleware was defined in, but the order when _receiving_ the response
2222
is reversed.
23-
23+
2424
For example, `Tesla.Middleware.DecompressResponse` must come _after_ `Tesla.Middleware.JSON`,
2525
otherwise the response isn't decompressed before it reaches the JSON parser.
2626

lib/tesla/middleware/compression.ex

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,19 @@ defmodule Tesla.Middleware.Compression do
6161
def decompress({:error, reason}), do: {:error, reason}
6262

6363
def decompress(env) do
64+
body = decompress_body(env.body, Tesla.get_header(env, "content-encoding"))
65+
66+
content_length =
67+
body
68+
|> byte_size()
69+
|> to_string()
70+
6471
env
65-
|> Tesla.put_body(decompress_body(env.body, Tesla.get_header(env, "content-encoding")))
72+
|> Tesla.put_body(body)
73+
# what should happen if the content-encoding is not supported and the body
74+
# is not decompressed?
75+
|> Tesla.delete_header("content-encoding")
76+
|> Tesla.put_header("content-length", content_length)
6677
end
6778

6879
defp decompress_body(<<31, 139, 8, _::binary>> = body, "gzip"), do: :zlib.gunzip(body)

lib/tesla/middleware/json.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ defmodule Tesla.Middleware.DecodeJson do
164164
"""
165165
@moduledoc since: "1.8.0"
166166

167+
@behaviour Tesla.Middleware
168+
167169
@impl Tesla.Middleware
168170
def call(env, next, opts) do
169171
opts = opts || []
@@ -180,6 +182,8 @@ defmodule Tesla.Middleware.EncodeJson do
180182
"""
181183
@moduledoc since: "1.8.0"
182184

185+
@behaviour Tesla.Middleware
186+
183187
@impl Tesla.Middleware
184188
def call(env, next, opts) do
185189
opts = opts || []

test/support/test_support.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
defmodule TestSupport do
2+
def gzip_headers(env) do
3+
env.headers
4+
|> Enum.map_join("|", fn {key, value} -> "#{key}: #{value}" end)
5+
|> :zlib.gzip()
6+
end
7+
end

test/tesla/middleware/compression_test.exs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ defmodule Tesla.Middleware.CompressionTest do
6969

7070
test "decompress response body (gzip)" do
7171
assert {:ok, env} = CompressionResponseClient.get("/response-gzip")
72+
assert env.headers == [{"content-type", "text/plain"}, {"content-length", "17"}]
7273
assert env.body == "decompressed gzip"
7374
end
7475

@@ -114,7 +115,8 @@ defmodule Tesla.Middleware.CompressionTest do
114115
{status, headers, body} =
115116
case env.url do
116117
"/" ->
117-
{200, [{"content-type", "text/plain"}, {"content-encoding", "gzip"}], env.headers}
118+
{200, [{"content-type", "text/plain"}, {"content-encoding", "gzip"}],
119+
TestSupport.gzip_headers(env)}
118120
end
119121

120122
{:ok, %{env | status: status, headers: headers, body: body}}
@@ -123,7 +125,7 @@ defmodule Tesla.Middleware.CompressionTest do
123125

124126
test "Compression headers" do
125127
assert {:ok, env} = CompressionHeadersClient.get("/")
126-
assert env.body == [{"accept-encoding", "gzip, deflate"}]
128+
assert env.body == "accept-encoding: gzip, deflate"
127129
end
128130

129131
defmodule DecompressResponseHeadersClient do
@@ -135,7 +137,8 @@ defmodule Tesla.Middleware.CompressionTest do
135137
{status, headers, body} =
136138
case env.url do
137139
"/" ->
138-
{200, [{"content-type", "text/plain"}, {"content-encoding", "gzip"}], env.headers}
140+
{200, [{"content-type", "text/plain"}, {"content-encoding", "gzip"}],
141+
TestSupport.gzip_headers(env)}
139142
end
140143

141144
{:ok, %{env | status: status, headers: headers, body: body}}
@@ -144,7 +147,7 @@ defmodule Tesla.Middleware.CompressionTest do
144147

145148
test "Decompress response headers" do
146149
assert {:ok, env} = DecompressResponseHeadersClient.get("/")
147-
assert env.body == [{"accept-encoding", "gzip, deflate"}]
150+
assert env.body == "accept-encoding: gzip, deflate"
148151
end
149152

150153
defmodule CompressRequestHeadersClient do

0 commit comments

Comments
 (0)