Class: HTTPFiesta::Assertion
- Inherits:
-
Object
- Object
- HTTPFiesta::Assertion
- Defined in:
- lib/httpfiesta/assertion.rb
Constant Summary collapse
- CONTENT_TYPE_MAP =
{ json: 'application/json', xml: 'text/xml', html: 'text/html' }
Instance Attribute Summary collapse
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
- #content_type(type) ⇒ Object (also: #content)
-
#initialize(response) ⇒ Assertion
constructor
A new instance of Assertion.
- #status(*ranges_or_codes) ⇒ Object (also: #code)
Constructor Details
#initialize(response) ⇒ Assertion
Returns a new instance of Assertion.
13 14 15 |
# File 'lib/httpfiesta/assertion.rb', line 13 def initialize(response) @response = response end |
Instance Attribute Details
#response ⇒ Object (readonly)
Returns the value of attribute response.
5 6 7 |
# File 'lib/httpfiesta/assertion.rb', line 5 def response @response end |
Instance Method Details
#content_type(type) ⇒ Object Also known as: content
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/httpfiesta/assertion.rb', line 31 def content_type(type) raise ArgumentError, 'type cannot be nil' if type.nil? if type.is_a?(Symbol) if CONTENT_TYPE_MAP.key?(type) type = CONTENT_TYPE_MAP[type] else raise ArgumentError, "Unallowed content-type symbol :#{type}, valid symbols: #{CONTENT_TYPE_MAP.keys.join ', '}" end end raise ArgumentError, 'type must be a String or Symbol' unless type.is_a?(String) mime = response.headers['Content-Type'] error 'Content-Type header not present' if mime.nil? if mime.downcase.include?(type.to_s) self else error "response content type '#{mime}' does not match expected type of '#{type}'" end end |
#status(*ranges_or_codes) ⇒ Object Also known as: code
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/httpfiesta/assertion.rb', line 17 def status(*ranges_or_codes) ranges = ranges_or_codes.map do |range_or_code| range = range_or_code.is_a?(Range) ? range_or_code : range_or_code..range_or_code return self if range.include?(response.code.to_i) range end # If we're here, then error description = ranges.map(&:to_s).join(', ') error "status code '#{response.code}' not in allowable range: #{description}" end |