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.



180
181
182
183
184
185
186
# File 'lib/rack/mock.rb', line 180

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



178
179
180
# File 'lib/rack/mock.rb', line 178

def errors
  @errors
end

#original_headersObject (readonly)

Headers



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

def original_headers
  @original_headers
end

Instance Method Details

#=~(other) ⇒ Object



188
189
190
# File 'lib/rack/mock.rb', line 188

def =~(other)
  body =~ other
end

#bodyObject



196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/rack/mock.rb', line 196

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)


210
211
212
# File 'lib/rack/mock.rb', line 210

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

#match(other) ⇒ Object



192
193
194
# File 'lib/rack/mock.rb', line 192

def match(other)
  body.match other
end