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::STATUS_WITH_NO_ENTITY_BODY

Instance Attribute Summary collapse

Attributes inherited from Response

#headers, #length, #status

Instance Method Summary collapse

Methods inherited from Response

#chunked?, #close, #delete_header, #each, #finish, #get_header, #has_header?, #header, #redirect, #set_header, #write

Methods included from Response::Helpers

#accepted?, #add_header, #bad_request?, #cache!, #cache_control, #cache_control=, #client_error?, #content_length, #content_type, #content_type=, #created?, #delete_cookie, #do_not_cache!, #etag, #etag=, #forbidden?, #include?, #informational?, #invalid?, #location, #location=, #media_type, #media_type_params, #method_not_allowed?, #moved_permanently?, #no_content?, #not_acceptable?, #not_found?, #ok?, #precondition_failed?, #redirect?, #redirection?, #request_timeout?, #server_error?, #set_cookie, #set_cookie_header, #set_cookie_header=, #successful?, #unauthorized?, #unprocessable?

Constructor Details

#initialize(status, headers, body, errors = nil) ⇒ MockResponse

Returns a new instance of MockResponse.


188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/rack/mock.rb', line 188

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

Instance Attribute Details

#cookiesObject (readonly)

Headers


183
184
185
# File 'lib/rack/mock.rb', line 183

def cookies
  @cookies
end

#errorsObject

Errors


186
187
188
# File 'lib/rack/mock.rb', line 186

def errors
  @errors
end

#original_headersObject (readonly)

Headers


183
184
185
# File 'lib/rack/mock.rb', line 183

def original_headers
  @original_headers
end

Instance Method Details

#=~(other) ⇒ Object


203
204
205
# File 'lib/rack/mock.rb', line 203

def =~(other)
  body =~ other
end

#bodyObject


211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/rack/mock.rb', line 211

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

235
236
237
# File 'lib/rack/mock.rb', line 235

def cookie(name)
  cookies.fetch(name, nil)
end

#empty?Boolean

Returns:

  • (Boolean)

231
232
233
# File 'lib/rack/mock.rb', line 231

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

#match(other) ⇒ Object


207
208
209
# File 'lib/rack/mock.rb', line 207

def match(other)
  body.match other
end