Skip to content

Commit

Permalink
Request.verb is now an upcased string (ex: "GET")
Browse files Browse the repository at this point in the history
The reference for a request verb is now the string which is used
everywhere else, instead of the symbol corresponding to it. This was an
artifact from the import from httprb, and there is no advantage in it,
since these strings are frozen in most use cases, and the
transformations from symbol to strings being performed everywhere are
prooof that keeping the atom isn't really bringing any benefit.
  • Loading branch information
HoneyryderChuck committed Apr 17, 2023
1 parent dd3fb5e commit 092e594
Show file tree
Hide file tree
Showing 45 changed files with 129 additions and 125 deletions.
2 changes: 1 addition & 1 deletion integration_tests/datadog_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_datadog_successful_multiple_requests
set_datadog
uri = URI(build_uri("/status/200", "http://#{httpbin}"))

get_response, post_response = HTTPX.request([[:get, uri], [:post, uri]])
get_response, post_response = HTTPX.request([["GET", uri], ["POST", uri]])
verify_status(get_response, 200)
verify_status(post_response, 200)

Expand Down
2 changes: 1 addition & 1 deletion lib/httpx/adapters/datadog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def call

@request.on(:response, &method(:finish))

verb = @request.verb.to_s.upcase
verb = @request.verb
uri = @request.uri

@span = build_span
Expand Down
2 changes: 1 addition & 1 deletion lib/httpx/adapters/faraday.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def build_request(env)
headers: env.request_headers,
body: env.body,
}
[meth, env.url, request_options]
[meth.to_s.upcase, env.url, request_options]
end

def options_from_env(env)
Expand Down
2 changes: 1 addition & 1 deletion lib/httpx/adapters/sentry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def extract_request_info(req)
uri = req.uri

result = {
method: req.verb.to_s.upcase,
method: req.verb,
}

if ::Sentry.configuration.send_default_pii
Expand Down
2 changes: 1 addition & 1 deletion lib/httpx/adapters/webmock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def build_webmock_request_signature(request)
uri.path = uri.normalized_path.gsub("[^:]//", "/")

