From 79537632ac62957f1de6f189878d6757b0dd6abb Mon Sep 17 00:00:00 2001 From: Gibheer Date: Thu, 11 Oct 2012 06:38:55 +0200 Subject: added new router to the tools This small router is intended to work like URLMap, but with the feature that it can extract variables from routes. --- spec/integration/router_spec.rb | 49 +++++++++++++++++++++++++++++++++++++++++ spec/spec_helper.rb | 14 ++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 spec/integration/router_spec.rb create mode 100644 spec/spec_helper.rb (limited to 'spec') diff --git a/spec/integration/router_spec.rb b/spec/integration/router_spec.rb new file mode 100644 index 0000000..3be7b3a --- /dev/null +++ b/spec/integration/router_spec.rb @@ -0,0 +1,49 @@ +require 'spec_helper' + +describe Zero::Router do + let(:router) { Zero::Router.new(routes) } + subject { router.call(env) } +# let(:app) do +# lambda {|env| [200, {'Content-Type' => 'text/html'}, 'correct'] } +# end + let(:app) { double } + let(:wrong_app) do + lambda {|env| [200, {'Content-Type' => 'text/html'}, 'Wrong'] } + end + + before :each do + app.should_receive(:call) + end + + context 'a working route' do + let(:routes) { { '/app' => app } } + let(:env) { generate_env('/app') } + it('takes a route') { subject } + end + + context 'select the right route' do + let(:routes) do + { '/wrong' => wrong_app, + '/correct' => app } + end + let(:env) { generate_env('/correct') } + it("selects the correct from multiple routes") { subject } + end + + context 'uses the deepest path' do + let(:routes) { { '/wrong' => wrong_app, + '/deep' => wrong_app, + '/deep/wrong' => wrong_app, + '/deep/correct' => app }} + let(:env) { generate_env('/deep/correct') } + it("finds uses the deepest path first") { subject } + end + + context 'converts parts of the url to parameters' do + let(:routes) { { '/foo/:id' => app } } + let(:env) { generate_env('/foo/42') } + it "should extract variables from the url" do + subject + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..b31fe56 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,14 @@ +require 'zero' + +class SpecApp + attr_reader :env + + def call(env) + @env = env + return [200, {'Content-Type' => 'text/html'}, ['success']] + end +end + +def generate_env(path, options = {}) + Rack::MockRequest.env_for(path, options = {}) +end -- cgit v1.2.3-70-g09d2