1 # frozen_string_literal: true
6 require_relative 'response'
9 # Rack::MockResponse provides useful helpers for testing your apps.
10 # Usually, you don't create the MockResponse on your own, but use
13 class MockResponse < Rack::Response
19 attr_reader :original_headers, :cookies
24 def initialize(status, headers, body, errors = nil)
25 @original_headers = headers
28 @errors = errors.string if errors.respond_to?(:string)
33 super(body, status, headers)
35 @cookies = parse_cookies_from_header
48 return @buffered_body if defined?(@buffered_body)
50 # FIXME: apparently users of MockResponse expect the return value of
51 # MockResponse#body to be a string. However, the real response object
52 # returns the body as a list.
54 # See spec_showstatus.rb:
56 # should "not replace existing messages" do
58 # res.body.should == "foo!"
60 buffer = @buffered_body = String.new
70 [201, 204, 304].include? status
74 cookies.fetch(name, nil)
79 def parse_cookies_from_header
81 set_cookie_header = headers['set-cookie']
82 if set_cookie_header && !set_cookie_header.empty?
83 Array(set_cookie_header).each do |cookie|
84 cookie_name, cookie_filling = cookie.split('=', 2)
85 cookie_attributes = identify_cookie_attributes cookie_filling
86 parsed_cookie = CGI::Cookie.new(
87 'name' => cookie_name.strip,
88 'value' => cookie_attributes.fetch('value'),
89 'path' => cookie_attributes.fetch('path', nil),
90 'domain' => cookie_attributes.fetch('domain', nil),
91 'expires' => cookie_attributes.fetch('expires', nil),
92 'secure' => cookie_attributes.fetch('secure', false)
94 cookies.store(cookie_name, parsed_cookie)
100 def identify_cookie_attributes(cookie_filling)
101 cookie_bits = cookie_filling.split(';')
102 cookie_attributes = Hash.new
103 cookie_attributes.store('value', cookie_bits[0].strip)
104 cookie_bits.drop(1).each do |bit|
106 cookie_attribute, attribute_value = bit.split('=', 2)
107 cookie_attributes.store(cookie_attribute.strip.downcase, attribute_value.strip)
109 if bit.include? 'secure'
110 cookie_attributes.store('secure', true)
114 if cookie_attributes.key? 'max-age'
115 cookie_attributes.store('expires', Time.now + cookie_attributes['max-age'].to_i)
116 elsif cookie_attributes.key? 'expires'
117 cookie_attributes.store('expires', Time.httpdate(cookie_attributes['expires']))