WebMock::RequestSignature.new(
request.verb,
request.verb.downcase.to_sym,
uri.to_s,
body: request.body.each.to_a.join,
headers: request.headers.to_h
Expand Down
8 changes: 4 additions & 4 deletions lib/httpx/chainable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

module HTTPX
module Chainable
%i[head get post put delete trace options connect patch].each do |meth|
%w[head get post put delete trace options connect patch].each do |meth|
class_eval(<<-MOD, __FILE__, __LINE__ + 1)
def #{meth}(*uri, **options) # def get(*uri, **options)
request(:#{meth}, uri, **options) # request(:get, uri, **options)
end # end
def #{meth}(*uri, **options) # def get(*uri, **options)
request("#{meth.upcase}", uri, **options) # request("GET", uri, **options)
end # end
MOD
end

Expand Down
2 changes: 1 addition & 1 deletion lib/httpx/connection/http1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ def handle(request)
end

def join_headline(request)
"#{request.verb.to_s.upcase} #{request.path} HTTP/#{@version.join(".")}"
"#{request.verb} #{request.path} HTTP/#{@version.join(".")}"
end

def join_headers(request)
Expand Down
2 changes: 1 addition & 1 deletion lib/httpx/connection/http2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def handle_stream(stream, request)
def set_protocol_headers(request)
{
":scheme" => request.scheme,
":method" => request.verb.to_s.upcase,
":method" => request.verb,
":path" => request.path,
":authority" => request.authority,
}
Expand Down
2 changes: 1 addition & 1 deletion lib/httpx/plugins/authentication/digest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def can_authenticate?(authenticate)
end

def authenticate(request, authenticate)
"Digest #{generate_header(request.verb.to_s.upcase, request.path, authenticate)}"
"Digest #{generate_header(request.verb, request.path, authenticate)}"
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/httpx/plugins/aws_sigv4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def sign!(request)
end.join

# canonical request
creq = "#{request.verb.to_s.upcase}" \
creq = "#{request.verb}" \
"\n#{request.canonical_path}" \
"\n#{request.canonical_query}" \
"\n#{canonical_headers}" \
Expand Down
2 changes: 1 addition & 1 deletion lib/httpx/plugins/follow_redirects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def build_redirect_request(request, response, options)
max_redirects: max_redirects - 1)
end

build_request(:get, redirect_uri, retry_options)
build_request("GET", redirect_uri, retry_options)
end

def __get_location_from_response(response)
Expand Down
2 changes: 1 addition & 1 deletion lib/httpx/plugins/grpc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def build_grpc_request(rpc_method, input, deadline:, metadata: nil, **)
Message.encode(input, deflater: deflater)
end

build_request(:post, uri, headers: headers, body: body)
build_request("POST", uri, headers: headers, body: body)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/httpx/plugins/h2c.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Plugins
# https://gitlab.com/os85/httpx/wikis/Upgrade#h2c
#
module H2C
VALID_H2C_VERBS = %i[get options head].freeze
VALID_H2C_VERBS = %w[GET OPTIONS HEAD].freeze

class << self
def load_dependencies(*)
Expand Down
6 changes: 3 additions & 3 deletions lib/httpx/plugins/proxy/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ def __http_on_connect(request, response)

module ProxyParser
def join_headline(request)
return super if request.verb == :connect
return super if request.verb == "CONNECT"

"#{request.verb.to_s.upcase} #{request.uri} HTTP/#{@version.join(".")}"
"#{request.verb} #{request.uri} HTTP/#{@version.join(".")}"
end

def set_protocol_headers(request)
Expand All @@ -161,7 +161,7 @@ def set_protocol_headers(request)

class ConnectRequest < Request
def initialize(uri, _options)
super(:connect, uri, {})
super("CONNECT", uri, {})
@headers.delete("accept")
end

Expand Down
4 changes: 2 additions & 2 deletions lib/httpx/plugins/response_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Plugins
# https://gitlab.com/os85/httpx/wikis/Response-Cache
#
module ResponseCache
CACHEABLE_VERBS = %i[get head].freeze
CACHEABLE_VERBS = %w[GET HEAD].freeze
CACHEABLE_STATUS_CODES = [200, 203, 206, 300, 301, 410].freeze
private_constant :CACHEABLE_VERBS
private_constant :CACHEABLE_STATUS_CODES
Expand Down Expand Up @@ -96,7 +96,7 @@ def fetch_response(request, *)

module RequestMethods
def response_cache_key
@response_cache_key ||= Digest::SHA1.hexdigest("httpx-response-cache-#{@verb}#{@uri}")
@response_cache_key ||= Digest::SHA1.hexdigest("httpx-response-cache-#{@verb}-#{@uri}")
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/httpx/plugins/retries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Retries
MAX_RETRIES = 3
# TODO: pass max_retries in a configure/load block

IDEMPOTENT_METHODS = %i[get options head put delete].freeze
IDEMPOTENT_METHODS = %w[GET OPTIONS HEAD PUT DELETE].freeze
RETRYABLE_ERRORS = [
IOError,
EOFError,
Expand Down
14 changes: 7 additions & 7 deletions lib/httpx/plugins/webdav.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ module Plugins
module WebDav
module InstanceMethods
def copy(src, dest)
request(:copy, src, headers: { "destination" => @options.origin.merge(dest) })
request("COPY", src, headers: { "destination" => @options.origin.merge(dest) })
end

def move(src, dest)
request(:move, src, headers: { "destination" => @options.origin.merge(dest) })
request("MOVE", src, headers: { "destination" => @options.origin.merge(dest) })
end

def lock(path, timeout: nil, &blk)
Expand All @@ -30,7 +30,7 @@ def lock(path, timeout: nil, &blk)
"<D:locktype><D:write/></D:locktype>" \
"<D:owner>null</D:owner>" \
"</D:lockinfo>"
response = request(:lock, path, headers: headers, xml: xml)
response = request("LOCK", path, headers: headers, xml: xml)

return response unless response.is_a?(Response)

Expand All @@ -46,11 +46,11 @@ def lock(path, timeout: nil, &blk)
end

def unlock(path, lock_token)
request(:unlock, path, headers: { "lock-token" => lock_token })
request("UNLOCK", path, headers: { "lock-token" => lock_token })
end

def mkcol(dir)
request(:mkcol, dir)
request("MKCOL", dir)
end

def propfind(path, xml = nil)
Expand All @@ -64,13 +64,13 @@ def propfind(path, xml = nil)
xml
end

request(:propfind, path, headers: { "depth" => "1" }, xml: body)
request("PROPFIND", path, headers: { "depth" => "1" }, xml: body)
end

def proppatch(path, xml)
body = "<?xml version=\"1.0\"?>" \
"<D:propertyupdate xmlns:D=\"DAV:\" xmlns:Z=\"http://ns.example.com/standards/z39.50/\">#{xml}</D:propertyupdate>"
request(:proppatch, path, xml: body)
request("PROPPATCH", path, xml: body)
end
# %i[ orderpatch acl report search]
end
Expand Down
8 changes: 6 additions & 2 deletions lib/httpx/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ class Request
def_delegator :@body, :empty?

def initialize(verb, uri, options = {})
@verb = verb.to_s.downcase.to_sym
if verb.is_a?(Symbol)
warn "DEPRECATION WARNING: Using symbols for `verb` is deprecated, and will not be supported in httpx 1.0. " \
"Use \"#{verb.to_s.upcase}\" instead."
end
@verb = verb.to_s.upcase
@options = Options.new(options)
@uri = Utils.to_uri(uri)
if @uri.relative?
Expand Down Expand Up @@ -138,7 +142,7 @@ def drain_body
# :nocov:
def inspect
"#<HTTPX::Request:#{object_id} " \
"#{@verb.to_s.upcase} " \
"#{@verb} " \
"#{uri} " \
"@headers=#{@headers} " \
"@body=#{@body}>"
Expand Down
4 changes: 2 additions & 2 deletions lib/httpx/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ def finish!
end

def bodyless?
@request.verb == :head ||
@request.verb == "HEAD" ||
no_data?
end

def complete?
bodyless? || (@request.verb == :connect && @status == 200)
bodyless? || (@request.verb == "CONNECT" && @status == 200)
end

# :nocov:
Expand Down
2 changes: 1 addition & 1 deletion regression_tests/bug_0_14_5_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_http2_post_with_concurrent_post_requests_with_large_payload_blocking
http.get("https://#{httpbin}/get")

requests = 2.times.map do
http.build_request(:post, post_uri, body: "a" * (1 << 16)) # 65k body, must be above write buffer size
http.build_request("POST", post_uri, body: "a" * (1 << 16)) # 65k body, must be above write buffer size
end

responses = http.request(*requests)
Expand Down
6 changes: 3 additions & 3 deletions sig/chainable.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ module HTTPX
module Chainable
def request: (*Request, **untyped) -> Array[response]
| (Request, **untyped) -> response
| (verb | string, uri | [uri], **untyped) -> response
| (Array[[verb | string, uri] | [verb | string, uri, options]], **untyped) -> Array[response]
| (verb | string, _Each[uri | [uri, options]], **untyped) -> Array[response]
| (verb, uri | [uri], **untyped) -> response
| (Array[[verb, uri] | [verb, uri, options]], **untyped) -> Array[response]
| (verb, _Each[uri | [uri, options]], **untyped) -> Array[response]

def accept: (String) -> Session
def wrap: () { (Session) -> void } -> void
Expand Down
6 changes: 3 additions & 3 deletions sig/httpx.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ module HTTPX
type uri = URI::HTTP | URI::HTTPS | string
type generic_uri = String | URI::Generic

type verb = :options | :get | :head | :post | :put | :delete | :trace | :connect |
:propfind | :proppatch | :mkcol | :copy | :move | :lock | :unlock | :orderpatch |
:acl | :report | :patch | :search
type verb = "OPTIONS" | "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "TRACE" | "CONNECT" |
"PROPFIND" | "PROPPATCH" | "MKCOL" | "COPY" | "MOVE" | "LOCK" | "UNLOCK" | "ORDERPATCH" |
"ACL" | "REPORT" | "PATCH" | "SEARCH"

type ip_family = Integer #Socket::AF_INET6 | Socket::AF_INET

Expand Down
2 changes: 1 addition & 1 deletion sig/plugins/h2c.rbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module HTTPX
module Plugins
module H2C
VALID_H2C_VERBS: Array[Symbol]
VALID_H2C_VERBS: Array[verb]

def self.load_dependencies: (*untyped) -> void
def self.configure: (singleton(Session)) -> void
Expand Down
2 changes: 1 addition & 1 deletion sig/plugins/response_cache.rbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module HTTPX
module Plugins
module ResponseCache
CACHEABLE_VERBS: Array[Symbol]
CACHEABLE_VERBS: Array[verb]

def self?.cacheable_request?: (Request request) -> bool
def self?.cacheable_response?: (response response) -> bool
Expand Down
2 changes: 1 addition & 1 deletion sig/request.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module HTTPX
METHODS: Array[Symbol]
USER_AGENT: String

attr_reader verb: Symbol
attr_reader verb: verb
attr_reader uri: URI::Generic
attr_reader headers: Headers
attr_reader body: Body
Expand Down
12 changes: 6 additions & 6 deletions sig/session.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module HTTPX

def close: (*untyped) -> void

def build_request: (String | verb, generic_uri, ?options) -> Request
def build_request: (verb, generic_uri, ?options) -> Request

private

Expand All @@ -35,11 +35,11 @@ module HTTPX

def build_altsvc_connection: (Connection existing_connection, Array[Connection] connections, URI::Generic alt_origin, String origin, Hash[String, String] alt_params, Options options) -> Connection?

def build_requests: (verb | string, uri, options) -> Array[Request]
| (Array[[verb | string, uri, options]], options) -> Array[Request]
| (Array[[verb | string, uri]], options) -> Array[Request]
| (verb | string, _Each[[uri, options]], Options) -> Array[Request]
| (verb | string, _Each[uri], options) -> Array[Request]
def build_requests: (verb, uri, options) -> Array[Request]
| (Array[[verb, uri, options]], options) -> Array[Request]
| (Array[[verb, uri]], options) -> Array[Request]
| (verb, _Each[[uri, options]], Options) -> Array[Request]
| (verb, _Each[uri], options) -> Array[Request]

def build_connection: (URI::Generic, Options) -> Connection

Expand Down
2 changes: 1 addition & 1 deletion standalone_tests/response_json_multi_json_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_response_decoders

private

def request(verb = :get, uri = "http://google.com")
def request(verb = "GET", uri = "http://google.com")
Request.new(verb, uri)
end

Expand Down
2 changes: 1 addition & 1 deletion standalone_tests/response_json_oj_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_response_decoders

private

def request(verb = :get, uri = "http://google.com")
def request(verb = "GET", uri = "http://google.com")
Request.new(verb, uri)
end

Expand Down
2 changes: 1 addition & 1 deletion standalone_tests/response_json_yajl_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_response_decoders

private

def request(verb = :get, uri = "http://google.com")
def request(verb = "GET", uri = "http://google.com")
Request.new(verb, uri)
end

Expand Down
2 changes: 1 addition & 1 deletion test/altsvc_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_altsvc_clear_cache
entries = AltSvc.cached_altsvc("http://www.example-clear-cache.com")
assert !entries.empty?

req = Request.new(:get, "http://www.example-clear-cache.com/")
req = Request.new("GET", "http://www.example-clear-cache.com/")
res = Response.new(req, 200, "2.0", { "alt-svc" => "clear" })

AltSvc.emit(req, res)
Expand Down
Loading

0 comments on commit 092e594

Please sign in to comment.