Last active
March 27, 2016 18:31
-
-
Save adamluzsi/f424de92447daf37ccd9 to your computer and use it in GitHub Desktop.
How to mount any Rack based application into Rack::App
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require './app' | |
run App |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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