Skip to content

Commit

Permalink
Move useful response test helpers into request
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Apr 26, 2009
1 parent dbbe2e7 commit 5352a24
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 64 deletions.
7 changes: 5 additions & 2 deletions actionpack/lib/action_controller/testing/integration.rb
Expand Up @@ -304,8 +304,11 @@ def process(method, path, parameters = nil, headers = nil)
@response = @controller.response
@controller.send(:set_test_assigns)
else
@request = ::Rack::Request.new(env)
@response = response
@request = Request.new(env)
@response = Response.new
@response.status = @status
@response.headers = @headers
@response.body = @body
end

# Decorate the response with the standard behavior of the
Expand Down
61 changes: 1 addition & 60 deletions actionpack/lib/action_controller/testing/process.rb
Expand Up @@ -156,54 +156,7 @@ def url_encoded_request_parameters
# A refactoring of TestResponse to allow the same behavior to be applied
# to the "real" CgiResponse class in integration tests.
module TestResponseBehavior #:nodoc:
# The response code of the request
def response_code
status.to_s[0,3].to_i rescue 0
end

# Returns a String to ensure compatibility with Net::HTTPResponse
def code
status.to_s.split(' ')[0]
end

def message
status.to_s.split(' ',2)[1]
end

# Was the response successful?
def success?
(200..299).include?(response_code)
end

# Was the URL not found?
def missing?
response_code == 404
end

# Were we redirected?
def redirect?
(300..399).include?(response_code)
end

# Was there a server-side error?
def error?
(500..599).include?(response_code)
end

alias_method :server_error?, :error?

# Was there a client client?
def client_error?
(400..499).include?(response_code)
end

# Returns the redirection location or nil
def redirect_url
headers['Location']
end

# Does the redirect location match this regexp pattern?
def redirect_url_match?( pattern )
def redirect_url_match?(pattern)
return false if redirect_url.nil?
p = Regexp.new(pattern) if pattern.class == String
p = pattern if pattern.class == Regexp
Expand Down Expand Up @@ -252,18 +205,6 @@ def has_template_object?(name=nil)
!template_objects[name].nil?
end

# Returns the response cookies, converted to a Hash of (name => value) pairs
#
# assert_equal 'AuthorOfNewPage', r.cookies['author']
def cookies
cookies = {}
Array(headers['Set-Cookie']).each do |cookie|
key, value = cookie.split(";").first.split("=").map {|val| Rack::Utils.unescape(val)}
cookies[key] = value
end
cookies
end

# Returns binary content (downloadable file), converted to a String
def binary_content
raise "Response body is not a Proc: #{body_parts.inspect}" unless body_parts.kind_of?(Proc)
Expand Down
65 changes: 63 additions & 2 deletions actionpack/lib/action_dispatch/http/response.rb
Expand Up @@ -37,6 +37,9 @@ class Response < Rack::Response
attr_accessor :session, :assigns, :template, :layout
attr_accessor :redirected_to, :redirected_to_method_params

attr_writer :header
alias_method :headers=, :header=

delegate :default_charset, :to => 'ActionController::Base'

def initialize
Expand All @@ -45,6 +48,47 @@ def initialize
@session, @assigns = [], []
end

# The response code of the request
def response_code
status.to_s[0,3].to_i rescue 0
end

# Returns a String to ensure compatibility with Net::HTTPResponse
def code
status.to_s.split(' ')[0]
end

def message
status.to_s.split(' ',2)[1] || StatusCodes::STATUS_CODES[response_code]
end

# Was the response successful?
def success?
(200..299).include?(response_code)
end

# Was the URL not found?
def missing?
response_code == 404
end

# Were we redirected?
def redirect?
(300..399).include?(response_code)
end

# Was there a server-side error?
def error?
(500..599).include?(response_code)
end

alias_method :server_error?, :error?

# Was there a client client?
def client_error?
(400..499).include?(response_code)
end

def body
str = ''
each { |part| str << part.to_s }
Expand All @@ -64,9 +108,14 @@ def body_parts
@body
end

def location; headers['Location'] end
def location=(url) headers['Location'] = url end
def location
headers['Location']
end
alias_method :redirect_url, :location

def location=(url)
headers['Location'] = url
end

# Sets the HTTP response's content MIME type. For example, in the controller
# you could write this:
Expand Down Expand Up @@ -192,6 +241,18 @@ def set_cookie(key, value)
super(key, value)
end

# Returns the response cookies, converted to a Hash of (name => value) pairs
#
# assert_equal 'AuthorOfNewPage', r.cookies['author']
def cookies
cookies = {}
Array(headers['Set-Cookie']).each do |cookie|
key, value = cookie.split(";").first.split("=").map { |v| Rack::Utils.unescape(v) }
cookies[key] = value
end
cookies
end

private
def handle_conditional_get!
if etag? || last_modified?
Expand Down
47 changes: 47 additions & 0 deletions actionpack/test/dispatch/response_test.rb
Expand Up @@ -80,4 +80,51 @@ def setup
status, headers, body = @response.to_a
assert !headers.has_key?('Status')
end

test "response code" do
@response.status = "200 OK"
assert_equal 200, @response.response_code

@response.status = "200"
assert_equal 200, @response.response_code

@response.status = 200
assert_equal 200, @response.response_code
end

test "code" do
@response.status = "200 OK"
assert_equal "200", @response.code

@response.status = "200"
assert_equal "200", @response.code

@response.status = 200
assert_equal "200", @response.code
end

test "message" do
@response.status = "200 OK"
assert_equal "OK", @response.message

@response.status = "200"
assert_equal "OK", @response.message

@response.status = 200
assert_equal "OK", @response.message
end

test "cookies" do
@response.set_cookie("user_name", :value => "david", :path => "/")
@response.prepare!
status, headers, body = @response.to_a
assert_equal "user_name=david; path=/", headers["Set-Cookie"]
assert_equal({"user_name" => "david"}, @response.cookies)

@response.set_cookie("login", :value => "foo&bar", :path => "/", :expires => Time.utc(2005, 10, 10,5))
@response.prepare!
status, headers, body = @response.to_a
assert_equal "user_name=david; path=/\nlogin=foo%26bar; path=/; expires=Mon, 10-Oct-2005 05:00:00 GMT", headers["Set-Cookie"]
assert_equal({"login" => "foo&bar", "user_name" => "david"}, @response.cookies)
end
end

0 comments on commit 5352a24

Please sign in to comment.