Skip to content

Commit

Permalink
Merge branch 'master' into 0.4.x
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfonso Uceda committed Jun 29, 2015
2 parents 069eca9 + 8311593 commit bf9763e
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 77 deletions.
80 changes: 15 additions & 65 deletions lib/lotus/routing/http_router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ def find(options)
#
# @since 0.1.0
# @api private
def path(route, *args)
_rescue_url_recognition { super }
def raw_path(route, *args)
_rescue_url_recognition do
_custom_path(super(route, *args))
end
end

# Generate an absolute URL for a specified named route.
Expand All @@ -85,68 +87,10 @@ def path(route, *args)
#
# @since 0.1.0
# @api private
def url(route, *args)
_rescue_url_recognition { super }
end

# Support for GET HTTP verb
#
# @see Lotus::Router#options
#
# @since 0.4.1
# @api private
def get(path, options = {}, &blk)
super(@prefix.join(path), options, &blk)
end

# Support for POST HTTP verb
#
# @see Lotus::Router#post
#
# @since 0.4.1
# @api private
def post(path, options = {}, &blk)
super(@prefix.join(path), options, &blk)
end

# Support for PUT HTTP verb
#
# @see Lotus::Router#put
#
# @since 0.4.1
# @api private
def put(path, options = {}, &blk)
super(@prefix.join(path), options, &blk)
end

# Support for PATCH HTTP verb
#
# @see Lotus::Router#patch
#
# @since 0.4.1
# @api private
def patch(path, options = {}, &blk)
super(@prefix.join(path), options, &blk)
end

# Support for DELETE HTTP verb
#
# @see Lotus::Router#delete
#
# @since 0.4.1
# @api private
def delete(path, options = {}, &blk)
super(@prefix.join(path), options, &blk)
end

# Support for TRACE HTTP verb
#
# @see Lotus::Router#trace
#
# @since 0.4.1
# @api private
def trace(path, options = {}, &blk)
super(@prefix.join(path), options, &blk)
def raw_url(route, *args)
_rescue_url_recognition do
_custom_path(super(route, *args))
end
end

# Support for OPTIONS HTTP verb
Expand All @@ -156,7 +100,7 @@ def trace(path, options = {}, &blk)
# @since 0.1.0
# @api private
def options(path, options = {}, &blk)
add_with_request_method(@prefix.join(path), :options, options, &blk)
add_with_request_method(path, :options, options, &blk)
end

# Allow to mount a Rack app
Expand Down Expand Up @@ -212,6 +156,12 @@ def _rescue_url_recognition
::HttpRouter::TooManyParametersException => e
raise Routing::InvalidRouteException.new(e.message)
end

def _custom_path(uri_string)
uri = URI.parse(uri_string)
uri.path = @prefix.join(uri.path)
uri.to_s
end
end
end
end
26 changes: 26 additions & 0 deletions test/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,32 @@ def call(params)
end
end # Web

module Front
class App
def call(env)
case env['PATH_INFO']
when '/home'
[200, {}, ['front']]
else
[404, {}, ['Not Found']]
end
end
end
end # Front

module Back
class App
def call(env)
case env['PATH_INFO']
when '/home'
[200, {}, ['back']]
else
[404, {}, ['Not Found']]
end
end
end
end # Back

module Api
class App
def call(env)
Expand Down
5 changes: 5 additions & 0 deletions test/integration/full_stack_application_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

describe 'Lotus integration' do
before do
@router_container = Lotus::Router.new(scheme: 'https', host: 'lotus.test', port: 443) do
mount Dashboard::Index, at: '/dashboard'
mount Backend::App, at: '/backend'
end

@routes = Lotus::Router.new(namespace: Travels::Controllers) do
get '/dashboard', to: 'journeys#index'
resources :journeys, only: [:index]
Expand Down
16 changes: 16 additions & 0 deletions test/integration/router_container_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'test_helper'

describe 'Router wrapper as container' do
it 'reach correct application' do
@router_container = Lotus::Router.new(scheme: 'https', host: 'lotus.test', port: 443) do
mount Front::App, at: '/front'
mount Back::App, at: '/back'
end

@app = Rack::MockRequest.new(@router_container)
response = @app.get('/front/home')
response.body.must_equal 'front'
response = @app.get('/back/home')
response.body.must_equal 'back'
end
end
8 changes: 4 additions & 4 deletions test/namespace_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@
@app.request('PATCH', '/electronics/keyboards/23').status.must_equal 405
@app.request('DELETE', '/electronics/keyboards/23').status.must_equal 405

-> { @router.path(:new_electronics_keyboards) }.must_raise HttpRouter::InvalidRouteException
-> { @router.path(:new_electronics_keyboards) }.must_raise Lotus::Routing::InvalidRouteException
end
end

