Skip to content

Instantly share code, notes, and snippets.

@adamluzsi
Last active March 27, 2016 18:31
Show Gist options
  • Save adamluzsi/f424de92447daf37ccd9 to your computer and use it in GitHub Desktop.
Save adamluzsi/f424de92447daf37ccd9 to your computer and use it in GitHub Desktop.
How to mount any Rack based application into Rack::App
require 'rack/app'
require './rack_based_application.rb'
class App < Rack::App
mount_rack_based_application RackBasedApplication
namespace :mount do
mount_rack_based_application RackBasedApplication, :to => '/point'
end
end
require 'spec_helper'
describe do
include Rack::App::Test
rack_app App
it { expect(get('/').body).to eq 'endpoints have bigger priority than mounted applications' }
it { expect(get('/hello/world/test/endpoint').body).to eq 'Hello, World!' }
it { expect(post('/hello/world/test/endpoint').body).to eq 'Hello, World!' }
it { expect(put('/hello/world/test/endpoint').body).to eq 'Hello, World!' }
it { expect(delete('/hello/world/test/endpoint').body).to eq 'Hello, World!' }
it { expect(patch('/hello/world/test/endpoint').body).to eq 'Hello, World!' }
it { expect(head('/hello/world/test/endpoint').body).to eq 'Hello, World!' }
it { expect(options('/hello/world/test/endpoint').body).to eq 'Hello, World!' }
it 'should mount to the correct namespace and mount point' do
expect(get('/mount/point/hello/world/test/endpoint').body).to eq 'Hello, World!'
end
it { expect(get('/hello/world/to_you').body).to eq 'partially matching endpoint' }
end
require './app'
run App
class RackBasedApplication
def self.call(env)
new.call(env)
end
def call(env)
request_path = env[::Rack::PATH_INFO]
if request_path == '/'
['200', {'Content-Type' => 'text/html'}, ['static endpoint']]
elsif request_path =~ /^\/users\/.*/
['200', {'Content-Type' => 'text/html'}, ['dynamic endpoint']]
elsif request_path == '/hello/world/test/endpoint'
['200', {'Content-Type' => 'text/html'}, ['Hello, World!']]
else
['404', {}, ['404 Not Found!']]
end
end
end
require 'rack/app/test'
require_relative '../app'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment