Skip to content

Commit

Permalink
refactor(uri_normalizer): update the functionality of api
Browse files Browse the repository at this point in the history
  • Loading branch information
mamoonraja committed Feb 25, 2019
1 parent c862d3e commit 1eae155
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 75 deletions.
4 changes: 2 additions & 2 deletions lib/http/client.rb
Expand Up @@ -43,15 +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
uri_normalizer = opts.features[:uri_normalizer]&.method(:normalize_uri)
normalize_uri = opts.features[:normalize_uri]&.method(:normalize_uri)

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

opts.features.inject(req) do |request, (_name, feature)|
Expand Down
2 changes: 1 addition & 1 deletion lib/http/feature.rb
Expand Up @@ -20,4 +20,4 @@ def wrap_response(response)
require "http/features/auto_deflate"
require "http/features/logging"
require "http/features/instrumentation"
require "http/features/uri_normalizer"
require "http/features/normalize_uri"
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
32 changes: 0 additions & 32 deletions lib/http/features/uri_normalizer.rb

This file was deleted.

15 changes: 1 addition & 14 deletions lib/http/request.rb
Expand Up @@ -82,7 +82,7 @@ class UnsupportedSchemeError < RequestError; end
# @option opts [String, Enumerable, IO, nil] :body
def initialize(opts)
@verb = opts.fetch(:verb).to_s.downcase.to_sym
@uri_normalizer = opts[:uri_normalizer] ? opts.fetch(:uri_normalizer) : self.class.method(:normalize_uri)
@uri_normalizer = opts[:normalize_uri] ? opts.fetch(:normalize_uri) : HTTP::URI::NORMALIZER
@uri = @uri_normalizer.call(opts.fetch(:uri))
@scheme = @uri.scheme.to_s.downcase.to_sym if @uri.scheme

Expand Down Expand Up @@ -200,19 +200,6 @@ def inspect
"#<#{self.class}/#{@version} #{verb.to_s.upcase} #{uri}>"
end

# @return [HTTP::URI] URI with all components but query being normalized.
def self.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

private

# @!attribute [r] host
Expand Down
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
31 changes: 12 additions & 19 deletions spec/lib/http_spec.rb
Expand Up @@ -431,35 +431,28 @@ def setsockopt(*args)
end
end

context "with :uri_normalizer" do
class CustomUriNormalizer
def normalize_uri(uri)
uri = HTTP::URI.parse uri
HTTP::URI.new(
:scheme => uri.normalized_scheme,
:authority => uri.normalized_authority,
:path => "uri_normalizer/custom",
:query => uri.query,
:fragment => uri.normalized_fragment
)
context "with :normalize_uri" do
module Normalizer
def self.call(uri)
uri
end
end

it "Use the defaul Uri normalizer when user does not use uri normalizer" do
response = HTTP.get HTTP::URI.parse "#{dummy.endpoint}/uri_normalizer/%EF%BC%A1%EF%BC%A2%EF%BC%A3"
expect(response.to_s).to eq("default normalizer")
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(:uri_normalizer => {:custom_uri_normalizer => CustomUriNormalizer.new})
response = client.get("#{dummy.endpoint}/uri_normalizer/%EF%BC%A1%EF%BC%A2%EF%BC%A3")
expect(response.to_s).to eq("custom normalizer")
client = HTTP.use(:normalize_uri => {:normalizer => Normalizer})
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 :uri_normalizer
response = client.get("#{dummy.endpoint}/uri_normalizer/%EF%BC%A1%EF%BC%A2%EF%BC%A3")
expect(response.to_s).to eq("default normalizer")
client = HTTP.use :normalize_uri
response = client.get("#{dummy.endpoint}/hello world")
expect(response.to_s).to eq("hello world")
end
end
end
Expand Down
9 changes: 2 additions & 7 deletions spec/support/dummy_server/servlet.rb
Expand Up @@ -148,14 +148,9 @@ def do_#{method.upcase}(req, res)
res.body = req.body
end

get "/uri_normalizer/ABC" do |_req, res|
get "/hello world" do |_req, res|
res.status = 200
res.body = "default normalizer"
end

get "/uri_normalizer/custom" do |_req, res|
res.status = 200
res.body = "custom normalizer"
res.body = "hello world"
end

post "/encoded-body" do |req, res|
Expand Down

0 comments on commit 1eae155

Please sign in to comment.