Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for users to provide custom uri normalizer #533

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions .rubocop.yml
Expand Up @@ -31,10 +31,10 @@ Metrics/ClassLength:

# TODO: Lower to 6
Metrics/CyclomaticComplexity:
Max: 8
Max: 9

Metrics/PerceivedComplexity:
Max: 8
Max: 9

# TODO: Lower to 80
Metrics/LineLength:
Expand Down
4 changes: 3 additions & 1 deletion lib/http/client.rb
Expand Up @@ -43,13 +43,15 @@ def build_request(verb, uri, opts = {}) # rubocop:disable Style/OptionHash
headers = make_request_headers(opts)
body = make_request_body(opts, headers)
proxy = opts.proxy
normalize_uri = opts.features[:normalize_uri]&.method(:normalize_uri)

req = HTTP::Request.new(
:verb => verb,
:uri => uri,
:headers => headers,
:proxy => proxy,
:body => body
:body => body,
:normalize_uri => normalize_uri
)

opts.features.inject(req) do |request, (_name, feature)|
Expand Down
1 change: 1 addition & 0 deletions lib/http/feature.rb
Expand Up @@ -20,3 +20,4 @@ def wrap_response(response)
require "http/features/auto_deflate"
require "http/features/logging"
require "http/features/instrumentation"
require "http/features/normalize_uri"
3 changes: 2 additions & 1 deletion lib/http/features/auto_deflate.rb
Expand Up @@ -32,7 +32,8 @@ def wrap_request(request)
:uri => request.uri,
:headers => request.headers,
:proxy => request.proxy,
:body => deflated_body(request.body)
:body => deflated_body(request.body),
:uri_normalizer => request.uri_normalizer
)
end

Expand Down
25 changes: 25 additions & 0 deletions lib/http/features/normalize_uri.rb
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module HTTP
module Features
class NormalizeUri < Feature
attr_reader :normalizer

def initialize(normalizer: Normalizer)
@normalizer = normalizer
end

def normalize_uri(uri)
normalizer.call(uri)
end

module Normalizer
def self.call(uri)
HTTP::URI::NORMALIZER.call(uri)
end
end

HTTP::Options.register_feature(:normalize_uri, self)
end
end
end
19 changes: 5 additions & 14 deletions lib/http/request.rb
Expand Up @@ -66,20 +66,24 @@ class UnsupportedSchemeError < RequestError; end
# Scheme is normalized to be a lowercase symbol e.g. :http, :https
attr_reader :scheme

attr_reader :uri_normalizer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to expose it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am exposing it because we are using it in wrap_request method in auto_deflate


# "Request URI" as per RFC 2616
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html
attr_reader :uri
attr_reader :proxy, :body, :version

# @option opts [String] :version
# @option opts [#to_s] :verb HTTP request method
# @option opts [#to_s] :uri_normalizer uri normalizer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

description is redundant. It's obvious from the key name.

# @option opts [HTTP::URI, #to_s] :uri
# @option opts [Hash] :headers
# @option opts [Hash] :proxy
# @option opts [String, Enumerable, IO, nil] :body
def initialize(opts)
@verb = opts.fetch(:verb).to_s.downcase.to_sym
@uri = normalize_uri(opts.fetch(:uri))
@uri_normalizer = opts[:normalize_uri] ? opts.fetch(:normalize_uri) : HTTP::URI::NORMALIZER
@uri = @uri_normalizer.call(opts.fetch(:uri))
mamoonraja marked this conversation as resolved.
Show resolved Hide resolved
@scheme = @uri.scheme.to_s.downcase.to_sym if @uri.scheme

raise(UnsupportedMethodError, "unknown method: #{verb}") unless METHODS.include?(@verb)
Expand Down Expand Up @@ -212,18 +216,5 @@ def port
def default_host_header_value
PORTS[@scheme] != port ? "#{host}:#{port}" : host
end

# @return [HTTP::URI] URI with all componentes but query being normalized.
def normalize_uri(uri)
uri = HTTP::URI.parse uri

HTTP::URI.new(
:scheme => uri.normalized_scheme,
:authority => uri.normalized_authority,
:path => uri.normalized_path,
:query => uri.query,
:fragment => uri.normalized_fragment
)
end
end
end
11 changes: 11 additions & 0 deletions lib/http/uri.rb
Expand Up @@ -26,6 +26,17 @@ class URI
# @private
HTTPS_SCHEME = "https"

NORMALIZER = lambda do |uri|
uri = HTTP::URI.parse uri
HTTP::URI.new(
:scheme => uri.normalized_scheme,
:authority => uri.normalized_authority,
:path => uri.normalized_path,
:query => uri.query,
:fragment => uri.normalized_fragment
)
end

# Parse the given URI string, returning an HTTP::URI object
#
# @param [HTTP::URI, String, #to_str] uri to parse
Expand Down
25 changes: 25 additions & 0 deletions spec/lib/http_spec.rb
Expand Up @@ -430,6 +430,31 @@ def setsockopt(*args)
expect(response.to_s).to eq("#{body}-deflated")
end
end

context "with :normalize_uri" do
module Normalizer
def self.call(uri)
uri
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't declare module/class dynamically. You basically don't need this at all.


it "Use the defaul Uri normalizer when user does not use uri normalizer" do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

URI should be all caps. And topic of example should start with lowercase. Also typo in defaul.
In this particular example should be moved into default context, either here:

or here:

And should look like:

it "normalizes URI" do
  response = HTTP.get "#{dummy.endpoint}/hello world"
  expect(response.to_s).to eq("hello world")
end

others examples are OK to be left in here, just make them less "wordy"...

response = HTTP.get "#{dummy.endpoint}/hello world"
expect(response.to_s).to eq("hello world")
end

it "Use the custom Uri Normalizer method" do
client = HTTP.use(:normalize_uri => {:normalizer => Normalizer})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I said above you don't need Normalizer module here. Replace this line with:

Suggested change
client = HTTP.use(:normalize_uri => {:normalizer => Normalizer})
client = HTTP.use(:normalize_uri => {:normalizer => :itself.to_proc})

response = client.get("#{dummy.endpoint}/hello world")
expect(response.status).to eq(400)
end

it "Use the default Uri normalizer when user does not specify custom uri normalizer" do
client = HTTP.use :normalize_uri
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth to add expectation that default normalizer will be called in here:

expect(HTTP::URI::NORMALIZER).to receive(:call).and_call_original

response = client.get("#{dummy.endpoint}/hello world")
expect(response.to_s).to eq("hello world")
end
end
end

it "unifies socket errors into HTTP::ConnectionError" do
Expand Down
5 changes: 5 additions & 0 deletions spec/support/dummy_server/servlet.rb
Expand Up @@ -148,6 +148,11 @@ def do_#{method.upcase}(req, res)
res.body = req.body
end

get "/hello world" do |_req, res|
res.status = 200
res.body = "hello world"
end
mamoonraja marked this conversation as resolved.
Show resolved Hide resolved

post "/encoded-body" do |req, res|
res.status = 200

Expand Down