# class Rack::MockResponse

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

### Constants

Cookie

### Attributes

cookies[R]

Headers

errors[RW]

Errors

original_headers[R]

Headers

### Public Class Methods

new(status, headers, body, errors = StringIO.new("")) click to toggle source
Calls superclass method `Rack::Response::new`

    # File lib/rack/mock.rb, line 213
    def initialize(status, headers, body, errors = StringIO.new(""))
      @original_headers = headers
      @errors           = errors.string if errors.respond_to?(:string)
      @cookies = parse_cookies_from_header

      super(body, status, headers)

      buffered_body!
    end

### Public Instance Methods

=~(other) click to toggle source

    # File lib/rack/mock.rb, line 223
    def =~(other)
      body =~ other
    end

body() click to toggle source
Calls superclass method

    # File lib/rack/mock.rb, line 231
    def 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 = String.new

      super.each do |chunk|
        buffer << chunk
      end

      return buffer
    end

cookie(name) click to toggle source

    # File lib/rack/mock.rb, line 255
    def cookie(name)
      cookies.fetch(name, nil)
    end

empty?() click to toggle source

    # File lib/rack/mock.rb, line 251
    def empty?
      [201, 204, 304].include? status
    end

match(other) click to toggle source

    # File lib/rack/mock.rb, line 227
    def match(other)
      body.match other
    end

### Private Instance Methods

identify_cookie_attributes(cookie_filling) click to toggle source

    # File lib/rack/mock.rb, line 282
    def identify_cookie_attributes(cookie_filling)
      cookie_bits = cookie_filling.split(';')
      cookie_attributes = Hash.new
      cookie_attributes.store('value', Array(cookie_bits[0].strip))
      cookie_bits.each do |bit|
        if bit.include? '='
          cookie_attribute, attribute_value = bit.split('=')
          cookie_attributes.store(cookie_attribute.strip, attribute_value.strip)
          if cookie_attribute.include? 'max-age'
            cookie_attributes.store('expires', Time.now + attribute_value.strip.to_i)
          end
        end
        if bit.include? 'secure'
          cookie_attributes.store('secure', true)
        end
      end
      cookie_attributes
    end

parse_cookies_from_header() click to toggle source

    # File lib/rack/mock.rb, line 261
    def parse_cookies_from_header
      cookies = Hash.new
      if original_headers.has_key? 'Set-Cookie'
        set_cookie_header = original_headers.fetch('Set-Cookie')
        set_cookie_header.split("\n").each do |cookie|
          cookie_name, cookie_filling = cookie.split('=', 2)
          cookie_attributes = identify_cookie_attributes cookie_filling
          parsed_cookie = 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