http_response_test.rb 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. require './resource_path'
  2. require './response_check'
  3. require 'test/unit'
  4. require 'digest'
  5. class HTTPResponseTest < Test::Unit::TestCase
  6. def default_test; end # placeholder to stop Test::Unit from complaining
  7. def self.useragent=(useragent)
  8. ResponseCheck.useragent = useragent
  9. end
  10. def self.reset_useragent
  11. ResponseCheck.useragent = nil
  12. end
  13. def self.ssl_no_verify=(verify)
  14. ResponseCheck.ssl_no_verify = verify
  15. end
  16. # no accessors needed, as every class that is derived from this one can be adjusted featurewise by just setting:
  17. # @useragent
  18. # @ssl_no_verify
  19. def self.uri_be_code(options)
  20. @get_uri = options[:uri].to_s
  21. @http_code = options[:code].to_s
  22. @last_uri = @get_uri
  23. @last_code = @http_code
  24. id = Digest::SHA1.hexdigest "#{@get_uri}#{@http_code}#{@ssl_no_verify}#{@useragent}"
  25. id = id[0..7]
  26. #puts "uri to be code ssl_no_verify: " + @ssl_no_verify.to_s
  27. class_eval <<-CODE
  28. def test_#{name_for(@get_uri)}_response_code_should_be_#{@http_code}_#{id}
  29. check = ResponseCheck.new(:get_uri => '#{@get_uri}')
  30. if #{@ssl_no_verify}
  31. check.ssl_no_verify = #{@ssl_no_verify}
  32. end
  33. if '#{@useragent}'.size > 0
  34. check.useragent = '#{@useragent}'
  35. end
  36. assert_equal('#{@http_code}', check.response.code,
  37. "'" + check.get_uri + "' says '" + check.response.code + "' instead of '#{@http_code}'.")
  38. end
  39. CODE
  40. end
  41. def self.uri_be_success(options)
  42. @get_uri = options.fetch(:uri)
  43. @last_uri = @get_uri
  44. id = Digest::SHA1.hexdigest "#{@get_uri}#{@ssl_no_verify}"
  45. id = id[0..7]
  46. class_eval (
  47. <<-CODE
  48. def test_#{name_for(@get_uri)}_response_should_be_success_#{id}
  49. check = ResponseCheck.new(:get_uri => '#{@get_uri}')
  50. if #{@ssl_no_verify}
  51. check.ssl_no_verify = #{@ssl_no_verify}
  52. end
  53. if '#{@useragent}'.size > 0
  54. check.useragent = '#{@useragent}'
  55. end
  56. assert_equal(true, check.success?,
  57. "'" + check.get_uri + "' says '" + check.response.code + "' instead of '2xx'-something.")
  58. end
  59. CODE
  60. )
  61. end
  62. def self.uri_body_contains(options)
  63. @get_uri = options.fetch(:uri, @last_to)
  64. contains = nil
  65. contains_trueness = false
  66. if (options[:strings] != nil) and (options[:strings].is_a?(Array))
  67. contains = options[:strings]
  68. contains_trueness = true
  69. else
  70. abort "'uri_body_contains :strings => ['abc']' is missing. Remember, must be array."
  71. end
  72. @last_uri = @get_uri
  73. contains.each do |string|
  74. id = Digest::SHA1.hexdigest "#{@get_uri}#{string}#{@ssl_no_verify}"
  75. id = id[0..7]
  76. #puts string
  77. class_eval <<-CODE
  78. def test_#{name_for(@get_uri)}_response_contains_a_string_#{id}
  79. check = ResponseCheck.new(:get_uri => '#{@get_uri}')
  80. if #{@ssl_no_verify}
  81. check.ssl_no_verify = #{@ssl_no_verify}
  82. end
  83. if '#{@useragent}'.size > 0
  84. check.useragent = '#{@useragent}'
  85. end
  86. to_match = '#{string}'
  87. matched = check.body_contains?(to_match)
  88. assert_equal(true, matched,
  89. "'" + check.get_uri + "' does not contain '" + to_match + "' inside response body.")
  90. end
  91. CODE
  92. end
  93. end
  94. def self.uri_should_redirect(options = {})
  95. begin
  96. @get_uri = options.fetch(:from, @last_to)
  97. wanted_dst_uri = options.fetch(:to)
  98. rescue
  99. abort "Usage: uri_should_redirect [:from => '',] :to => ''\n :from defaults to @last_to"
  100. end
  101. if @get_uri == wanted_dst_uri
  102. abort "uri_should_redirect: :to must be different than :from. Both were '#{wanted_dst_uri}'."
  103. end
  104. by_code = nil
  105. by_code_trueness = false
  106. if options[:by] != nil
  107. if options[:by].to_i >= 300 and options[:by].to_i < 400
  108. by_code = options[:by].to_i
  109. @last_code = by_code
  110. by_code_trueness = true
  111. else
  112. abort "uri_should_redirect: :by must be in 300 to 399 range. Not '#{options[:by]}'."
  113. end
  114. end
  115. @last_to = wanted_dst_uri
  116. @last_from = @get_uri
  117. id = Digest::SHA1.hexdigest "#{@get_uri}#{wanted_dst_uri}#{by_code}#{@ssl_no_verify}#{@useragent}"
  118. id = id[0..7]
  119. class_eval <<-CODE
  120. def test_#{name_for(@get_uri)}_should_redirect_to_#{name_for(wanted_dst_uri)}_#{id}
  121. check = ResponseCheck.new(:get_uri => '#{@get_uri}')
  122. check.wanted_dst_uri = '#{wanted_dst_uri}'
  123. if #{@ssl_no_verify}
  124. check.ssl_no_verify = #{@ssl_no_verify}
  125. end
  126. if '#{@useragent}'.size > 0
  127. check.useragent = '#{@useragent}'
  128. end
  129. assert_equal(true, check.redirect?,
  130. "'" + check.get_uri + "' is not redirecting at all (" + check.response.code + "), but we expected a redirection to '" + check.wanted_dst_uri + "'.")
  131. assert_equal(check.wanted_dst_uri, check.redirected_uri,
  132. "'" + check.get_uri + "' is not redirecting to '" + check.wanted_dst_uri + "'.")
  133. @response = check.response
  134. if #{by_code_trueness}
  135. assert_equal('#{by_code}', check.response.code,
  136. "The redirection is not the wanted '#{by_code}' redirect. It's a '" + check.response.code + "'." +
  137. "From '" + check.get_uri + "' to '" + check.wanted_dst_uri + "'")
  138. end
  139. end
  140. CODE
  141. end
  142. def self.path_should_redirect(source, options)
  143. source_path = ResourcePath.new(source, :param => 'subdir').to_s
  144. destination_path = ResourcePath.new(options[:to], :param => 'subdir').to_s
  145. permanent = options.fetch(:permanent, true)
  146. class_eval <<-CODE
  147. def test_#{name_for(source_path)}_should_redirect_to_#{name_for(destination_path)}
  148. check = ResponseCheck.new(:src_path => '#{source_path}', :dst_path => '#{destination_path}')
  149. if #{@ssl_no_verify}
  150. check.ssl_no_verify = #{@ssl_no_verify}
  151. end
  152. if '#{@useragent}'.size > 0
  153. check.useragent = '#{@useragent}'
  154. end
  155. assert_equal true, check.redirect?, "'#{source_path}' is not redirecting"
  156. assert_equal '#{destination_path}', check.redirected_path,
  157. "'#{source_path}' is not redirecting to '#{destination_path}'"
  158. if #{permanent}
  159. assert_equal true, check.permanent_redirect?,
  160. "The redirection from '#{source_path}' to '#{destination_path}' doesn't appear to be a permanent redirect"
  161. end
  162. end
  163. CODE
  164. end
  165. def self.path_should_not_redirect(path)
  166. class_eval <<-CODE
  167. def test_#{name_for(path)}_should_not_redirect
  168. check = ResponseCheck.new(:src_path => '#{path}')
  169. if #{@ssl_no_verify}
  170. check.ssl_no_verify = #{@ssl_no_verify}
  171. end
  172. if '#{@useragent}'.size > 0
  173. check.useragent = '#{@useragent}'
  174. end
  175. assert_equal false, check.redirect?, "#{path} is redirecting"
  176. assert_equal true, check.success?, "#{path} is not a success response"
  177. end
  178. CODE
  179. end
  180. private
  181. def self.name_for(path)
  182. name = path.gsub(/\W+/, '_')
  183. name.gsub!(/^_+/, '')
  184. name.gsub!(/_+$/, '')
  185. name = 'root' if name == ''
  186. name
  187. end
  188. end