Class: Rack::MockResponse

Inherits:
Response show all
Defined in:
lib/rack/mock_response.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?, #no_entity_body?, #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.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rack/mock_response.rb', line 24

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



19
20
21
# File 'lib/rack/mock_response.rb', line 19

def cookies
  @cookies
end

#errorsObject

Errors



22
23
24
# File 'lib/rack/mock_response.rb', line 22

def errors
  @errors
end

#original_headersObject (readonly)

Headers



19
20
21
# File 'lib/rack/mock_response.rb', line 19

def original_headers
  @original_headers
end

Instance Method Details

#=~(other) ⇒ Object



39
40
41
# File 'lib/rack/mock_response.rb', line 39

def =~(other)
  body =~ other
end

#bodyObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rack/mock_response.rb', line 47

def body
  return @buffered_body if defined?(@buffered_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 = @buffered_body = String.new

  @body.each do |chunk|
    buffer << chunk
  end

  return buffer
end


73
74
75
# File 'lib/rack/mock_response.rb', line 73

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

#empty?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/rack/mock_response.rb', line 69

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

#match(other) ⇒ Object



43
44
45
# File 'lib/rack/mock_response.rb', line 43

def match(other)
  body.match other
end