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.

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

#client_error?, #content_length, #content_type, #forbidden?, #include?, #informational?, #invalid?, #location, #not_found?, #ok?, #redirect?, #redirection?, #server_error?, #successful?

Constructor Details

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

Returns a new instance of MockResponse.



151
152
153
154
155
156
157
# File 'lib/rack/mock.rb', line 151

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



149
150
151
# File 'lib/rack/mock.rb', line 149

def errors
  @errors
end

#original_headersObject (readonly)

Headers



146
147
148
# File 'lib/rack/mock.rb', line 146

def original_headers
  @original_headers
end

Instance Method Details

#=~(other) ⇒ Object



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

def =~(other)
  body =~ other
end

#bodyObject



167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/rack/mock.rb', line 167

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)


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

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

#match(other) ⇒ Object



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

def match(other)
  body.match other
end