123456789_123456789_123456789_123456789_123456789_

Class: Rack::MockResponse

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Response
Instance Chain:
Inherits: Rack::Response
Defined in: lib/rack/mock_response.rb

Overview

MockResponse provides useful helpers for testing your apps. Usually, you don’t create the MockResponse on your own, but use MockRequest.

Constant Summary

Response - Inherited

CHUNKED, STATUS_WITH_NO_ENTITY_BODY

Class Method Summary

Response - Inherited

.[],
.new

Initialize the response object with the specified #body, status and headers.

Instance Attribute Summary

Instance Method Summary

Response - Inherited

#[]
#[]=
#close, #delete_header, #each,
#finish

Generate a response array consistent with the requirements of the SPEC.

#get_header, #has_header?, #redirect, #set_header,
#to_a

Alias for Response#finish.

#write

Append a chunk to the response body.

Response::Helpers - Included

#add_header

Add a header that may have multiple values.

#cache!

Specify that the content should be cached.

#content_length, #delete_cookie,
#do_not_cache!

Specifies that the content shouldn’t be cached.

#include?, #media_type, #media_type_params, #set_cookie

Constructor Details

.new(status, headers, body, errors = nil) ⇒ MockResponse

[ GitHub ]

  
# File 'lib/rack/mock_response.rb', line 24

def initialize(status, headers, body, errors = nil)
  @original_headers = headers

  if errors
    @errors = errors.string if errors.respond_to?(:string)
  else
    @errors = ""
  end

  super(body, status, headers)

  @cookies = parse_cookies_from_header
  buffered_body!
end

Class Method Details

.[](body = nil, status = 200, headers = {})

Alias for Response.new.

[ GitHub ]

  
# File 'lib/rack/mock_response.rb', line 15

alias [] new

Instance Attribute Details

#cookies (readonly)

[ GitHub ]

  
# File 'lib/rack/mock_response.rb', line 19

attr_reader :original_headers, :cookies

#empty?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rack/mock_response.rb', line 69

def empty?
  [201, 204, 304].include? status
end

#errors (rw)

Errors

[ GitHub ]

  
# File 'lib/rack/mock_response.rb', line 22

attr_accessor :errors

#original_headers (readonly)

[ GitHub ]

  
# File 'lib/rack/mock_response.rb', line 19

attr_reader :original_headers, :cookies

Instance Method Details

#=~(other)

[ GitHub ]

  
# File 'lib/rack/mock_response.rb', line 39

def =~(other)
  body =~ other
end

#body

[ GitHub ]

  
# File 'lib/rack/mock_response.rb', line 47

def body
  return @buffered_body if defined?(@buffered_body)

  # FIXME: apparently users of MockResponse expect the return value of
  # MockResponse#body to be a string.  However, the real response object
  # returns the body as a list.
  #
  # See spec_showstatus.rb:
  #
  #   should "not replace existing messages" do
  #     ...
  #     res.body.should == "foo!"
  #   end
  buffer = @buffered_body = String.new

  @body.each do |chunk|
    buffer << chunk
  end

  return buffer
end

#match(other)

[ GitHub ]

  
# File 'lib/rack/mock_response.rb', line 43

def match(other)
  body.match other
end

#parse_cookies_from_header (private)

[ GitHub ]

  
# File 'lib/rack/mock_response.rb', line 79

def parse_cookies_from_header
  cookies = Hash.new
  set_cookie_header = headers['set-cookie']
  if set_cookie_header && !set_cookie_header.empty?
    Array(set_cookie_header).each do |cookie|
      cookie_name, cookie_filling = cookie.split('=', 2)
      cookie_attributes = identify_cookie_attributes cookie_filling
      parsed_cookie = CGI::Cookie.new(
        'name' => cookie_name.strip,
        'value' => cookie_attributes.fetch('value'),
        'path' => cookie_attributes.fetch('path', nil),
        'domain' => cookie_attributes.fetch('domain', nil),
        'expires' => cookie_attributes.fetch('expires', nil),
        'secure' => cookie_attributes.fetch('secure', false)
      )
      cookies.store(cookie_name, parsed_cookie)
    end
  end
  cookies
end