Class: TestContext
- Inherits:
-
Object
- Object
- TestContext
- Defined in:
- lib/rest_test.rb
Instance Attribute Summary collapse
-
#output_html ⇒ Object
writeonly
Sets the attribute output_html.
-
#request_filter ⇒ Object
writeonly
Sets the attribute request_filter.
-
#show_passed ⇒ Object
writeonly
Sets the attribute show_passed.
-
#show_xmlbody ⇒ Object
writeonly
Sets the attribute show_xmlbody.
Instance Method Summary collapse
- #alias_host(old, new) ⇒ Object
- #bold(str) ⇒ Object
- #error(str = nil) ⇒ Object
- #failed ⇒ Object
- #get_binding ⇒ Object
- #green(str) ⇒ Object
-
#initialize(requests) ⇒ TestContext
constructor
A new instance of TestContext.
- #magenta(str) ⇒ Object
- #out(str) ⇒ Object
- #out_clear ⇒ Object
- #out_flush ⇒ Object
- #passed ⇒ Object
- #print_summary ⇒ Object
- #red(str) ⇒ Object
- #request(arg, return_code = nil, xml_check_wanted = true) ⇒ Object
- #skipped ⇒ Object
- #start ⇒ Object
- #substitute_parameters(request) ⇒ Object
- #unsupported ⇒ Object
- #validate_xml(xml, schema_file) ⇒ Object
Constructor Details
#initialize(requests) ⇒ TestContext
Returns a new instance of TestContext.
11 12 13 14 15 16 17 18 |
# File 'lib/rest_test.rb', line 11 def initialize requests @host_aliases = Hash.new @output = "" @requests = requests start end |
Instance Attribute Details
#output_html=(value) ⇒ Object (writeonly)
Sets the attribute output_html
9 10 11 |
# File 'lib/rest_test.rb', line 9 def output_html=(value) @output_html = value end |
#request_filter=(value) ⇒ Object (writeonly)
Sets the attribute request_filter
9 10 11 |
# File 'lib/rest_test.rb', line 9 def request_filter=(value) @request_filter = value end |
#show_passed=(value) ⇒ Object (writeonly)
Sets the attribute show_passed
9 10 11 |
# File 'lib/rest_test.rb', line 9 def show_passed=(value) @show_passed = value end |
#show_xmlbody=(value) ⇒ Object (writeonly)
Sets the attribute show_xmlbody
9 10 11 |
# File 'lib/rest_test.rb', line 9 def show_xmlbody=(value) @show_xmlbody = value end |
Instance Method Details
#alias_host(old, new) ⇒ Object
94 95 96 |
# File 'lib/rest_test.rb', line 94 def alias_host old, new @host_aliases[ old ] = new end |
#bold(str) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/rest_test.rb', line 29 def bold str if @output_html str.gsub! /</, "<" str.gsub! />/, ">" "<b>#{str}</b>" else "\033[1m#{str}\033[0m" end end |
#error(str = nil) ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/rest_test.rb', line 84 def error str = nil error_str = " ERROR" if ( str ) error_str += ": " + str end out red( error_str ) @error += 1 out_flush end |
#failed ⇒ Object
62 63 64 65 66 |
# File 'lib/rest_test.rb', line 62 def failed out red( " FAILED" ) @failed += 1 out_flush end |
#get_binding ⇒ Object
52 53 54 |
# File 'lib/rest_test.rb', line 52 def get_binding return binding() end |
#green(str) ⇒ Object
44 45 46 |
# File 'lib/rest_test.rb', line 44 def green str bold str end |
#magenta(str) ⇒ Object
48 49 50 |
# File 'lib/rest_test.rb', line 48 def magenta str bold str end |
#out(str) ⇒ Object
98 99 100 |
# File 'lib/rest_test.rb', line 98 def out str @output += str + "\n"; end |
#out_clear ⇒ Object
102 103 104 |
# File 'lib/rest_test.rb', line 102 def out_clear @output = "" end |
#out_flush ⇒ Object
106 107 108 109 |
# File 'lib/rest_test.rb', line 106 def out_flush print @output out_clear end |
#passed ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/rest_test.rb', line 68 def passed out green( " PASSED" ) @passed += 1 if ( @show_passed ) out_flush else out_clear end end |
#print_summary ⇒ Object
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/rest_test.rb', line 305 def print_summary undefined = @tested - @unsupported - @failed - @passed - @error - @skipped puts "#tester passed #{@passed}" puts "#tester failed #{@failed}" puts "#tester error #{@error}" puts "#tester skipped #{@unsupported + @skipped + undefined}" puts puts "Total #{@tested} tests" puts " #{@passed} passed" puts " #{@failed} failed" if ( @unsupported > 0 ) puts " #{@unsupported} unsupported" end if ( @error > 0 ) puts " #{@error} errors" end if ( @skipped > 0 ) puts " #{@skipped} skipped" end if ( undefined > 0 ) puts " #{undefined} undefined" end end |
#red(str) ⇒ Object
39 40 41 42 |
# File 'lib/rest_test.rb', line 39 def red str bold str # "\E[31m#{str}\E[30m" end |
#request(arg, return_code = nil, xml_check_wanted = true) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/rest_test.rb', line 111 def request arg, return_code = nil, xml_check_wanted = true @tested += 1 if ( @request_filter && arg !~ /#{@request_filter}/ ) skipped return nil end out bold( "REQUEST: " + arg ) request = @requests.find { |r| r.to_s == arg } if ( !request ) STDERR.puts " Request not defined" return nil end xml_bodies = request.all_children XmlBody if ( !xml_bodies.empty? ) xml_body = xml_bodies[0] out " XMLBODY: " + xml_body.name end xml_results = request.all_children XmlResult if ( !xml_results.empty? ) xml_result = xml_results[0] out " XMLRESULT: " + xml_result.name end out " host: '#{request.host}'" host = request.host.to_s if ( !host || host.empty? ) error "No host defined" return nil end if @host_aliases[ host ] host = @host_aliases[ host ] end out " aliased host: #{host}" begin path = substitute_parameters request rescue ParameterError error return nil end out " Path: " + path splitted_host = host.split( ":" ) host_name = splitted_host[0] host_port = splitted_host[1] out " Host name: #{host_name} port: #{host_port}" if ( request.verb == "GET" ) req = Net::HTTP::Get.new( path ) if ( true||@user ) req.basic_auth( @user, @password ) end response = Net::HTTP.start( host_name, host_port ) do |http| http.request( req ) end if ( response.is_a? Net::HTTPRedirection ) location = URI.parse response["location"] out " Redirected to #{location}, scheme is #{location.scheme}" http = Net::HTTP.new( location.host, location.port ) if location.scheme == "https" http.use_ssl = true end http.start do |http| req = Net::HTTP::Get.new( location.path ) if ( @user ) out " setting user #{@user}" req.basic_auth( @user, @password ) end out " calling #{location.host}, #{location.port}" response = http.request( req ) end end elsif( request.verb == "POST" ) req = Net::HTTP::Post.new( path ) if ( @user ) req.basic_auth( @user, @password ) end response = Net::HTTP.start( host_name, host_port ) do |http| http.request( req, "" ) end elsif( request.verb == "PUT" ) if ( !@data_body ) error "No body data defined for PUT" return nil end if ( xml_body && @show_xmlbody ) out "Request body:" out @data_body end req = Net::HTTP::Put.new( path ) if ( @user ) req.basic_auth( @user, @password ) end response = Net::HTTP.start( host_name, host_port ) do |http| http.request( req, @data_body ) end else STDERR.puts " Test of method '#{request.verb}' not supported yet." unsupported return nil end if ( response ) out " return code: #{response.code}" if ( xml_result && @show_xmlbody ) out "Response body:" out response.body end if ( ( return_code && response.code == return_code.to_s ) || ( response.is_a? Net::HTTPSuccess ) ) if ( xml_check_wanted && xml_result ) if ( xml_result.schema ) schema_file = xml_result.schema else schema_file = xml_result.name + ".xsd" end if ( validate_xml response.body, schema_file ) out " Response validates against schema '#{schema_file}'" passed else failed end else passed end else failed end end response end |
#skipped ⇒ Object
78 79 80 81 82 |
# File 'lib/rest_test.rb', line 78 def skipped # out magenta( " SKIPPED" ) @skipped += 1 out_flush end |
#start ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/rest_test.rb', line 20 def start @tested = 0 @unsupported = 0 @failed = 0 @passed = 0 @error = 0 @skipped = 0 end |
#substitute_parameters(request) ⇒ Object
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/rest_test.rb', line 264 def substitute_parameters request path = request.path.clone request.parameters.each do |parameter| p = parameter.name arg = eval( "@arg_#{parameter.name}" ) if ( !arg ) out " Can't substitute parameter '#{p}'. " + "No variable @arg_#{p} defined." raise ParameterError end path.gsub! /<#{p}>/, arg end path end |
#unsupported ⇒ Object
56 57 58 59 60 |
# File 'lib/rest_test.rb', line 56 def unsupported out magenta( " UNSUPPORTED" ) @unsupported += 1 out_flush end |
#validate_xml(xml, schema_file) ⇒ Object
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/rest_test.rb', line 281 def validate_xml xml, schema_file tmp = Tempfile.new('rest_test_validator') tmp.print xml tmp_path = tmp.path tmp.close found_schema_file = XmlFile.find_file schema_file if ( !found_schema_file ) out " Unable to find schema file '#{schema_file}'" return false end cmd = "/usr/bin/xmllint --noout --schema #{found_schema_file} #{tmp_path} 2>&1" # puts "CMD: " + cmd output = `#{cmd}` if $?.exitstatus > 0 out "xmllint return value: #{$?.exitstatus}" out output return false end return true end |