From: Cornelius Schumacher
---
docs/api/rest.rb | 9 +++
docs/api/rest_test | 50 +++++++++++++-
docs/api/rest_test.rb | 177 +++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 233 insertions(+), 3 deletions(-)
create mode 100644 docs/api/rest_test.rb
diff --git a/docs/api/rest.rb b/docs/api/rest.rb
index 4d0d888..94c307a 100755
--- a/docs/api/rest.rb
+++ b/docs/api/rest.rb
@@ -352,6 +352,15 @@ class TextPrinter < Printer
puts "Result: " + result.name
end
+ def print_xmlresult result
+ indent result
+ printf "XmlResult: " + result.name
+ if ( result.schema )
+ printf " (Schema: #{result.schema})"
+ end
+ printf "\n"
+ end
+
def print_body body
indent body
puts "Body: " + body.name
diff --git a/docs/api/rest_test b/docs/api/rest_test
index 6220948..b0f04fb 100755
--- a/docs/api/rest_test
+++ b/docs/api/rest_test
@@ -10,10 +10,12 @@ include ERB::Util
require "ftools"
require "rest.rb"
+require "rest_test.rb"
class Options
- attr_accessor :verbose, :filename, :html, :outline, :output_dir
+ attr_accessor :verbose, :filename, :html, :outline, :output_dir,
+ :create_template, :testfile, :force
def initialize
verbose = false
@@ -36,6 +38,18 @@ class Options
options.verbose = true
end
+ opt.on( "--create-test", "Create test template" ) do
+ options.create_template = true
+ end
+
+ opt.on( "--test=testfile", "Select test file" ) do |val|
+ options.testfile = val
+ end
+
+ opt.on( "--force", "Force overwriting of test file" ) do
+ options.force = true
+ end
+
begin
opt.parse!( args )
rescue OptionParser::InvalidOption
@@ -65,15 +79,45 @@ options = Options.parse( ARGV )
begin
+ if ( !options.testfile )
+ options.testfile = options.filename + ".test"
+ end
+
document = Document.new
document.parse_args
@requests = document.all_children Request
+
+ if ( options.create_template )
+ if ( File.exists?( options.testfile ) && !options.force )
+ STDERR.puts "Test file '#{options.testfile} already exists." +
+ " Use '--force' to overwrite it. Exiting."
+ end
+ File.open( options.testfile, "w" ) do |file|
+ @requests.each do |r|
+ file.puts "request \"#{r}\""
+ end
+ end
+ end
+
+ runner = TestRunner.new @requests
+
+ user = nil
+ password = nil
- @requests.each do |r|
- puts r
+ configfile = options.testfile + ".config"
+ File.open configfile do |file|
+ eval file.read
+ end
+
+ runner.context.user = user
+ runner.context.password = password
+ if ( options.verbose )
+ runner.context.show_body = true
end
+ runner.run options.testfile
+
rescue Errno::ENOENT
puts $!
end
diff --git a/docs/api/rest_test.rb b/docs/api/rest_test.rb
new file mode 100644
index 0000000..4e79077
--- /dev/null
+++ b/docs/api/rest_test.rb
@@ -0,0 +1,177 @@
+require "net/http"
+require "tempfile"
+
+class TestContext
+
+ attr_writer :user, :password, :show_body
+
+ def initialize requests
+ @requests = requests
+ start
+ end
+
+ def start
+ @tested = 0
+ @unsupported = 0
+ @failed = 0
+ @passed = 0
+ end
+
+ def bold str
+ "\033[1m#{str}\033[0m"
+ end
+
+ def red str
+ bold str
+# "\E[31m#{str}\E[30m"
+ end
+
+ def green str
+ str
+ end
+
+ def magenta str
+ bold str
+ end
+
+ def get_binding
+ return binding()
+ end
+
+ def unsupported
+ puts magenta( " UNSUPPORTED" )
+ @unsupported += 1
+ end
+
+ def failed
+ puts red( " FAILED" )
+ @failed += 1
+ end
+
+ def passed
+ puts green( " PASSED" )
+ @passed += 1
+ end
+
+ def request arg
+ puts bold( "REQUEST: " + arg )
+
+ @tested += 1
+
+ request = @requests.find { |r| r.to_s == arg }
+
+ if ( !request )
+ STDERR.puts " Request not defined"
+ return
+ end
+
+ xml_results = request.all_children XmlResult
+ if ( !xml_results.empty? )
+ xml_result = xml_results[0]
+ puts " XMLRESULT: " + xml_result.name
+ end
+
+ puts " host: '#{request.host}'"
+
+ host = request.host.to_s
+ if ( !host || host.empty? )
+ STDERR.puts " No host defined."
+ return
+ end
+
+ if ( request.verb == "GET" )
+ req = Net::HTTP::Get.new( request.path )
+ req.basic_auth( @user, @password )
+ response = Net::HTTP.start( host ) do |http|
+ http.request( req )
+ end
+ else
+ STDERR.puts " Test of method '#{request.verb}' not supported yet."
+ unsupported
+ return
+ end
+
+ parameters = request.parameters
+ if ( !parameters.empty? )
+ STDERR.puts " Parameter substitution not supported yet."
+ unsupported
+ return
+ end
+
+ if ( response )
+ puts " return code: #{response.code}"
+ if ( @show_body )
+ puts response.body
+ end
+
+ if ( response.is_a? Net::HTTPSuccess )
+ if ( 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 )
+ puts " Response validates against schema '#{schema_file}'"
+ passed
+ else
+ failed
+ end
+ else
+ passed
+ end
+ else
+ failed
+ end
+ end
+
+ end
+
+ def validate_xml xml, schema_file
+ tmp = Tempfile.new('rest_test_validator')
+ tmp.print xml
+ tmp_path = tmp.path
+ tmp.close
+
+ out = `/usr/bin/xmllint --noout --schema #{schema_file} #{tmp_path} 2>&1`
+ if $?.exitstatus > 0
+ STDERR.puts "xmllint return value: #{$?.exitstatus}"
+ STDERR.puts out
+ return false
+ end
+ return true
+ end
+
+ def print_summary
+ error = @tested - @unsupported - @failed - @passed
+
+ puts "Total #{@tested} tests"
+ puts " #{@passed} passed"
+ puts " #{@failed} failed"
+ if ( @unsupported )
+ puts " #{@unsupported} unsupported"
+ end
+ if ( error )
+ puts " #{error} errors"
+ end
+ end
+
+end
+
+class TestRunner
+
+ attr_reader :context
+
+ def initialize requests
+ @context = TestContext.new requests
+ end
+
+ def run testfile
+ File.open testfile do |file|
+ eval file.read, @context.get_binding
+ end
+
+ @context.print_summary
+ end
+
+end
--
1.7.7
--
To unsubscribe, e-mail: obs-commits+unsubscribe@opensuse.org
To contact the owner, e-mail: obs-commits+owner@opensuse.org