Expand All @@ -208,7 +208,7 @@
@app.request('PATCH', '/electronics/keyboards/23').status.must_equal 405
@app.request('DELETE', '/electronics/keyboards/23').status.must_equal 405

-> { @router.path(:new_electronics_keyboards) }.must_raise HttpRouter::InvalidRouteException
-> { @router.path(:new_electronics_keyboards) }.must_raise Lotus::Routing::InvalidRouteException
end
end

Expand Down Expand Up @@ -292,7 +292,7 @@
@app.request('POST', '/settings/profile').status.must_equal 405
@app.request('DELETE', '/settings/profile').status.must_equal 405

-> { @router.path(:new_settings_profile) }.must_raise HttpRouter::InvalidRouteException
-> { @router.path(:new_settings_profile) }.must_raise Lotus::Routing::InvalidRouteException
end
end

Expand Down Expand Up @@ -320,7 +320,7 @@
it 'does not recognize other paths' do
@app.request('GET', '/settings/profile/edit').status.must_equal 404

-> { @router.path(:edit_settings_profile) }.must_raise HttpRouter::InvalidRouteException
-> { @router.path(:edit_settings_profile) }.must_raise Lotus::Routing::InvalidRouteException
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions test/prefix_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def call(env)

%w(GET POST PUT PATCH DELETE TRACE OPTIONS).each do |verb|
it "recognizes requests (#{ verb })" do
env = Rack::MockRequest.env_for('/admin/home', method: verb)
env = Rack::MockRequest.env_for('/home', method: verb)
status, _, body = @router.call(env)

status.must_equal 200
Expand All @@ -104,15 +104,15 @@ def call(env)
end

it "recognizes RESTful resources" do
env = Rack::MockRequest.env_for('/admin/users')
env = Rack::MockRequest.env_for('/users')
status, _, body = @router.call(env)

status.must_equal 200
body.must_equal ['users']
end

it "recognizes RESTful resource" do
env = Rack::MockRequest.env_for('/admin/asteroid')
env = Rack::MockRequest.env_for('/asteroid')
status, _, body = @router.call(env)

status.must_equal 200
Expand All @@ -125,7 +125,7 @@ def call(env)
get '/redirect_destination', to: ->(env) { [200, {}, ['Redirect destination!']] }
end

env = Rack::MockRequest.env_for('/admin/redirect')
env = Rack::MockRequest.env_for('/redirect')
status, headers, _ = router.call(env)

status.must_equal 301
Expand Down
4 changes: 2 additions & 2 deletions test/resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
@app.request('GET', '/profile').status.must_equal 405
@app.request('DELETE', '/profile').status.must_equal 405

-> { @router.path(:new_profile) }.must_raise HttpRouter::InvalidRouteException
-> { @router.path(:new_profile) }.must_raise Lotus::Routing::InvalidRouteException
end
end

Expand All @@ -87,7 +87,7 @@
@app.request('GET', '/profile').status.must_equal 405
@app.request('DELETE', '/profile').status.must_equal 405

-> { @router.path(:new_profile) }.must_raise HttpRouter::InvalidRouteException
-> { @router.path(:new_profile) }.must_raise Lotus::Routing::InvalidRouteException
end
end

Expand Down
4 changes: 2 additions & 2 deletions test/resources_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
@app.request('PATCH', '/keyboards/23').status.must_equal 405
@app.request('DELETE', '/keyboards/23').status.must_equal 405

-> { @router.path(:new_keyboards) }.must_raise HttpRouter::InvalidRouteException
-> { @router.path(:new_keyboards) }.must_raise Lotus::Routing::InvalidRouteException
end
end

Expand All @@ -95,7 +95,7 @@
@app.request('PATCH', '/keyboards/23').status.must_equal 405
@app.request('DELETE', '/keyboards/23').status.must_equal 405

-> { @router.path(:new_keyboards) }.must_raise HttpRouter::InvalidRouteException
-> { @router.path(:new_keyboards) }.must_raise Lotus::Routing::InvalidRouteException
end
end

Expand Down
23 changes: 23 additions & 0 deletions test/routing/endpoint_resolver_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,29 @@
end
end

describe 'endpoint with nested routes' do
before :all do
class NestedRoutesApp
def call(env)
end

def routes
Lotus::Router.new do
get '/home', to: 'home#index'
end
end
end
end

it 'responds to :routes' do
@resolver.resolve(to: NestedRoutesApp).respond_to?(:routes).must_equal true
end

after do
Object.send(:remove_const, :NestedRoutesApp)
end
end

describe 'custom endpoint' do
before :all do
class CustomEndpoint
Expand Down

0 comments on commit bf9763e

Please sign in to comment.