Module: Fastr::Test::Controller

Defined in:
lib/fastr/test/controller.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



7
8
9
10
# File 'lib/fastr/test/controller.rb', line 7

def self.included(klass)
  klass.extend(ClassMethods)
  set_defaults(klass)
end

.set_defaults(klass) ⇒ Object

Sets up the test class.



13
14
15
16
17
18
19
20
# File 'lib/fastr/test/controller.rb', line 13

def self.set_defaults(klass)
  regex = Regexp.new(/^(\w+)ControllerTest$/)
  match = regex.match(klass.to_s)
  
  if not match.nil?
    klass.instance_variable_set(:@controller_class, match[1].to_s.uncamelcase)
  end
end

Instance Method Details

#get(action, options = {}) ⇒ Object



42
43
44
# File 'lib/fastr/test/controller.rb', line 42

def get(action, options={})
  make_request(:get, action, options)
end

#get_controller_classObject

Raises:

  • (Exception)


65
66
67
68
69
# File 'lib/fastr/test/controller.rb', line 65

def get_controller_class
  klass = self.class.instance_variable_get(:@controller_class)
  raise Exception.new("No controller set. Use set_controller.") if klass.nil?
  return klass
end

#make_request(method, action, options = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fastr/test/controller.rb', line 50

def make_request(method, action, options={})
  run_test_app do |app|
    env = {"PATH_INFO" => "/", "REQUEST_METHOD" => method.to_s.upcase}
    case method
    when :get then
      env['QUERY_STRING'] = Fastr::HTTP.build_query_string(options[:params]) if options[:params]
    when :post then
      env['rack.input'] = StringIO.new(Fastr::HTTP.build_query_string(options[:params])) if options[:params]
    end
    
    app.route = {:ok => {:controller => get_controller_class, :action => action}}
    app.dispatch(env)
  end
end

#post(action, options = {}) ⇒ Object



46
47
48
# File 'lib/fastr/test/controller.rb', line 46

def post(action, options={})
  make_request(:post, action, options)
end

#run_test_app(&block) ⇒ Object

Runs a test application.

Parameters:

  • The

    code to run.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fastr/test/controller.rb', line 25

def run_test_app(&block)
  EM.kqueue = true if EM.kqueue?
  EM.epoll = true if EM.epoll?
  EM.run do
    app_path = Dir.pwd
    app = Fastr::Test::Application.new(app_path)
    app.boot
    setup_dumb_router(app)
    return block.call(app)
  end
end

#setup_dumb_router(app) ⇒ Object

Sets up a router that has one route. We will always use / when calling dispatch.



38
39
40
# File 'lib/fastr/test/controller.rb', line 38

def setup_dumb_router(app)
  app.router.for '/', :to => "dummy#index"
end