Skip to content

Commit

Permalink
compressed: Default compressed: false when streaming response body
Browse files Browse the repository at this point in the history
Closes #294
  • Loading branch information
wojtekmach committed Feb 23, 2024
1 parent 68e484f commit 8fea780
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/req/steps.ex
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,18 @@ defmodule Req.Steps do
Supported formats:
* `gzip`
* `br` (if [brotli] is installed)
* `zstd` (if [ezstd] is installed)
## Request Options
* `:compressed` - if set to `true`, sets the `accept-encoding` header with compression
algorithms that Req supports. Defaults to `true`.
When streaming response body (`into: fun | collectable`), `compressed` defaults to `false`.
## Examples
Req automatically decompresses response body (`decompress_body/1` step) so let's disable that by
Expand Down Expand Up @@ -322,7 +326,7 @@ defmodule Req.Steps do
[ezstd]: https://hex.pm/packages/ezstd
"""
@doc step: :request
def compressed(request) do
def compressed(%Req.Request{into: nil} = request) do
case Req.Request.get_option(request, :compressed, true) do
true ->
Req.Request.put_new_header(request, "accept-encoding", supported_accept_encoding())
Expand All @@ -332,6 +336,10 @@ defmodule Req.Steps do
end
end

def compressed(request) do
request
end

defmacrop brotli_loaded? do
Code.ensure_loaded?(:brotli)
end
Expand Down
12 changes: 12 additions & 0 deletions test/req/steps_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ defmodule Req.StepsTest do

## Request steps

describe "compressed" do
test "sets accept-encoding" do
req = Req.new() |> Req.Request.prepare()
assert req.headers["accept-encoding"] == ["zstd, br, gzip"]
end

test "does not set accept-encoding when streaming response body" do
req = Req.new(into: []) |> Req.Request.prepare()
refute req.headers["accept-encoding"]
end
end

describe "put_base_url" do
test "it works", c do
Bypass.expect(c.bypass, "GET", "", fn conn ->
Expand Down

0 comments on commit 8fea780

Please sign in to comment.