Module: Rack::App::Test

Defined in:
lib/rack/app/test.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#last_responseObject (readonly)

Returns the value of attribute last_response.



11
12
13
# File 'lib/rack/app/test.rb', line 11

def last_response
  @last_response
end

Class Method Details

.included(klass) ⇒ Object



7
8
9
# File 'lib/rack/app/test.rb', line 7

def self.included(klass)
  klass.__send__(:extend, self::SingletonMethods)
end

Instance Method Details

#__send_rack_app_request__(request_method, *args) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/rack/app/test.rb', line 13

def __send_rack_app_request__(request_method, *args)
  properties = args.select { |e| e.is_a?(Hash) }.reduce({}, &:merge!)
  url = args.detect { |e| e.is_a?(String) } || properties.delete(:url)
  mock_request = Rack::MockRequest.new(rack_app)
  request_env = Rack::App::Test::Utils.env_by(url, properties)
  @last_response = mock_request.request(request_method.to_s.upcase, url, request_env)
end

#mount(app_class, options) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rack/app/test.rb', line 40

def mount(app_class, options)
  path_prefix = options.fetch(:to)

  selector = lambda do |e|
    if e.config.type == :endpoint
      e.config.app_class == app_class
    else
      e.config.callable == app_class
    end
  end

  endpoints = rack_app.router.endpoints.select(&selector)

  request_paths_that_has_prefix = lambda do |e|
    e.request_path.start_with?(path_prefix)
  end

  matching_endpoints = endpoints.select(&request_paths_that_has_prefix)

  if matching_endpoints.empty?
    raise("Can't find any path that fullfill the requirement")
  end

  return unless app_class.is_a?(Class) && app_class <= Rack::App
  app_owned_endpoints = app_class.router.endpoints.select(&selector)

  if matching_endpoints.length != app_owned_endpoints.length
    raise('endpoint count not matching')
  end
end

#rack_app(&block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rack/app/test.rb', line 27

def rack_app(&block)
  @rack_app ||= lambda do
    if defined?(__rack_app_class__)
      __rack_app_class__
    elsif defined?(described_class) && described_class.respond_to?(:call)
      described_class
    else
      raise('missing class definition')
    end
  end.call
  block.is_a?(Proc) ? @rack_app.instance_exec(&block) : @rack_app
end