sayso-routing-filter 0.2.2.001

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,92 @@
1
+ module Recognition
2
+ # 1 filter
3
+
4
+ test 'recognizes the path /some.html (extension)' do
5
+ params = self.params
6
+ assert_equal params, routes.recognize_path('/some.html')
7
+ end
8
+
9
+ test 'recognizes the path /de/some (locale)' do
10
+ params = self.params.merge(:locale => 'de')
11
+ assert_equal params, routes.recognize_path('/de/some')
12
+ end
13
+
14
+ test 'recognizes the path /some/page/2 (pagination)' do
15
+ params = self.params.merge(:page => 2)
16
+ assert_equal params, routes.recognize_path('/some/page/2')
17
+ end
18
+
19
+ test 'recognizes the path /:uuid/some (uuid)' do
20
+ params = self.params.merge(:uuid => uuid)
21
+ assert_equal params, routes.recognize_path("/#{uuid}/some")
22
+ end
23
+
24
+ # extension with any
25
+
26
+ test 'recognizes the path /de/some.html (extension, locale)' do
27
+ params = self.params.merge(:locale => 'de')
28
+ assert_equal params, routes.recognize_path('/de/some.html')
29
+ end
30
+
31
+ test 'recognizes the path /some/page/2.html (extension, pagination)' do
32
+ params = self.params.merge(:page => 2)
33
+ assert_equal params, routes.recognize_path('/some/page/2.html')
34
+ end
35
+
36
+ test 'recognizes the path /:uuid/some.html (extension, uuid)' do
37
+ params = self.params.merge(:uuid => uuid)
38
+ assert_equal params, routes.recognize_path("/#{uuid}/some.html")
39
+ end
40
+
41
+ # locale with any
42
+
43
+ test 'recognizes the path /de/some/page/2 (locale, pagination)' do
44
+ params = self.params.merge(:locale => 'de', :page => 2)
45
+ assert_equal params, routes.recognize_path('/de/some/page/2')
46
+ end
47
+
48
+ test 'recognizes the path /de/:uuid/some (locale, uuid)' do
49
+ params = self.params.merge(:locale => 'de', :uuid => uuid)
50
+ assert_equal params, routes.recognize_path("/de/#{uuid}/some")
51
+ end
52
+
53
+ # pagination with any
54
+
55
+ test 'recognizes the path /:uuid/some/page/2 (pagination, uuid)' do
56
+ params = self.params.merge(:page => 2, :uuid => uuid)
57
+ assert_equal params, routes.recognize_path("/#{uuid}/some/page/2")
58
+ end
59
+
60
+ # extension, locale with any
61
+
62
+ test 'recognizes the path /de/some/page/2.html (extension, locale, pagination)' do
63
+ params = self.params.merge(:locale => 'de', :page => 2)
64
+ assert_equal params, routes.recognize_path("/de/some/page/2.html")
65
+ end
66
+
67
+ test 'recognizes the path /de/:uuid/some.html (extension, locale, uuid)' do
68
+ params = self.params.merge(:locale => 'de', :uuid => uuid)
69
+ assert_equal params, routes.recognize_path("/de/#{uuid}/some.html")
70
+ end
71
+
72
+ # extension, pagination with any
73
+
74
+ test 'recognizes the path /some/page/2.html (extension, pagination, uuid)' do
75
+ params = self.params.merge(:page => 2, :uuid => uuid)
76
+ assert_equal params, routes.recognize_path("/#{uuid}/some/page/2.html")
77
+ end
78
+
79
+ # locale, pagination with any
80
+
81
+ test 'recognizes the path /de/some/page/2 (locale, pagination, uuid)' do
82
+ params = self.params.merge(:locale => 'de', :page => 2, :uuid => uuid)
83
+ assert_equal params, routes.recognize_path("/de/#{uuid}/some/page/2")
84
+ end
85
+
86
+ # all
87
+
88
+ test 'recognizes the path /de/:uuid/some/page/2.html (extension, locale, pagination, uuid)' do
89
+ params = self.params.merge(:locale => 'de', :page => 2, :uuid => uuid)
90
+ assert_equal params, routes.recognize_path("/de/#{uuid}/some/page/2.html")
91
+ end
92
+ end
@@ -0,0 +1,25 @@
1
+ require 'test_helper'
2
+ require 'filters/all_filters/generation'
3
+ require 'filters/all_filters/recognition'
4
+
5
+ class AllFiltersTest < Test::Unit::TestCase
6
+ attr_reader :routes, :params, :uuid
7
+
8
+ def setup
9
+ I18n.locale = nil
10
+ I18n.default_locale = :en
11
+ I18n.available_locales = %w(de en)
12
+
13
+ RoutingFilter::Locale.include_default_locale = false
14
+
15
+ @params = { :controller => 'some', :action => 'index' }
16
+ @uuid = 'd00fbbd1-82b6-4c1a-a57d-098d529d6854'
17
+
18
+ @routes = draw_routes do
19
+ filter :uuid, :pagination ,:locale, :extension
20
+ match 'some', :to => 'some#index'
21
+ end
22
+ end
23
+
24
+ include Recognition, Generation
25
+ end
@@ -0,0 +1,54 @@
1
+ require 'test_helper'
2
+
3
+ class ForceExtensionTest < Test::Unit::TestCase
4
+ attr_reader :routes, :params
5
+
6
+ def setup
7
+ @routes = draw_routes do
8
+ filter :extension, :exclude => %r(^/(admin|$))
9
+ match '/', :to => 'some#index'
10
+ match 'some/:id(.:format)', :to => 'some#show'
11
+ match '/admin/some/new', :to => 'some#new'
12
+ end
13
+ @params = { :controller => 'some', :action => 'show', :id => '1' }
14
+ end
15
+
16
+ test 'recognizes the path some/1.html and strips the extension' do
17
+ assert_nil routes.recognize_path('/some/1.html')[:format]
18
+ end
19
+
20
+ test 'recognizes the path some/1.xml but does not strip the extension' do
21
+ assert 'xml', routes.recognize_path('/some/1.xml')[:format]
22
+ end
23
+
24
+ test 'appends the extension .html to the generated path' do
25
+ assert_equal '/some/1.html', routes.generate(params)
26
+ end
27
+
28
+ test 'does not touch existing extensions in generated paths' do
29
+ assert_equal '/some/1.xml', routes.generate(params.merge(:format => 'xml'))
30
+ end
31
+
32
+ test 'does not touch url query params in generated paths' do
33
+ assert_equal '/some/1.html?foo=bar', routes.generate(params.merge(:foo => 'bar'))
34
+ end
35
+
36
+ test 'excludes / by default' do
37
+ assert_equal '/', routes.generate(:controller => 'some', :action => 'index')
38
+ end
39
+
40
+ test 'excludes / by default (with url query params)' do
41
+ assert_equal '/?foo=bar', routes.generate(:controller => 'some', :action => 'index', :foo => 'bar')
42
+ end
43
+
44
+ test 'excludes with custom regexp' do
45
+ assert_equal '/admin/some/new', routes.generate(:controller => 'some', :action => 'new')
46
+ end
47
+
48
+ # TODO - why would anyone want to have this?
49
+ #
50
+ # test 'does not exclude / when :exclude => false was passed' do
51
+ # routes.filters.first.instance_variable_set(:@exclude, false)
52
+ # assert_equal '/.html', routes.generate(:controller => 'some', :action => 'index')
53
+ # end
54
+ end
@@ -0,0 +1,69 @@
1
+ require 'test_helper'
2
+
3
+ class LocaleTest < Test::Unit::TestCase
4
+ attr_reader :routes, :show_params, :index_params
5
+
6
+ def setup
7
+ I18n.locale = nil
8
+ I18n.default_locale = :en
9
+ I18n.available_locales = %w(de en)
10
+
11
+ RoutingFilter::Locale.include_default_locale = true
12
+
13
+ @index_params = { :controller => 'some', :action => 'index' }
14
+ @show_params = { :controller => 'some', :action => 'show', :id => '1' }
15
+
16
+ @routes = draw_routes do
17
+ filter :locale
18
+ match 'products/:id', :to => 'some#show'
19
+ match '/', :to => 'some#index'
20
+ end
21
+ end
22
+
23
+ test 'recognizes the path /en' do
24
+ assert_equal index_params.merge(:locale => 'en'), routes.recognize_path('/en')
25
+ end
26
+
27
+ test 'recognizes the path /en/' do
28
+ assert_equal index_params.merge(:locale => 'en'), routes.recognize_path('/en/')
29
+ end
30
+
31
+ test 'recognizes the path /en/products/1' do
32
+ assert_equal show_params.merge(:locale => 'en'), routes.recognize_path('/en/products/1')
33
+ end
34
+
35
+ test 'recognizes the path /de/products/1' do
36
+ assert_equal show_params.merge(:locale => 'de'), routes.recognize_path('/de/products/1')
37
+ end
38
+
39
+
40
+ test 'prepends the segments /:locale to the generated path / if the current locale is not the default locale' do
41
+ I18n.locale = 'de'
42
+ assert_equal '/de', routes.generate(index_params)
43
+ end
44
+
45
+ test 'prepends the segments /:locale to the generated path /products/1 if the current locale is not the default locale' do
46
+ I18n.locale = 'de'
47
+ assert_equal '/de/products/1', routes.generate(show_params)
48
+ end
49
+
50
+ test 'prepends the segments /:locale to the generated path if it was passed as a param' do
51
+ assert_equal '/de/products/1', routes.generate(show_params.merge(:locale => 'de'))
52
+ end
53
+
54
+ test 'prepends the segments /:locale if the given locale is the default_locale and include_default_locale is true' do
55
+ assert RoutingFilter::Locale.include_default_locale?
56
+ assert_equal '/en/products/1', routes.generate(show_params.merge(:locale => 'en'))
57
+ end
58
+
59
+ test 'does not prepend the segments /:locale if the current locale is the default_locale and include_default_locale is false' do
60
+ I18n.locale = 'en'
61
+ RoutingFilter::Locale.include_default_locale = false
62
+ assert_equal '/products/1', routes.generate(show_params)
63
+ end
64
+
65
+ test 'does not prepend the segments /:locale if the given locale is the default_locale and include_default_locale is false' do
66
+ RoutingFilter::Locale.include_default_locale = false
67
+ assert_equal '/products/1', routes.generate(show_params.merge(:locale => I18n.default_locale))
68
+ end
69
+ end
@@ -0,0 +1,29 @@
1
+ require 'test_helper'
2
+
3
+ class PaginationTest < Test::Unit::TestCase
4
+ attr_reader :routes, :params
5
+
6
+ def setup
7
+ @routes = draw_routes do
8
+ filter :pagination
9
+ match 'some', :to => 'some#index'
10
+ end
11
+ @params = { :controller => 'some', :action => 'index', :page => 2 }
12
+ end
13
+
14
+ test 'recognizes the path some/page/2' do
15
+ assert_equal params, routes.recognize_path('/some/page/2')
16
+ end
17
+
18
+ test 'appends the segments /page/:page to the generated path if the passed :page param does not equal 1' do
19
+ assert_equal '/some/page/2', routes.generate(params)
20
+ end
21
+
22
+ test 'does not append anything to the generated path if the passed :page param equals 1' do
23
+ assert_equal '/some', routes.generate(params.merge(:page => 1))
24
+ end
25
+
26
+ test 'appends the segments /page/:page to the generated path but respects url query params' do
27
+ assert_equal '/some/page/2?foo=bar', routes.generate(params.merge(:foo => 'bar'))
28
+ end
29
+ end
@@ -0,0 +1,40 @@
1
+ require 'test_helper'
2
+
3
+ class UuidTest < Test::Unit::TestCase
4
+ attr_reader :routes, :uuid, :params
5
+
6
+ def setup
7
+ @routes = draw_routes do
8
+ filter :uuid
9
+ match 'some/:id', :to => 'some#show'
10
+ end
11
+ @uuid = 'd00fbbd1-82b6-4c1a-a57d-098d529d6854'
12
+ @params = { :controller => 'some', :action => 'show', :id => '1', :uuid => uuid }
13
+ end
14
+
15
+ test 'recognizes the path :uuid/product/1' do
16
+ assert_equal params, routes.recognize_path("/#{uuid}/some/1")
17
+ end
18
+
19
+ test 'prepends the :uuid segment to the generated path if passed as a param' do
20
+ assert_equal "/#{uuid}/some/1", routes.generate(params)
21
+ end
22
+
23
+ test 'matches uuid segments' do
24
+ pattern = Uuid::UUID_SEGMENT
25
+ uuids = %w(
26
+ d00fbbd1-82b6-4c1a-a57d-098d529d6854 cdb33760-94da-11df-981c-0800200c9a66
27
+ 0c65a6ec-6491-4316-a137-0021cf4e6471 cbbd44c3-c195-48e5-be04-3cc8a6578f51
28
+ )
29
+ uuids.each { |uuid| assert pattern.match("/#{uuid}/"), "does not match /#{uuid}/ but should" }
30
+ end
31
+
32
+ test 'does not match non-uuid segments' do
33
+ pattern = Uuid::UUID_SEGMENT
34
+ uuids = %w(
35
+ !aaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa aaaa-aaaaaaaa-aaaa-aaaa-aaaaaaaaaaaa
36
+ aaaaaaaa_aaaa_aaaa_aaaa_aaaaaaaaaaaa aaaaaaaa-aaaa-aaaa-aaaaaaaaaaaa
37
+ )
38
+ uuids.each { |uuid| assert !pattern.match("/#{uuid}/"), "matches /#{uuid}/ but shouldn't" }
39
+ end
40
+ end
@@ -0,0 +1,92 @@
1
+ require 'test_helper'
2
+ require "test_adapters/rails_#{ActionPack::VERSION::MAJOR}"
3
+
4
+ class RailsTest < Test::Unit::TestCase
5
+ include TestRailsAdapter
6
+
7
+ I18n.available_locales = [:en, :de]
8
+
9
+ class TestsController < ActionController::Base
10
+ include Rails.application.routes.url_helpers if defined?(Rails)
11
+
12
+ def index
13
+ url = url_for(params.merge(:only_path => true))
14
+ render :text => params.merge(:url => url).inspect
15
+ end
16
+
17
+ def show
18
+ url = foo_path(params)
19
+ render :text => params.merge(:url => url).inspect
20
+ end
21
+ end
22
+
23
+ def params
24
+ response.status.to_s.include?('200') ? eval(response.body).symbolize_keys : {}
25
+ end
26
+
27
+ test "get to /" do
28
+ get '/'
29
+ assert_nil params[:locale]
30
+ assert_nil params[:page]
31
+ assert_nil params[:uuid]
32
+ assert_equal '/en.html', params[:url]
33
+ end
34
+
35
+ test "get to /foo/1" do
36
+ get '/foo/1'
37
+ assert_nil params[:locale]
38
+ assert_nil params[:page]
39
+ assert_nil params[:uuid]
40
+ assert_equal '/en/foo/1.html', params[:url]
41
+ end
42
+
43
+ test "get to /de" do
44
+ get '/de'
45
+ assert_equal 'de', params[:locale]
46
+ assert_nil params[:page]
47
+ assert_nil params[:uuid]
48
+ assert_equal '/de.html', params[:url]
49
+ end
50
+
51
+ test "get to /de/foo/1" do
52
+ get '/de/foo/1'
53
+ assert_equal 'de', params[:locale]
54
+ assert_nil params[:page]
55
+ assert_nil params[:uuid]
56
+ assert_equal '/de/foo/1.html', params[:url]
57
+ end
58
+
59
+ test "get to /page/2" do
60
+ get '/page/2'
61
+ assert_nil params[:locale]
62
+ assert_equal 2, params[:page]
63
+ assert_nil params[:uuid]
64
+ assert_equal '/en/page/2.html', params[:url]
65
+ end
66
+
67
+ test "get to /foo/1/page/2" do
68
+ get '/foo/1/page/2'
69
+ assert_nil params[:locale]
70
+ assert_equal 2, params[:page]
71
+ assert_nil params[:uuid]
72
+ assert_equal '/en/foo/1/page/2.html', params[:url]
73
+ end
74
+
75
+ test "get to /:uuid" do
76
+ uuid = 'd00fbbd1-82b6-4c1a-a57d-098d529d6854'
77
+ get "/#{uuid}"
78
+ assert_nil params[:locale]
79
+ assert_nil params[:page]
80
+ assert_equal uuid, params[:uuid]
81
+ assert_equal "/en/#{uuid}.html", params[:url]
82
+ end
83
+
84
+ test "get to /foo/1/:uuid" do
85
+ uuid = 'd00fbbd1-82b6-4c1a-a57d-098d529d6854'
86
+ get "/#{uuid}/foo/1"
87
+ assert_nil params[:locale]
88
+ assert_nil params[:page]
89
+ assert_equal uuid, params[:uuid]
90
+ assert_equal "/en/#{uuid}/foo/1.html", params[:url]
91
+ end
92
+ end
@@ -0,0 +1,31 @@
1
+ require 'test_helper'
2
+
3
+ class RoutesTest < Test::Unit::TestCase
4
+ class RoutingFilter::Test < Filter
5
+ def around_recognize(path, env, &block)
6
+ 'recognized'
7
+ end
8
+
9
+ def around_generate(*args, &block)
10
+ 'generated'
11
+ end
12
+ end
13
+
14
+ attr_reader :routes
15
+
16
+ def setup
17
+ @routes = draw_routes { |set| set.filter :test }
18
+ end
19
+
20
+ test "routes.filter instantiates and registers a filter" do
21
+ assert routes.filters.first.is_a?(RoutingFilter::Test)
22
+ end
23
+
24
+ # test "filter.around_recognize is being called" do
25
+ # assert_equal 'recognized', routes.recognize_path('/')
26
+ # end
27
+
28
+ test "filter.around_generate is being called" do
29
+ assert_equal 'generated', routes.generate({})
30
+ end
31
+ end
@@ -0,0 +1,47 @@
1
+ require 'test_helper'
2
+
3
+ include RoutingFilter
4
+
5
+ class RoutingFilterTest < Test::Unit::TestCase
6
+ class FooFilter < Filter
7
+ attr_reader :name
8
+
9
+ def initialize(name)
10
+ @name = name
11
+ end
12
+
13
+ def foo(log, &block)
14
+ log << name
15
+ yield
16
+ end
17
+ end
18
+
19
+ attr_reader :chain
20
+
21
+ def setup
22
+ @chain = Chain.new
23
+ @chain.unshift FooFilter.new('custom filter')
24
+ @chain.unshift FooFilter.new('common filter')
25
+ end
26
+
27
+ test "filter.previous is nil for the first filter in the chain" do
28
+ assert_nil chain.first.previous
29
+ end
30
+
31
+ test "filter.previous returns the previous filter in the chain" do
32
+ assert_equal chain.first, chain.last.previous
33
+ end
34
+
35
+ test "filter.next is nil for the last filter in the chain" do
36
+ assert_nil chain.last.next
37
+ end
38
+
39
+ test "filter.next returns the next filter in the chain" do
40
+ assert_equal chain.last, chain.first.next
41
+ end
42
+
43
+ test "chain.run calls the given method on registered filters in reverse order" do
44
+ log = []
45
+ assert_equal 'common filter, custom filter, finalizer', chain.run(:foo, log, &lambda { log << 'finalizer' }).join(', ')
46
+ end
47
+ end