Merge branch 'issue-86' into 'master'

:compression_threshold_size

Closes #86

See merge request honeyryderchuck/httpx!86
This commit is contained in:
HoneyryderChuck 2020-04-28 10:32:45 +00:00
commit 754bb6b5ed
2 changed files with 35 additions and 7 deletions

View File

@ -14,13 +14,23 @@ module HTTPX
#
module Compression
extend Registry
def self.load_dependencies(klass)
klass.plugin(:"compression/gzip")
klass.plugin(:"compression/deflate")
end
def self.extra_options(options)
options.merge(headers: { "accept-encoding" => Compression.registry.keys })
class << self
def load_dependencies(klass)
klass.plugin(:"compression/gzip")
klass.plugin(:"compression/deflate")
end
def extra_options(options)
Class.new(options.class) do
def_option(:compression_threshold_size) do |bytes|
bytes = Integer(bytes)
raise Error, ":expect_threshold_size must be positive" unless bytes.positive?
bytes
end
end.new(options).merge(headers: { "accept-encoding" => Compression.registry.keys })
end
end
module RequestMethods
@ -32,10 +42,16 @@ module HTTPX
end
module RequestBodyMethods
def initialize(*)
def initialize(*, options)
super
return if @body.nil?
if (threshold = options.compression_threshold_size)
unless unbounded_body?
return if @body.bytesize < threshold
end
end
@headers.get("content-encoding").each do |encoding|
next if encoding == "identity"

View File

@ -50,6 +50,18 @@ module Requests
assert compressed_data.bytesize < 8012, "body hasn't been compressed"
end
def test_plugin_compression_gzip_post_threshold_size
session = HTTPX.plugin(:compression, compression_threshold_size: 8015)
uri = build_uri("/post")
response = session.with_headers("content-encoding" => "gzip")
.post(uri, body: "a" * 8012)
verify_status(response, 200)
body = json_body(response)
verify_header(body["headers"], "Content-Type", "application/octet-stream")
compressed_data = body["data"]
assert compressed_data.bytesize == 8012, "body has been compressed when it shouldn't"
end
def test_plugin_compression_deflate
session = HTTPX.plugin(:compression)
uri = build_uri("/deflate")