mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-12-08 00:00:58 -05:00
moving default headers setup to options
this allows overriding the defaults too
This commit is contained in:
parent
20ec162751
commit
c9086a62c7
@ -13,6 +13,9 @@ module HTTPX
|
|||||||
CONNECT_TIMEOUT = READ_TIMEOUT = WRITE_TIMEOUT = 60
|
CONNECT_TIMEOUT = READ_TIMEOUT = WRITE_TIMEOUT = 60
|
||||||
REQUEST_TIMEOUT = OPERATION_TIMEOUT = nil
|
REQUEST_TIMEOUT = OPERATION_TIMEOUT = nil
|
||||||
|
|
||||||
|
# default value used for "user-agent" header, when not overridden.
|
||||||
|
USER_AGENT = "httpx.rb/#{VERSION}".freeze # rubocop:disable Style/RedundantFreeze
|
||||||
|
|
||||||
@options_names = []
|
@options_names = []
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
@ -144,6 +147,7 @@ module HTTPX
|
|||||||
instance_variable_set(:"@#{k}", value)
|
instance_variable_set(:"@#{k}", value)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
do_initialize
|
||||||
freeze
|
freeze
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -369,6 +373,8 @@ module HTTPX
|
|||||||
end
|
end
|
||||||
|
|
||||||
def option_headers(value)
|
def option_headers(value)
|
||||||
|
value = value.dup if value.frozen?
|
||||||
|
|
||||||
headers_class.new(value)
|
headers_class.new(value)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -395,6 +401,20 @@ module HTTPX
|
|||||||
Array(value)
|
Array(value)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# called after all options are initialized
|
||||||
|
def do_initialize
|
||||||
|
hs = @headers
|
||||||
|
|
||||||
|
# initialized default request headers
|
||||||
|
hs["user-agent"] = USER_AGENT unless hs.key?("user-agent")
|
||||||
|
hs["accept"] = "*/*" unless hs.key?("accept")
|
||||||
|
if hs.key?("range")
|
||||||
|
hs.delete("accept-encoding")
|
||||||
|
else
|
||||||
|
hs["accept-encoding"] = supported_compression_formats unless hs.key?("accept-encoding")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def access_option(obj, k, ivar_map)
|
def access_option(obj, k, ivar_map)
|
||||||
case obj
|
case obj
|
||||||
when Hash
|
when Hash
|
||||||
|
|||||||
@ -14,9 +14,6 @@ module HTTPX
|
|||||||
|
|
||||||
ALLOWED_URI_SCHEMES = %w[https http].freeze
|
ALLOWED_URI_SCHEMES = %w[https http].freeze
|
||||||
|
|
||||||
# default value used for "user-agent" header, when not overridden.
|
|
||||||
USER_AGENT = "httpx.rb/#{VERSION}".freeze # rubocop:disable Style/RedundantFreeze
|
|
||||||
|
|
||||||
# the upcased string HTTP verb for this request.
|
# the upcased string HTTP verb for this request.
|
||||||
attr_reader :verb
|
attr_reader :verb
|
||||||
|
|
||||||
@ -75,16 +72,6 @@ module HTTPX
|
|||||||
@headers = options.headers.dup
|
@headers = options.headers.dup
|
||||||
merge_headers(params.delete(:headers)) if params.key?(:headers)
|
merge_headers(params.delete(:headers)) if params.key?(:headers)
|
||||||
|
|
||||||
@headers["user-agent"] ||= USER_AGENT
|
|
||||||
@headers["accept"] ||= "*/*"
|
|
||||||
|
|
||||||
# forego compression in the Range request case
|
|
||||||
if @headers.key?("range")
|
|
||||||
@headers.delete("accept-encoding")
|
|
||||||
else
|
|
||||||
@headers["accept-encoding"] ||= options.supported_compression_formats
|
|
||||||
end
|
|
||||||
|
|
||||||
@query_params = params.delete(:params) if params.key?(:params)
|
@query_params = params.delete(:params) if params.key?(:params)
|
||||||
|
|
||||||
@body = options.request_body_class.new(@headers, options, **params)
|
@body = options.request_body_class.new(@headers, options, **params)
|
||||||
@ -166,6 +153,9 @@ module HTTPX
|
|||||||
# merges +h+ into the instance of HTTPX::Headers of the request.
|
# merges +h+ into the instance of HTTPX::Headers of the request.
|
||||||
def merge_headers(h)
|
def merge_headers(h)
|
||||||
@headers = @headers.merge(h)
|
@headers = @headers.merge(h)
|
||||||
|
return unless @headers.key?("range")
|
||||||
|
|
||||||
|
@headers.delete("accept-encoding")
|
||||||
end
|
end
|
||||||
|
|
||||||
# the URI scheme of the request +uri+.
|
# the URI scheme of the request +uri+.
|
||||||
|
|||||||
@ -17,6 +17,7 @@ module HTTPX
|
|||||||
|
|
||||||
DEFAULT_OPTIONS: Hash[Symbol, untyped]
|
DEFAULT_OPTIONS: Hash[Symbol, untyped]
|
||||||
REQUEST_BODY_IVARS: Array[Symbol]
|
REQUEST_BODY_IVARS: Array[Symbol]
|
||||||
|
USER_AGENT: String
|
||||||
|
|
||||||
type timeout_type = :connect_timeout | :settings_timeout | :close_handshake_timeout | :operation_timeout | :keep_alive_timeout | :read_timeout | :write_timeout | :request_timeout
|
type timeout_type = :connect_timeout | :settings_timeout | :close_handshake_timeout | :operation_timeout | :keep_alive_timeout | :read_timeout | :write_timeout | :request_timeout
|
||||||
type timeout = Hash[timeout_type, Numeric?]
|
type timeout = Hash[timeout_type, Numeric?]
|
||||||
@ -146,6 +147,8 @@ module HTTPX
|
|||||||
|
|
||||||
def initialize: (?options options) -> void
|
def initialize: (?options options) -> void
|
||||||
|
|
||||||
|
def do_initialize: (?options options) -> void
|
||||||
|
|
||||||
def access_option: (Hash[Symbol, untyped] | Object | nil obj, Symbol k, Hash[Symbol, Symbol]? ivar_map) -> untyped
|
def access_option: (Hash[Symbol, untyped] | Object | nil obj, Symbol k, Hash[Symbol, Symbol]? ivar_map) -> untyped
|
||||||
|
|
||||||
# integer
|
# integer
|
||||||
|
|||||||
@ -1,14 +0,0 @@
|
|||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
require_relative "test_helper"
|
|
||||||
|
|
||||||
class CompressionTest < Minitest::Test
|
|
||||||
include HTTPX
|
|
||||||
|
|
||||||
def test_ignore_encoding_on_range
|
|
||||||
request = HTTPX::Session.new.build_request("GET", "http://example.com")
|
|
||||||
assert request.headers.key?("accept-encoding")
|
|
||||||
range_request = HTTPX::Session.new.build_request("GET", "http://example.com", headers: { "range" => "bytes=100-200" })
|
|
||||||
assert !range_request.headers.key?("accept-encoding")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@ -24,9 +24,12 @@ class OptionsTest < Minitest::Test
|
|||||||
|
|
||||||
def test_options_headers
|
def test_options_headers
|
||||||
opt1 = Options.new
|
opt1 = Options.new
|
||||||
assert opt1.headers.to_a.empty?, "headers should be empty"
|
assert opt1.headers["user-agent"].start_with?("httpx"), "should set default user-agent"
|
||||||
opt2 = Options.new(:headers => { "accept" => "*/*" })
|
assert opt1.headers["accept"], "should set default accet"
|
||||||
assert opt2.headers.to_a == [%w[accept */*]], "headers are unexpected"
|
assert opt1.headers.to_a.include?(%w[accept */*]), "accept headers are unexpected"
|
||||||
|
opt2 = Options.new(:headers => { "user-agent" => nil })
|
||||||
|
assert opt2.headers["user-agent"] == "", "should remove default user-agent"
|
||||||
|
assert opt2.headers.to_a.include?(%w[accept */*]), "accept headers are unexpected"
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_options_headers_with_instance
|
def test_options_headers_with_instance
|
||||||
@ -118,7 +121,13 @@ class OptionsTest < Minitest::Test
|
|||||||
:supported_compression_formats => %w[gzip deflate],
|
:supported_compression_formats => %w[gzip deflate],
|
||||||
:compress_request_body => true,
|
:compress_request_body => true,
|
||||||
:decompress_response_body => true,
|
:decompress_response_body => true,
|
||||||
:headers => { "accept" => "xml", "foo" => "foo", "bar" => "bar" },
|
:headers => {
|
||||||
|
"accept" => "xml",
|
||||||
|
"foo" => "foo",
|
||||||
|
"bar" => "bar",
|
||||||
|
"user-agent" => "httpx.rb/#{HTTPX::VERSION}",
|
||||||
|
"accept-encoding" => "gzip, deflate",
|
||||||
|
},
|
||||||
:max_concurrent_requests => nil,
|
:max_concurrent_requests => nil,
|
||||||
:request_class => bar.request_class,
|
:request_class => bar.request_class,
|
||||||
:response_class => bar.response_class,
|
:response_class => bar.response_class,
|
||||||
|
|||||||
@ -107,6 +107,18 @@ module Requests
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_compression_ignore_encoding_on_range
|
||||||
|
uri = build_uri("/get")
|
||||||
|
response = HTTPX.get(uri)
|
||||||
|
verify_status(response, 200)
|
||||||
|
body = json_body(response)
|
||||||
|
assert body["headers"].key?("Accept-Encoding")
|
||||||
|
|
||||||
|
response = HTTPX.get(uri, headers: { "range" => "bytes=100-200" })
|
||||||
|
body = json_body(response)
|
||||||
|
assert !body["headers"].key?("Accept-Encoding")
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def inflate_test_data(string)
|
def inflate_test_data(string)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user