Class: Rack::MockResponse

Inherits:
Response
  • Object
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.

Defined Under Namespace

Classes: Cookie

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of MockResponse.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rack/mock_response.rb', line 48

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



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

def cookies
  @cookies
end

#errorsObject

Errors



46
47
48
# File 'lib/rack/mock_response.rb', line 46

def errors
  @errors
end

#original_headersObject (readonly)

Headers



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

def original_headers
  @original_headers
end

Instance Method Details

#=~(other) ⇒ Object



63
64
65
# File 'lib/rack/mock_response.rb', line 63

def =~(other)
  body =~ other
end

#bodyObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rack/mock_response.rb', line 71

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

  begin
    if @body.respond_to?(:each)
      @body.each do |chunk|
        buffer << chunk
      end
    else
      @body.call(StringIO.new(buffer))
    end
  ensure
    @body.close if @body.respond_to?(:close)
  end

  return buffer
end


105
106
107
# File 'lib/rack/mock_response.rb', line 105

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

#empty?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/rack/mock_response.rb', line 101

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

#match(other) ⇒ Object



67
68
69
# File 'lib/rack/mock_response.rb', line 67

def match(other)
  body.match other
end