require 'rad/spec/controller' rspec do def request rad.workspace.request end def self.with_http with_controller before :all do rad.before_scope :cycle do if defined?(Rad::Processors) and defined?(Rad::Processors::PrepareParams) rad.conveyors.web do |web| web.definitions.delete_if{|pdef, args| pdef == Rad::Processors::PrepareParams} web.build! end end end end end WCALL_OPTIONS = [:controller] attr_writer :wcall_options def wcall_options; @wcall_options ||= {} end def set_wcall options invalid_options = options.keys - WCALL_OPTIONS raise "Unsupported options #{invalid_options}!" unless invalid_options.empty? self.wcall_options = options end def set_controller controller_class set_wcall controller: controller_class end def self.set_controller &block block.must_be.defined before(:each){set_controller block.call} end def wcall *args, &block workspace_variables, params = parse_wcall_arguments *args ccall nil, nil, params, workspace_variables, &block end def post_wcall *args, &block params = args.extract_options! raise "_method variable already set!" if params.include?(:_method) or params.include?('_method') wcall(*(args << params.merge(_method: 'post')), &block) end protected def parse_wcall_arguments *args # parsing params params = args.extract_options! first = args.first workspace_variables = {} if args.size == 1 and first.is_a?(String) and first =~ /^\// workspace_variables[:path] = first elsif (first.is_a?(String) or first.is_a?(Symbol)) and args.size <= 2 workspace_variables[:path] = '/' workspace_variables[:class] = wcall_options[:controller] || raise("not defined wcall controller (use set_wcall controller: SomeController)!") workspace_variables[:method_name] = first workspace_variables.merge! args[1] if args.size > 1 elsif first.is_a?(Class) and args.size <= 3 workspace_variables[:path] = '/' workspace_variables[:class] = args[0] workspace_variables[:method_name] = args[1] workspace_variables.merge! args[2] if args.size > 2 else raise "Invalid input!" end # preparing environment workspace_variables[:env] ||= {} workspace_variables[:env].reverse_merge! Rad::HTTPAdapter.mock_environment # setting request method if request_method = params.delete(:_method) || params.delete('_method') request_method = request_method.to_s.upcase raise "invalid request method :#{request}" unless %w(GET POST PUT DELETE).include? request_method. raise "REQUEST_METHOD variable already set!" if workspace_variables[:env].include? 'REQUEST_METHOD' workspace_variables[:env]['REQUEST_METHOD'] = request_method end workspace_variables[:request] = Rad::Request.new(workspace_variables[:env]) return workspace_variables, params end end