Class: WebMock::Util::Headers

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

Class Method Summary collapse

Class Method Details

.basic_auth_header(*credentials) ⇒ Object


56
57
58
# File 'lib/webmock/util/headers.rb', line 56

def self.basic_auth_header(*credentials)
  "Basic #{Base64.strict_encode64(credentials.join(':')).chomp}"
end

.decode_userinfo_from_header(header) ⇒ Object


52
53
54
# File 'lib/webmock/util/headers.rb', line 52

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

.normalize_headers(headers) ⇒ Object


9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/webmock/util/headers.rb', line 9

def self.normalize_headers(headers)
  return nil unless headers
  array = headers.map { |name, value|
    [name.to_s.split(/_|-/).map { |segment| segment.capitalize }.join("-"),
     case value
      when Regexp then value
      when Array then (value.size == 1) ? value.first.to_s : value.map {|v| v.to_s}.sort
      else value.to_s
     end
    ]
  }
  Hash[*array.inject([]) {|r,x| r + x}]
end

.pp_headers_string(headers) ⇒ Object


37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/webmock/util/headers.rb', line 37

def self.pp_headers_string(headers)
  headers = WebMock::Util::Headers.normalize_headers(headers)
  seperator = "\n\t "
  str = "{#{seperator} ".dup
  str << headers.map do |k,v|
    v = case v
      when Regexp then v.inspect
      when Array then "["+v.map{|w| "'#{w.to_s}'"}.join(", ")+"]"
      else "'#{v.to_s}'"
    end
    "'#{k}'=>#{v}"
  end.sort.join(",#{seperator} ")
  str << "\n    }"
end

.sorted_headers_string(headers) ⇒ Object


23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/webmock/util/headers.rb', line 23

def self.sorted_headers_string(headers)
  headers = WebMock::Util::Headers.normalize_headers(headers)
  str = '{'.dup
  str << headers.map do |k,v|
    v = case v
      when Regexp then v.inspect
      when Array then "["+v.map{|w| "'#{w.to_s}'"}.join(", ")+"]"
      else "'#{v.to_s}'"
    end
    "'#{k}'=>#{v}"
  end.sort.join(", ")
  str << '}'
end