|
@@ -0,0 +1,226 @@
|
|
|
+require './resource_path'
|
|
|
+require './response_check'
|
|
|
+
|
|
|
+require 'test/unit'
|
|
|
+require 'digest'
|
|
|
+
|
|
|
+class HTTPResponseTest < Test::Unit::TestCase
|
|
|
+
|
|
|
+ def default_test; end # placeholder to stop Test::Unit from complaining
|
|
|
+
|
|
|
+ def self.useragent=(useragent)
|
|
|
+ ResponseCheck.useragent = useragent
|
|
|
+ end
|
|
|
+ def self.reset_useragent
|
|
|
+ ResponseCheck.useragent = nil
|
|
|
+ end
|
|
|
+ def self.ssl_no_verify=(verify)
|
|
|
+ ResponseCheck.ssl_no_verify = verify
|
|
|
+ end
|
|
|
+
|
|
|
+ # no accessors needed, as every class that is derived from this one can be adjusted featurewise by just setting:
|
|
|
+ # @useragent
|
|
|
+ # @ssl_no_verify
|
|
|
+
|
|
|
+ def self.uri_be_code(options)
|
|
|
+ @get_uri = options[:uri].to_s
|
|
|
+ @http_code = options[:code].to_s
|
|
|
+ @last_uri = @get_uri
|
|
|
+ @last_code = @http_code
|
|
|
+
|
|
|
+ id = Digest::SHA1.hexdigest "#{@get_uri}#{@http_code}#{@ssl_no_verify}#{@useragent}"
|
|
|
+ id = id[0..7]
|
|
|
+
|
|
|
+ #puts "uri to be code ssl_no_verify: " + @ssl_no_verify.to_s
|
|
|
+ class_eval <<-CODE
|
|
|
+ def test_#{name_for(@get_uri)}_response_code_should_be_#{@http_code}_#{id}
|
|
|
+ check = ResponseCheck.new(:get_uri => '#{@get_uri}')
|
|
|
+ if #{@ssl_no_verify}
|
|
|
+ check.ssl_no_verify = #{@ssl_no_verify}
|
|
|
+ end
|
|
|
+ if '#{@useragent}'.size > 0
|
|
|
+ check.useragent = '#{@useragent}'
|
|
|
+ end
|
|
|
+
|
|
|
+ assert_equal('#{@http_code}', check.response.code,
|
|
|
+ "'" + check.get_uri + "' says '" + check.response.code + "' instead of '#{@http_code}'.")
|
|
|
+ end
|
|
|
+ CODE
|
|
|
+ end
|
|
|
+
|
|
|
+ def self.uri_be_success(options)
|
|
|
+ @get_uri = options.fetch(:uri)
|
|
|
+ @last_uri = @get_uri
|
|
|
+
|
|
|
+ id = Digest::SHA1.hexdigest "#{@get_uri}#{@ssl_no_verify}"
|
|
|
+ id = id[0..7]
|
|
|
+
|
|
|
+ class_eval (
|
|
|
+ <<-CODE
|
|
|
+ def test_#{name_for(@get_uri)}_response_should_be_success_#{id}
|
|
|
+ check = ResponseCheck.new(:get_uri => '#{@get_uri}')
|
|
|
+ if #{@ssl_no_verify}
|
|
|
+ check.ssl_no_verify = #{@ssl_no_verify}
|
|
|
+ end
|
|
|
+ if '#{@useragent}'.size > 0
|
|
|
+ check.useragent = '#{@useragent}'
|
|
|
+ end
|
|
|
+
|
|
|
+ assert_equal(true, check.success?,
|
|
|
+ "'" + check.get_uri + "' says '" + check.response.code + "' instead of '2xx'-something.")
|
|
|
+ end
|
|
|
+ CODE
|
|
|
+ )
|
|
|
+ end
|
|
|
+
|
|
|
+ def self.uri_body_contains(options)
|
|
|
+ @get_uri = options.fetch(:uri, @last_to)
|
|
|
+ contains = nil
|
|
|
+ contains_trueness = false
|
|
|
+ if (options[:strings] != nil) and (options[:strings].is_a?(Array))
|
|
|
+ contains = options[:strings]
|
|
|
+ contains_trueness = true
|
|
|
+ else
|
|
|
+ abort "'uri_body_contains :strings => ['abc']' is missing. Remember, must be array."
|
|
|
+ end
|
|
|
+ @last_uri = @get_uri
|
|
|
+
|
|
|
+ contains.each do |string|
|
|
|
+ id = Digest::SHA1.hexdigest "#{@get_uri}#{string}#{@ssl_no_verify}"
|
|
|
+ id = id[0..7]
|
|
|
+ #puts string
|
|
|
+ class_eval <<-CODE
|
|
|
+ def test_#{name_for(@get_uri)}_response_contains_a_string_#{id}
|
|
|
+ check = ResponseCheck.new(:get_uri => '#{@get_uri}')
|
|
|
+ if #{@ssl_no_verify}
|
|
|
+ check.ssl_no_verify = #{@ssl_no_verify}
|
|
|
+ end
|
|
|
+ if '#{@useragent}'.size > 0
|
|
|
+ check.useragent = '#{@useragent}'
|
|
|
+ end
|
|
|
+
|
|
|
+ to_match = '#{string}'
|
|
|
+ matched = check.body_contains?(to_match)
|
|
|
+ assert_equal(true, matched,
|
|
|
+ "'" + check.get_uri + "' does not contain '" + to_match + "' inside response body.")
|
|
|
+ end
|
|
|
+ CODE
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
+
|
|
|
+ def self.uri_should_redirect(options = {})
|
|
|
+ begin
|
|
|
+ @get_uri = options.fetch(:from, @last_to)
|
|
|
+ wanted_dst_uri = options.fetch(:to)
|
|
|
+ rescue
|
|
|
+ abort "Usage: uri_should_redirect [:from => '',] :to => ''\n :from defaults to @last_to"
|
|
|
+ end
|
|
|
+
|
|
|
+ if @get_uri == wanted_dst_uri
|
|
|
+ abort "uri_should_redirect: :to must be different than :from. Both were '#{wanted_dst_uri}'."
|
|
|
+ end
|
|
|
+ by_code = nil
|
|
|
+ by_code_trueness = false
|
|
|
+ if options[:by] != nil
|
|
|
+ if options[:by].to_i >= 300 and options[:by].to_i < 400
|
|
|
+ by_code = options[:by].to_i
|
|
|
+ @last_code = by_code
|
|
|
+ by_code_trueness = true
|
|
|
+ else
|
|
|
+ abort "uri_should_redirect: :by must be in 300 to 399 range. Not '#{options[:by]}'."
|
|
|
+ end
|
|
|
+ end
|
|
|
+ @last_to = wanted_dst_uri
|
|
|
+ @last_from = @get_uri
|
|
|
+
|
|
|
+ id = Digest::SHA1.hexdigest "#{@get_uri}#{wanted_dst_uri}#{by_code}#{@ssl_no_verify}#{@useragent}"
|
|
|
+ id = id[0..7]
|
|
|
+
|
|
|
+ class_eval <<-CODE
|
|
|
+ def test_#{name_for(@get_uri)}_should_redirect_to_#{name_for(wanted_dst_uri)}_#{id}
|
|
|
+ check = ResponseCheck.new(:get_uri => '#{@get_uri}')
|
|
|
+ check.wanted_dst_uri = '#{wanted_dst_uri}'
|
|
|
+ if #{@ssl_no_verify}
|
|
|
+ check.ssl_no_verify = #{@ssl_no_verify}
|
|
|
+ end
|
|
|
+ if '#{@useragent}'.size > 0
|
|
|
+ check.useragent = '#{@useragent}'
|
|
|
+ end
|
|
|
+
|
|
|
+ assert_equal(true, check.redirect?,
|
|
|
+ "'" + check.get_uri + "' is not redirecting at all (" + check.response.code + "), but we expected a redirection to '" + check.wanted_dst_uri + "'.")
|
|
|
+
|
|
|
+ assert_equal(check.wanted_dst_uri, check.redirected_uri,
|
|
|
+ "'" + check.get_uri + "' is not redirecting to '" + check.wanted_dst_uri + "'.")
|
|
|
+
|
|
|
+ @response = check.response
|
|
|
+
|
|
|
+ if #{by_code_trueness}
|
|
|
+ assert_equal('#{by_code}', check.response.code,
|
|
|
+ "The redirection is not the wanted '#{by_code}' redirect. It's a '" + check.response.code + "'." +
|
|
|
+ "From '" + check.get_uri + "' to '" + check.wanted_dst_uri + "'")
|
|
|
+ end
|
|
|
+ end
|
|
|
+ CODE
|
|
|
+ end
|
|
|
+
|
|
|
+ def self.path_should_redirect(source, options)
|
|
|
+ source_path = ResourcePath.new(source, :param => 'subdir').to_s
|
|
|
+ destination_path = ResourcePath.new(options[:to], :param => 'subdir').to_s
|
|
|
+
|
|
|
+ permanent = options.fetch(:permanent, true)
|
|
|
+
|
|
|
+ class_eval <<-CODE
|
|
|
+ def test_#{name_for(source_path)}_should_redirect_to_#{name_for(destination_path)}
|
|
|
+ check = ResponseCheck.new(:src_path => '#{source_path}', :dst_path => '#{destination_path}')
|
|
|
+ if #{@ssl_no_verify}
|
|
|
+ check.ssl_no_verify = #{@ssl_no_verify}
|
|
|
+ end
|
|
|
+ if '#{@useragent}'.size > 0
|
|
|
+ check.useragent = '#{@useragent}'
|
|
|
+ end
|
|
|
+
|
|
|
+ assert_equal true, check.redirect?, "'#{source_path}' is not redirecting"
|
|
|
+ assert_equal '#{destination_path}', check.redirected_path,
|
|
|
+ "'#{source_path}' is not redirecting to '#{destination_path}'"
|
|
|
+
|
|
|
+ if #{permanent}
|
|
|
+ assert_equal true, check.permanent_redirect?,
|
|
|
+ "The redirection from '#{source_path}' to '#{destination_path}' doesn't appear to be a permanent redirect"
|
|
|
+ end
|
|
|
+ end
|
|
|
+ CODE
|
|
|
+
|
|
|
+ end
|
|
|
+
|
|
|
+ def self.path_should_not_redirect(path)
|
|
|
+ class_eval <<-CODE
|
|
|
+ def test_#{name_for(path)}_should_not_redirect
|
|
|
+ check = ResponseCheck.new(:src_path => '#{path}')
|
|
|
+ if #{@ssl_no_verify}
|
|
|
+ check.ssl_no_verify = #{@ssl_no_verify}
|
|
|
+ end
|
|
|
+ if '#{@useragent}'.size > 0
|
|
|
+ check.useragent = '#{@useragent}'
|
|
|
+ end
|
|
|
+
|
|
|
+ assert_equal false, check.redirect?, "#{path} is redirecting"
|
|
|
+ assert_equal true, check.success?, "#{path} is not a success response"
|
|
|
+ end
|
|
|
+ CODE
|
|
|
+ end
|
|
|
+
|
|
|
+ private
|
|
|
+
|
|
|
+ def self.name_for(path)
|
|
|
+ name = path.gsub(/\W+/, '_')
|
|
|
+ name.gsub!(/^_+/, '')
|
|
|
+ name.gsub!(/_+$/, '')
|
|
|
+
|
|
|
+ name = 'root' if name == ''
|
|
|
+
|
|
|
+ name
|
|
|
+ end
|
|
|
+
|
|
|
+end
|