diff --git a/lib/sinatra.rb b/lib/sinatra.rb index eb7c18d3..c68f0156 100644 --- a/lib/sinatra.rb +++ b/lib/sinatra.rb @@ -160,10 +160,13 @@ module Sinatra def invoke(request) params = {} - if options[:agent] - return unless request.user_agent =~ options[:agent] + if agent = options[:agent] + return unless request.user_agent =~ agent params[:agent] = $~[1..-1] end + if host = options[:host] + return unless host === request.host + end return unless pattern =~ request.path_info.squeeze('/') params.merge!(param_keys.zip($~.captures.map(&:from_param)).to_hash) Result.new(block, params, 200) diff --git a/test/diddy_test.rb b/test/diddy_test.rb new file mode 100644 index 00000000..a6a1cfbe --- /dev/null +++ b/test/diddy_test.rb @@ -0,0 +1,41 @@ +require File.dirname(__FILE__) + '/helper' + +context "Diddy" do + + setup do + Sinatra.application = nil + end + + specify "should map urls to different apps" do + + get '/' do + 'asdf' + end + + get_it '/' + assert ok? + assert_equal('asdf', body) + + get '/foo', :host => 'foo.sinatrarb.com' do + 'in foo!' + end + + get '/foo', :host => 'bar.sinatrarb.com' do + 'in bar!' + end + + get_it '/foo', {}, 'HTTP_HOST' => 'foo.sinatrarb.com' + assert ok? + assert_equal 'in foo!', body + + get_it '/foo', {}, 'HTTP_HOST' => 'bar.sinatrarb.com' + assert ok? + assert_equal 'in bar!', body + + get_it '/foo' + assert not_found? + + end + +end +