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?, #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_found?, #ok?, #precondition_failed?, #redirect?, #redirection?, #server_error?, #set_cookie, #set_cookie_header, #set_cookie_header=, #successful?, #unauthorized?, #unprocessable?

Constructor Details

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

Returns a new instance of MockResponse.


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

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

Instance Attribute Details

#cookiesObject (readonly)

Headers


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

def cookies
  @cookies
end

#errorsObject

Errors


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

def errors
  @errors
end

#original_headersObject (readonly)

Headers


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

def original_headers
  @original_headers
end

Instance Method Details

#=~(other) ⇒ Object


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

def =~(other)
  body =~ other
end

#bodyObject


202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/rack/mock.rb', line 202

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

226
227
228
# File 'lib/rack/mock.rb', line 226

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

#empty?Boolean

Returns:

  • (Boolean)

222
223
224
# File 'lib/rack/mock.rb', line 222

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

#match(other) ⇒ Object


198
199
200
# File 'lib/rack/mock.rb', line 198

def match(other)
  body.match other
end