Module: FakeWeb::Utility

Defined in:
lib/fake_web/utility.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.decode_userinfo_from_header(header) ⇒ Object



4
5
6
# File 'lib/fake_web/utility.rb', line 4

def self.decode_userinfo_from_header(header)
  header.sub(/^Basic /, "").unpack("m").first
end

.encode_unsafe_chars_in_userinfo(userinfo) ⇒ Object



8
9
10
11
# File 'lib/fake_web/utility.rb', line 8

def self.encode_unsafe_chars_in_userinfo(userinfo)
  unsafe_in_userinfo = /[^#{URI::REGEXP::PATTERN::UNRESERVED};&=+$,]|^(#{URI::REGEXP::PATTERN::ESCAPED})/
  userinfo.split(":").map { |part| URI.escape(part, unsafe_in_userinfo) }.join(":")
end

.puts_warning_for_net_http_around_advice_libs_if_neededObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fake_web/utility.rb', line 21

def self.puts_warning_for_net_http_around_advice_libs_if_needed
  libs = {"Samuel" => defined?(Samuel)}
  warnings = libs.select { |_, loaded| loaded }.map do |name, _|
    "      \\e[1mWarning: FakeWeb was loaded after \#{name}\\e[0m\n      * \#{name}'s code is being ignored when a request is handled by FakeWeb,\n        because both libraries work by patching Net::HTTP.\n      * To fix this, just reorder your requires so that FakeWeb is before \#{name}.\n    TEXT\n  end\n  $stderr.puts \"\\n\" + warnings.join(\"\\n\") + \"\\n\" if warnings.any?\nend\n".gsub(/ {10}/, '')

.puts_warning_for_net_http_replacement_libs_if_neededObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fake_web/utility.rb', line 39

def self.puts_warning_for_net_http_replacement_libs_if_needed
  libs = {"RightHttpConnection" => defined?(RightHttpConnection)}
  warnings = libs.select { |_, loaded| loaded }.
                reject { |name, _| @loaded_net_http_replacement_libs.include?(name) }.
                map do |name, _|
    "      \\e[1mWarning: \#{name} was loaded after FakeWeb\\e[0m\n      * FakeWeb's code is being ignored, because \#{name} replaces parts of\n        Net::HTTP without deferring to other libraries. This will break Net::HTTP requests.\n      * To fix this, just reorder your requires so that \#{name} is before FakeWeb.\n    TEXT\n  end\n  $stderr.puts \"\\n\" + warnings.join(\"\\n\") + \"\\n\" if warnings.any?\nend\n".gsub(/ {10}/, '')

.record_loaded_net_http_replacement_libsObject



34
35
36
37
# File 'lib/fake_web/utility.rb', line 34

def self.record_loaded_net_http_replacement_libs
  libs = {"RightHttpConnection" => defined?(RightHttpConnection)}
  @loaded_net_http_replacement_libs = libs.map { |name, loaded| name if loaded }.compact
end

.strip_default_port_from_uri(uri) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/fake_web/utility.rb', line 13

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