Class: Rack::MockResponse

Inherits:
Response show all
Defined in:
lib/rack/mock.rb

Overview

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

Constant Summary

Constants inherited from Response

Response::CHUNKED, Response::TRANSFER_ENCODING

Instance Attribute Summary collapse

Attributes inherited from Response

#header, #length, #status

Attributes included from Response::Helpers

#headers

Instance Method Summary collapse

Methods inherited from Response

#[], #[]=, #close, #delete_cookie, #each, #finish, #redirect, #set_cookie, #write

Methods included from Response::Helpers

#accepted?, #bad_request?, #client_error?, #content_length, #content_type, #created?, #forbidden?, #i_m_a_teapot?, #include?, #informational?, #invalid?, #location, #method_not_allowed?, #not_found?, #ok?, #redirect?, #redirection?, #server_error?, #successful?, #unauthorized?, #unprocessable?

Constructor Details

#initialize(status, headers, body, errors = StringIO.new("")) ⇒ MockResponse

Returns a new instance of MockResponse.



164
165
166
167
168
169
170
# File 'lib/rack/mock.rb', line 164

def initialize(status, headers, body, errors=StringIO.new(""))
  @original_headers = headers
  @errors           = errors.string if errors.respond_to?(:string)
  @body_string      = nil

  super(body, status, headers)
end

Instance Attribute Details

#errorsObject

Errors



162
163
164
# File 'lib/rack/mock.rb', line 162

def errors
  @errors
end

#original_headersObject (readonly)

Headers



159
160
161
# File 'lib/rack/mock.rb', line 159

def original_headers
  @original_headers
end

Instance Method Details

#=~(other) ⇒ Object



172
173
174
# File 'lib/rack/mock.rb', line 172

def =~(other)
  body =~ other
end

#bodyObject



180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/rack/mock.rb', line 180

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
  super.join
end

#empty?Boolean

Returns:

  • (Boolean)


194
195
196
# File 'lib/rack/mock.rb', line 194

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

#match(other) ⇒ Object



176
177
178
# File 'lib/rack/mock.rb', line 176

def match(other)
  body.match other
end