lib/hanami/routes.rb in hanami-0.7.3 vs lib/hanami/routes.rb in hanami-0.8.0

- old
+ new

@@ -133,10 +133,76 @@ # # => 'https://bookshelf.org/login?return_to=%2Fdashboard' def url(name, *args) Utils::Escape::SafeString.new(@routes.url(name, *args)) end + # Recognize a route from a Rack env. + # + # This method is designed for testing purposes + # + # @param env [Hash] a Rack env + # + # @return [Hanami::Routing::RecognizedRoute] the recognized route + # + # @since 0.8.0 + # + # @see http://hanamirb.org/guides/routing/testing + # + # @example Path Generation + # # spec/web/routes_spec.rb + # RSpec.describe Web::Routes do + # it 'generates "/"' do + # actual = described_class.path(:root) + # expect(actual).to eq '/' + # end + # + # it 'generates "/books/23"' do + # actual = described_class.path(:book, id: 23) + # expect(actual).to eq '/books/23' + # end + # end + # + # @example Route Recognition + # # spec/web/routes_spec.rb + # RSpec.describe Web::Routes do + # + # # ... + # + # it 'recognizes "GET /"' do + # env = Rack::MockRequest.env_for('/') + # route = described_class.recognize(env) + # + # expect(route).to be_routable + # + # expect(route.path).to eq '/' + # expect(route.verb).to eq 'GET' + # expect(route.params).to eq({}) + # end + # + # it 'recognizes "PATCH /books/23"' do + # env = Rack::MockRequest.env_for('/books/23', method: 'PATCH') + # route = described_class.recognize(env) + # + # expect(route).to be_routable + # + # expect(route.path).to eq '/books/23' + # expect(route.verb).to eq 'PATCH' + # expect(route.params).to eq(id: '23') + # end + # + # it 'does not recognize unknown route' do + # env = Rack::MockRequest.env_for('/foo') + # route = described_class.recognize(env) + # + # expect(route).to_not be_routable + # end + # end + def recognize(env) + @routes.recognize(env) + end + protected + # @since 0.3.0 # @api private def method_missing(m, *args) named_route, type = m.to_s.split(/\_(path|url)\z/)