# 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