Class: RSpec::Http::HeaderMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/http/header_matchers.rb

Constant Summary collapse

NO_VALUE =
Object.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expected) ⇒ HeaderMatcher

Returns a new instance of HeaderMatcher.



7
8
9
# File 'lib/rspec/http/header_matchers.rb', line 7

def initialize(expected)
  @header, @expected_value = expected.kind_of?(String) ? [expected, NO_VALUE] : expected.first
end

Instance Attribute Details

#expected_valueObject (readonly)

Returns the value of attribute expected_value.



4
5
6
# File 'lib/rspec/http/header_matchers.rb', line 4

def expected_value
  @expected_value
end

#headerObject (readonly)

Returns the value of attribute header.



4
5
6
# File 'lib/rspec/http/header_matchers.rb', line 4

def header
  @header
end

#responseObject (readonly)

Returns the value of attribute response.



4
5
6
# File 'lib/rspec/http/header_matchers.rb', line 4

def response
  @response
end

Instance Method Details

#descriptionObject



25
26
27
# File 'lib/rspec/http/header_matchers.rb', line 25

def description
  @matcher.description
end

#failure_messageObject



29
30
31
# File 'lib/rspec/http/header_matchers.rb', line 29

def failure_message
  @matcher.failure_message
end

#failure_message_when_negatedObject



33
34
35
# File 'lib/rspec/http/header_matchers.rb', line 33

def failure_message_when_negated
  @matcher.failure_message_when_negated
end

#matches?(response) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rspec/http/header_matchers.rb', line 11

def matches?(response)
  if response[header]
    @matcher = case expected_value
      when String then HeaderStringMatcher.new(header, expected_value)
      when Regexp then HeaderRegexpMatcher.new(header, expected_value)
      when NO_VALUE then HeaderPresenceMatcher.new(header)
      else raise SyntaxError.new("The value for a header should be either a String or a Regexp and not of type #{expected_value.class}")
    end
  else
    @matcher = HeaderPresenceMatcher.new(header)
  end
  @matcher.matches?(response)
end