Class: WebMock::Util::URI

Inherits:
Object
  • Object
show all
Defined in:
lib/webmock/util/uri.rb

Defined Under Namespace

Modules: CharacterClasses

Constant Summary collapse

ADDRESSABLE_URIS =
Hash.new do |hash, key|
  hash[key] = Addressable::URI.heuristic_parse(key)
end
NORMALIZED_URIS =
Hash.new do |hash, uri|
  normalized_uri = WebMock::Util::URI.heuristic_parse(uri)
  if normalized_uri.query_values
    sorted_query_values = sort_query_values(WebMock::Util::QueryMapper.query_to_values(normalized_uri.query, notation: Config.instance.query_values_notation) || {})
    normalized_uri.query = WebMock::Util::QueryMapper.values_to_query(sorted_query_values, notation: WebMock::Config.instance.query_values_notation)
  end
  normalized_uri = normalized_uri.normalize #normalize! is slower
  normalized_uri.query = normalized_uri.query.gsub("+", "%2B") if normalized_uri.query
  normalized_uri.port = normalized_uri.inferred_port unless normalized_uri.port
  hash[uri] = normalized_uri
end

Class Method Summary collapse

Class Method Details

.encode_unsafe_chars_in_userinfo(userinfo) ⇒ Object



67
68
69
# File 'lib/webmock/util/uri.rb', line 67

def self.encode_unsafe_chars_in_userinfo(userinfo)
  Addressable::URI.encode_component(userinfo, WebMock::Util::URI::CharacterClasses::USERINFO)
end

.heuristic_parse(uri) ⇒ Object



28
29
30
# File 'lib/webmock/util/uri.rb', line 28

def self.heuristic_parse(uri)
  ADDRESSABLE_URIS[uri].dup
end

.is_uri_localhost?(uri) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
# File 'lib/webmock/util/uri.rb', line 71

def self.is_uri_localhost?(uri)
  uri.is_a?(Addressable::URI) &&
  %w(localhost 127.0.0.1 0.0.0.0 [::1]).include?(uri.host)
end

.normalize_uri(uri) ⇒ Object



32
33
34
35
36
# File 'lib/webmock/util/uri.rb', line 32

def self.normalize_uri(uri)
  return uri if uri.is_a?(Regexp)
  uri = 'http://' + uri unless uri.match('^https?://') if uri.is_a?(String)
  NORMALIZED_URIS[uri].dup
end

.strip_default_port_from_uri_string(uri_string) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/webmock/util/uri.rb', line 59

def self.strip_default_port_from_uri_string(uri_string)
  case uri_string
  when %r{^http://}  then uri_string.sub(%r{:80(/|$)}, '\1')
  when %r{^https://} then uri_string.sub(%r{:443(/|$)}, '\1')
  else uri_string
  end
end

.variations_of_uri_as_strings(uri_object, only_with_scheme: false) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/webmock/util/uri.rb', line 38

def self.variations_of_uri_as_strings(uri_object, only_with_scheme: false)
  normalized_uri = normalize_uri(uri_object.dup).freeze
  uris = [ normalized_uri ]

  if normalized_uri.path == '/'
    uris = uris_with_trailing_slash_and_without(uris)
  end

  if normalized_uri.port == Addressable::URI.port_mapping[normalized_uri.scheme]
    uris = uris_with_inferred_port_and_without(uris)
  end

  uris = uris_encoded_and_unencoded(uris)

  if normalized_uri.scheme == "http" && !only_with_scheme
    uris = uris_with_scheme_and_without(uris)
  end

  uris.map {|uri| uri.to_s.gsub(/^\/\//,'') }.uniq
end