Class: Crspec::Rails::RequestHelpers::ResponseStruct

Inherits:
Struct
  • Object
show all
Defined in:
lib/crspec/rails/request_helpers.rb

Overview

Lightweight response wrapper that mimics the small subset of the Rails test response API that the specs rely on (status, body, headers, media_type, parsed_body, etc.).

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bodyObject

Returns the value of attribute body

Returns:

  • (Object)

    the current value of body



24
25
26
# File 'lib/crspec/rails/request_helpers.rb', line 24

def body
  @body
end

#headersObject

Returns the value of attribute headers

Returns:

  • (Object)

    the current value of headers



24
25
26
# File 'lib/crspec/rails/request_helpers.rb', line 24

def headers
  @headers
end

#statusObject

Returns the value of attribute status

Returns:

  • (Object)

    the current value of status



24
25
26
# File 'lib/crspec/rails/request_helpers.rb', line 24

def status
  @status
end

Instance Method Details

#media_typeObject



25
26
27
28
29
30
31
# File 'lib/crspec/rails/request_helpers.rb', line 25

def media_type
  content_type_header = headers["Content-Type"] || headers["content-type"]
  return nil unless content_type_header

  # Strip any charset or parameters, e.g. "application/json; charset=utf-8"
  content_type_header.split(";").first&.strip
end

#parsed_bodyObject



33
34
35
36
37
38
# File 'lib/crspec/rails/request_helpers.rb', line 33

def parsed_body
  return nil unless body
  JSON.parse(body)
rescue JSON::ParserError
  nil
end