sinatra-mapping 1.0.6 → 1.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +2 -0
- data/LICENSE +0 -2
- data/README.rdoc +97 -29
- data/VERSION +2 -2
- data/lib/sinatra/mapping.rb +4 -4
- data/test/fixtures/modular_application.rb +71 -0
- data/test/helper.rb +6 -0
- data/test/modular_mapping_test.rb +112 -0
- metadata +5 -3
- data/test/mapping_test.rb +0 -218
data/CHANGES
CHANGED
data/LICENSE
CHANGED
data/README.rdoc
CHANGED
@@ -1,16 +1,17 @@
|
|
1
1
|
= Sinatra::Mapping
|
2
2
|
|
3
|
-
Map easily URLs
|
3
|
+
Map easily URLs in your Web applications.
|
4
4
|
|
5
5
|
* {Repository}[http://github.com/hallison/sinatra-mapping]
|
6
6
|
* {Project}[http://rubyforge.org/projects/sinatra-mapping]
|
7
7
|
* {Documentation}[:link:Sinatra/Mapping.html]
|
8
8
|
* {Issues}[http://github.com/hallison/sinatra-mapping/issues]
|
9
9
|
|
10
|
-
The
|
10
|
+
The extension Sinatra::Mapping is a minimal module that is useful for
|
11
11
|
create map names for {Ruby}[http://www.ruby-lang.org]
|
12
12
|
{Sinatra}[http://www.sinatrarb.com] web applications.
|
13
13
|
|
14
|
+
== Getting start
|
14
15
|
Install stable version gem from {RubyForge.org}[http://www.rubyforge.org/]:
|
15
16
|
|
16
17
|
gem install sinatra-mapping
|
@@ -19,7 +20,7 @@ Or, install development version gem from {GitHub.com}[http://github.com/]:
|
|
19
20
|
|
20
21
|
gem install hallison-sinatra-mapping --source http://gems.github.com
|
21
22
|
|
22
|
-
|
23
|
+
== How to use
|
23
24
|
|
24
25
|
Sinatra implements REST routes:
|
25
26
|
|
@@ -39,54 +40,122 @@ Sinatra implements REST routes:
|
|
39
40
|
.. annihilate something ..
|
40
41
|
end
|
41
42
|
|
42
|
-
|
43
|
-
|
43
|
+
For improve this routes use extension by registered method in the main
|
44
|
+
source of application. To better understand, copy and paste the following
|
45
|
+
example in {Ruby}[http://www.ruby-lang.org] source file +webfoo.rb+:
|
44
46
|
|
47
|
+
#!/usr/bin/env ruby
|
48
|
+
|
49
|
+
require 'rubygems'
|
45
50
|
require 'sinatra'
|
46
51
|
require 'sinatra/mapping'
|
52
|
+
require 'sinatra/mapping_helpers'
|
47
53
|
|
48
|
-
class
|
54
|
+
class Sinatra::Application
|
49
55
|
|
50
56
|
register Sinatra::Mapping
|
57
|
+
helpers Sinatra::MappingHelpers
|
51
58
|
|
52
|
-
map :root, "blog"
|
53
|
-
map :
|
59
|
+
map :root, "blog" # /blog/
|
60
|
+
map :entries, "posts" # /blog/posts
|
61
|
+
map :tags, "labels" # /blog/labels
|
54
62
|
|
55
|
-
mapping :
|
56
|
-
:
|
63
|
+
mapping :entry => "posts/:entry_id", # /blog/posts/id-for-post
|
64
|
+
:entry_comments => "posts/:entry_id/comments", # /blog/posts/id-for-post/comments
|
65
|
+
:tagged_entries => "labels/:tag_id/entries" # /blog/labels/id-for-tag/entries
|
57
66
|
|
67
|
+
# /blog/
|
58
68
|
get root_path do
|
59
|
-
|
60
|
-
|
69
|
+
<<-end_content
|
70
|
+
<h1>Welcome to Foo Web Application</h1>
|
71
|
+
<ul>
|
72
|
+
<li>#{link_to title_path(:entries), :entries, :title => title_path(:entries)}</li>
|
73
|
+
<li>#{link_to title_path(:tags), :tags, :title => title_path(:tags)}</li>
|
74
|
+
</ul>
|
75
|
+
end_content
|
61
76
|
end
|
62
77
|
|
63
|
-
|
64
|
-
|
65
|
-
|
78
|
+
# /blog/entries
|
79
|
+
get entries_path do
|
80
|
+
<<-end_content
|
81
|
+
<h1>Welcome to Foo Web Application</h1>
|
82
|
+
<h2>#{title_path(:entries)}</h2>
|
83
|
+
<ul>
|
84
|
+
<li>#{link_to "Testing new entry ...", :entries, "testing-new-entry"}</li>
|
85
|
+
<li>#{link_to "Testing old entry ...", :entries, "testing-old-entry"}</li>
|
86
|
+
</ul>
|
87
|
+
<p>
|
88
|
+
#{link_to "Back", :root}
|
89
|
+
</p>
|
90
|
+
end_content
|
66
91
|
end
|
67
92
|
|
68
|
-
|
69
|
-
|
70
|
-
|
93
|
+
# /blog/labels/tag-id-for-show-content
|
94
|
+
get tags_path do
|
95
|
+
<<-end_content
|
96
|
+
<h1>Welcome to Foo Web Application</h1>
|
97
|
+
<h2>#{title_path(:tags)}</h2>
|
98
|
+
<ul>
|
99
|
+
<li>#{link_to "Ruby", :tags, "ruby", "entries"}</li>
|
100
|
+
<li>#{link_to "Sinatra", :tags, "sinatra", "entries"}</li>
|
101
|
+
<li>#{link_to "Mapping", :tags, "mapping", "entries"}</li>
|
102
|
+
</ul>
|
103
|
+
<p>
|
104
|
+
#{link_to "Back", :root}
|
105
|
+
</p>
|
106
|
+
end_content
|
71
107
|
end
|
72
108
|
|
73
|
-
|
74
|
-
|
75
|
-
|
109
|
+
# /blog/entries/entry-id-for-show-content
|
110
|
+
get entry_path do |entry_id|
|
111
|
+
title = entry_id.gsub('-',' ').capitalize
|
112
|
+
<<-end_content
|
113
|
+
<h1>Welcome to Foo Web Application</h1>
|
114
|
+
<h2>#{title}</h2>
|
115
|
+
<p>
|
116
|
+
It works!
|
117
|
+
</p>
|
118
|
+
<p>
|
119
|
+
#{link_to "Back", :root} | #{link_to "Comments", :entries, entry_id, 'comments'}
|
120
|
+
</p>
|
121
|
+
end_content
|
76
122
|
end
|
77
123
|
|
78
|
-
|
79
|
-
|
124
|
+
# /blog/entries/entry-id-for-show-content/comments
|
125
|
+
get entry_comments_path do |entry_id|
|
126
|
+
title = entry_id.gsub('-',' ').capitalize
|
127
|
+
<<-end_content
|
128
|
+
<h1>Welcome to Foo Web Application</h1>
|
129
|
+
<h2>#{title}</h2>
|
130
|
+
<p>
|
131
|
+
It works and show comments for "#{title}".
|
132
|
+
</p>
|
133
|
+
<p>
|
134
|
+
#{link_to "Back", :root}
|
135
|
+
</p>
|
136
|
+
end_content
|
80
137
|
end
|
81
138
|
|
82
|
-
get
|
83
|
-
|
84
|
-
|
139
|
+
get tagged_entries_path do |tag_id|
|
140
|
+
<<-end_content
|
141
|
+
<h1>Welcome to Foo Web Application</h1>
|
142
|
+
<h2>#{tag_id.capitalize}</h2>
|
143
|
+
<p>
|
144
|
+
It works and show all entries tagged with "#{tag_id}".
|
145
|
+
</p>
|
146
|
+
<p>
|
147
|
+
#{link_to "Back", :root}
|
148
|
+
</p>
|
149
|
+
end_content
|
85
150
|
end
|
86
151
|
|
87
152
|
end
|
88
153
|
|
89
|
-
|
154
|
+
Run:
|
155
|
+
|
156
|
+
$ ruby webfoo.rb -p 3000
|
157
|
+
|
158
|
+
Open Web browser http://localhost:3000/blog/ and look ... it works!
|
90
159
|
|
91
160
|
== More informations
|
92
161
|
|
@@ -96,5 +165,4 @@ Easy!!!
|
|
96
165
|
|
97
166
|
== Copyright
|
98
167
|
|
99
|
-
|
100
|
-
|
168
|
+
:include:LICENSE
|
data/VERSION
CHANGED
data/lib/sinatra/mapping.rb
CHANGED
@@ -27,12 +27,12 @@ module Sinatra
|
|
27
27
|
if name.to_sym == :root
|
28
28
|
@locations[:root] = cleanup_paths("/#{path}/")
|
29
29
|
metadef "#{name}_path" do |*paths|
|
30
|
-
@locations[:root]
|
30
|
+
cleanup_paths("/#{@locations[:root]}/?")
|
31
31
|
end
|
32
32
|
else
|
33
|
-
@locations[name.to_sym] = path || name.to_s
|
33
|
+
@locations[name.to_sym] = cleanup_paths(path || name.to_s)
|
34
34
|
metadef "#{name}_path" do |*paths|
|
35
|
-
map_path_to(@locations[name.to_sym], *paths)
|
35
|
+
map_path_to(@locations[name.to_sym], *paths << "/?")
|
36
36
|
end
|
37
37
|
end
|
38
38
|
end
|
@@ -79,7 +79,7 @@ module Sinatra
|
|
79
79
|
# Check arguments. If argument is a symbol and exist map path before
|
80
80
|
# setted, then return path mapped by symbol name.
|
81
81
|
def map_path_to(*args)
|
82
|
-
script_name = args.shift if args.first.to_s =~
|
82
|
+
script_name = args.shift if args.first.to_s =~ %r{^/\w.*}
|
83
83
|
path_mapped(script_name, *locations_get_from(*args))
|
84
84
|
end
|
85
85
|
|
@@ -0,0 +1,71 @@
|
|
1
|
+
$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../../lib/")
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'sinatra/base'
|
5
|
+
require 'sinatra/mapping'
|
6
|
+
require 'sinatra/mapping_helpers'
|
7
|
+
|
8
|
+
class Sinatra::Base
|
9
|
+
def env
|
10
|
+
@env.update('SCRIPT_NAME' => '/test')
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class ModularApplication < Sinatra::Base
|
15
|
+
|
16
|
+
register Sinatra::Mapping
|
17
|
+
helpers Sinatra::MappingHelpers
|
18
|
+
|
19
|
+
map :root, "blog" # root_path => /blog/
|
20
|
+
map :about # about_path => /blog/about
|
21
|
+
|
22
|
+
mapping :posts => "articles", # posts_path => /blog/articles
|
23
|
+
:archive => "archive/articles", # archive_path => /blog/archive/articles
|
24
|
+
:search => "find-articles", # search_path => /blog/find-articles
|
25
|
+
:drafts => "unpublished" # drafts_path => /blog/unpublished
|
26
|
+
|
27
|
+
before do
|
28
|
+
@date = Date.today
|
29
|
+
end
|
30
|
+
|
31
|
+
get root_path do
|
32
|
+
"#{title_path :root, :path}:#{path_to :root}"
|
33
|
+
end
|
34
|
+
|
35
|
+
get posts_path do
|
36
|
+
"#{title_path :posts, :published}:#{path_to :posts}"
|
37
|
+
end
|
38
|
+
|
39
|
+
get posts_path "/:year/:month/:day/:permalink" do |year, month, day, permalink|
|
40
|
+
"#{title_path :posts}:" + path_to(:posts, "#{@date.to_s.gsub('-','/')}/#{permalink}")
|
41
|
+
end
|
42
|
+
|
43
|
+
get archive_path do
|
44
|
+
"#{title_path :archive}:#{path_to :archive}"
|
45
|
+
end
|
46
|
+
|
47
|
+
get archive_path "/:year/:month/:day/:permalink" do |year, month, day, permalink|
|
48
|
+
"#{title_path :archive}:" + path_to(:archive, "#{@date.to_s.gsub('-','/')}/#{permalink}")
|
49
|
+
end
|
50
|
+
|
51
|
+
get about_path do
|
52
|
+
"#{title_path :about}:#{path_to :about}"
|
53
|
+
end
|
54
|
+
|
55
|
+
get search_path do
|
56
|
+
<<-end_content.gsub(/^ /,'')
|
57
|
+
#{title_path :search}:#{path_to :search, :keywords => 'ruby'}
|
58
|
+
#{link_to "Search", :search, :title => 'Search'}
|
59
|
+
#{link_to "Search", :search, :title => 'Search', :keywords => 'ruby'}
|
60
|
+
end_content
|
61
|
+
end
|
62
|
+
|
63
|
+
get drafts_path do
|
64
|
+
<<-end_content.gsub(/^ /,'')
|
65
|
+
#{title_path :drafts}:#{path_to [:drafts, :posts]}
|
66
|
+
#{link_to "Unpublished", :drafts, :posts, :title => 'Unpublished'}
|
67
|
+
end_content
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
data/test/helper.rb
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/..")
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'test/unit'
|
5
|
+
require 'rack/test'
|
6
|
+
require 'ruby-debug'
|
7
|
+
|
8
|
+
require 'test/fixtures/modular_application'
|
9
|
+
|
10
|
+
class ModularTestMapping < Test::Unit::TestCase
|
11
|
+
|
12
|
+
include Rack::Test::Methods
|
13
|
+
|
14
|
+
def setup
|
15
|
+
@date = Date.today
|
16
|
+
@link_paths = {
|
17
|
+
:root_path => "/test/blog",
|
18
|
+
:posts_path => "/test/blog/articles",
|
19
|
+
:archive_path => "/test/blog/archive/articles",
|
20
|
+
:about_path => "/test/blog/about",
|
21
|
+
:search_path => "/test/blog/find-articles",
|
22
|
+
:drafts_path => "/test/blog/unpublished"
|
23
|
+
}
|
24
|
+
@root_paths = @link_paths.inject({}) do |hash, (name, path)|
|
25
|
+
hash[name] = "#{path}/"
|
26
|
+
hash
|
27
|
+
end
|
28
|
+
@paths = @root_paths.inject({}) do |hash, (name, path)|
|
29
|
+
hash[name] = "#{path}?"
|
30
|
+
hash
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def app
|
35
|
+
@app = ::ModularApplication
|
36
|
+
@app.set :environment, :test
|
37
|
+
@app
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_check_map_locations
|
41
|
+
@paths.each do |name, location|
|
42
|
+
path = location.gsub(/\/test/,'')
|
43
|
+
assert_equal path, app.send(name)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_should_return_ok_in_root_path
|
48
|
+
get app.root_path do |response|
|
49
|
+
assert response.ok?
|
50
|
+
assert_equal "http://example.org#{@paths[:root_path]}", last_request.url
|
51
|
+
assert_equal "Blog path:#{@link_paths[:root_path]}", response.body
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_should_return_ok_in_posts_path
|
56
|
+
get app.posts_path do |response|
|
57
|
+
assert response.ok?
|
58
|
+
assert_equal "http://example.org#{@paths[:posts_path]}", last_request.url
|
59
|
+
assert_equal "Articles published:#{@link_paths[:posts_path]}", response.body
|
60
|
+
end
|
61
|
+
|
62
|
+
path = app.posts_path "/#{@date.to_s.gsub('-','/')}/post-permalink"
|
63
|
+
get path do |response|
|
64
|
+
assert response.ok?
|
65
|
+
assert_equal "http://example.org/test#{path}", last_request.url
|
66
|
+
assert_equal "Articles:/test#{path}", response.body
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_should_return_ok_in_archive_path
|
71
|
+
get app.archive_path do |response|
|
72
|
+
assert response.ok?
|
73
|
+
assert_equal "http://example.org#{@root_paths[:archive_path]}", last_request.url
|
74
|
+
assert_equal "Archive articles:#{@link_paths[:archive_path]}", response.body
|
75
|
+
end
|
76
|
+
|
77
|
+
path = app.archive_path "/#{@date.to_s.gsub('-','/')}/post-permalink"
|
78
|
+
get path do |response|
|
79
|
+
assert response.ok?
|
80
|
+
assert_equal "http://example.org/test#{path}", last_request.url
|
81
|
+
assert_equal "Archive articles:/test#{path}", response.body
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_should_return_ok_in_about_path
|
86
|
+
get app.about_path do |response|
|
87
|
+
assert response.ok?
|
88
|
+
assert_equal "http://example.org#{@root_paths[:about_path]}", last_request.url
|
89
|
+
assert_equal "About:#{@link_paths[:about_path]}", response.body
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_should_return_ok_in_search_path
|
94
|
+
get "#{app.search_path}?keywords=ruby" do |response|
|
95
|
+
assert response.ok?
|
96
|
+
assert_equal "http://example.org#{@paths[:search_path]}?keywords=ruby", last_request.url
|
97
|
+
assert_equal "Find articles:#{@paths[:search_path]}?keywords=ruby", response.body.split("\n")[0]
|
98
|
+
assert_equal "<a href=\"#{@paths[:search_path]}\" title=\"Search\">Search</a>", response.body.split("\n")[1]
|
99
|
+
assert_equal "<a href=\"#{@paths[:search_path]}?keywords=ruby\" title=\"Search\">Search</a>", response.body.split("\n")[2]
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_should_check_path_method_with_array_params
|
104
|
+
get app.drafts_path do |response|
|
105
|
+
assert response.ok?
|
106
|
+
assert_equal "Unpublished:#{@link_paths[:drafts_path]}/articles".squeeze('/'), response.body.split("\n")[0]
|
107
|
+
body_link = "<a href=\"#{@link_paths[:drafts_path]}/articles\" title=\"Unpublished\">Unpublished</a>"
|
108
|
+
assert_equal body_link, response.body.split("\n")[1]
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-mapping
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hallison Batista
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-08-
|
12
|
+
date: 2009-08-25 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -51,7 +51,9 @@ files:
|
|
51
51
|
- lib/sinatra/mapping.rb
|
52
52
|
- lib/sinatra/mapping_helpers.rb
|
53
53
|
- sinatra-mapping.gemspec
|
54
|
-
- test/
|
54
|
+
- test/fixtures/modular_application.rb
|
55
|
+
- test/helper.rb
|
56
|
+
- test/modular_mapping_test.rb
|
55
57
|
has_rdoc: true
|
56
58
|
homepage: http://sinatra-mapping.rubyforge.org/
|
57
59
|
licenses: []
|
data/test/mapping_test.rb
DELETED
@@ -1,218 +0,0 @@
|
|
1
|
-
$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/..")
|
2
|
-
|
3
|
-
require 'rubygems'
|
4
|
-
require 'lib/sinatra/mapping'
|
5
|
-
require 'lib/sinatra/mapping_helpers'
|
6
|
-
require 'test/unit'
|
7
|
-
require 'rack/test'
|
8
|
-
require 'ruby-debug'
|
9
|
-
|
10
|
-
class Sinatra::Base
|
11
|
-
def env
|
12
|
-
@env ||= { 'SCRIPT_NAME' => '/blog' }
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
class AppForTest < Sinatra::Base
|
17
|
-
|
18
|
-
register Sinatra::Mapping
|
19
|
-
helpers Sinatra::MappingHelpers
|
20
|
-
|
21
|
-
map :root, "blog" # root_path => /blog/
|
22
|
-
map :about # about_path => /blog/about
|
23
|
-
|
24
|
-
mapping :posts => "articles", # posts_path => /blog/articles
|
25
|
-
:archive => "archive/articles", # archive_path => /blog/archive/articles
|
26
|
-
:search => "find-articles", # search_path => /blog/find-articles
|
27
|
-
:drafts => "unpublished" # drafts_path => /blog/unpublished
|
28
|
-
|
29
|
-
before do
|
30
|
-
@date = Date.today
|
31
|
-
end
|
32
|
-
|
33
|
-
get root_path do
|
34
|
-
"#{title_path :root, :path}:#{path_to :root}"
|
35
|
-
end
|
36
|
-
|
37
|
-
get posts_path do
|
38
|
-
"#{title_path :posts, :published}:#{path_to :posts}"
|
39
|
-
end
|
40
|
-
|
41
|
-
get posts_path "/" do
|
42
|
-
redirect path_to(:posts), 301
|
43
|
-
end
|
44
|
-
|
45
|
-
get posts_path "/:year/:month/:day/:permalink" do |year, month, day, permalink|
|
46
|
-
"#{title_path :posts}:" + path_to(:posts, "#{@date.to_s.gsub('-','/')}/#{permalink}")
|
47
|
-
end
|
48
|
-
|
49
|
-
get posts_path "/:year/:month/:day/:permalink/" do |year, month, day, permalink|
|
50
|
-
redirect path_to(:posts, year, month, day, permalink), 301
|
51
|
-
end
|
52
|
-
|
53
|
-
get archive_path do
|
54
|
-
"#{title_path :archive}:#{path_to :archive}"
|
55
|
-
end
|
56
|
-
|
57
|
-
get archive_path "/" do
|
58
|
-
redirect path_to(:archive), 301
|
59
|
-
end
|
60
|
-
|
61
|
-
get archive_path "/:year/:month/:day/:permalink" do |year, month, day, permalink|
|
62
|
-
"#{title_path :archive}:" + path_to(:archive, "#{@date.to_s.gsub('-','/')}/#{permalink}")
|
63
|
-
end
|
64
|
-
|
65
|
-
get archive_path "/:year/:month/:day/:permalink/" do |year, month, day, permalink|
|
66
|
-
redirect path_to(:archive, year, month, day, permalink), 301
|
67
|
-
end
|
68
|
-
|
69
|
-
get about_path do
|
70
|
-
"#{title_path :about}:#{path_to :about}"
|
71
|
-
end
|
72
|
-
|
73
|
-
get about_path "/" do
|
74
|
-
redirect path_to(:about), 301
|
75
|
-
end
|
76
|
-
|
77
|
-
get search_path do
|
78
|
-
<<-end_content.gsub(/^ /,'')
|
79
|
-
#{title_path :search}:#{path_to :search, :keywords => 'ruby'}
|
80
|
-
#{link_to "Search", :search, :title => 'Search'}
|
81
|
-
#{link_to "Search", :search, :title => 'Search', :keywords => 'ruby'}
|
82
|
-
end_content
|
83
|
-
end
|
84
|
-
|
85
|
-
get drafts_path do
|
86
|
-
<<-end_content.gsub(/^ /,'')
|
87
|
-
#{title_path :drafts}:#{path_to [:drafts, :posts]}
|
88
|
-
#{link_to "Unpublished", :drafts, :posts, :title => 'Unpublished'}
|
89
|
-
end_content
|
90
|
-
end
|
91
|
-
|
92
|
-
end
|
93
|
-
|
94
|
-
class TestMapping < Test::Unit::TestCase
|
95
|
-
|
96
|
-
include Rack::Test::Methods
|
97
|
-
|
98
|
-
def setup
|
99
|
-
@date = Date.today
|
100
|
-
@locations = {
|
101
|
-
:root_path => "/",
|
102
|
-
:posts_path => "/articles",
|
103
|
-
:archive_path => "/archive/articles",
|
104
|
-
:about_path => "/about",
|
105
|
-
:search_path => "/find-articles",
|
106
|
-
:drafts_path => "/unpublished"
|
107
|
-
}
|
108
|
-
end
|
109
|
-
|
110
|
-
def app
|
111
|
-
@app = ::AppForTest
|
112
|
-
@app.set :environment, :test
|
113
|
-
@app
|
114
|
-
end
|
115
|
-
|
116
|
-
def test_check_map_locations
|
117
|
-
@locations.each do |name, location|
|
118
|
-
assert_equal "#{location}", app.send(name)
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
|
-
def test_should_return_ok_in_root_path
|
123
|
-
get app.root_path do |response|
|
124
|
-
assert response.ok?
|
125
|
-
assert_equal "http://example.org#{@locations[:root_path]}", last_request.url
|
126
|
-
assert_equal "Path:#{@locations[:root_path]}", response.body
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
def test_should_return_ok_in_posts_path
|
131
|
-
get app.posts_path do |response|
|
132
|
-
assert response.ok?
|
133
|
-
assert_equal "http://example.org#{@locations[:posts_path]}", last_request.url
|
134
|
-
assert_equal "Articles published:#{@locations[:posts_path]}", response.body
|
135
|
-
end
|
136
|
-
|
137
|
-
get app.posts_path "/" do
|
138
|
-
follow_redirect!
|
139
|
-
assert last_response.ok?
|
140
|
-
assert_equal "http://example.org#{@locations[:posts_path]}", last_request.url
|
141
|
-
end
|
142
|
-
|
143
|
-
path = app.posts_path "/#{@date.to_s.gsub('-','/')}/post-permalink"
|
144
|
-
get path do |response|
|
145
|
-
assert response.ok?
|
146
|
-
assert_equal "http://example.org#{path}", last_request.url
|
147
|
-
assert_equal "Articles:#{path}", response.body
|
148
|
-
end
|
149
|
-
|
150
|
-
get "#{path}/" do
|
151
|
-
follow_redirect!
|
152
|
-
assert last_response.ok?
|
153
|
-
assert_equal "http://example.org#{path}", last_request.url
|
154
|
-
assert_equal "Articles:#{path}", last_response.body
|
155
|
-
end
|
156
|
-
end
|
157
|
-
|
158
|
-
def test_should_return_ok_in_archive_path
|
159
|
-
get app.archive_path do |response|
|
160
|
-
assert response.ok?
|
161
|
-
assert_equal "http://example.org#{@locations[:archive_path]}", last_request.url
|
162
|
-
assert_equal "Archive articles:#{@locations[:archive_path]}", response.body
|
163
|
-
end
|
164
|
-
|
165
|
-
get app.archive_path "/" do
|
166
|
-
follow_redirect!
|
167
|
-
assert last_response.ok?
|
168
|
-
assert_equal "http://example.org#{@locations[:archive_path]}", last_request.url
|
169
|
-
end
|
170
|
-
|
171
|
-
path = app.archive_path "/#{@date.to_s.gsub('-','/')}/post-permalink"
|
172
|
-
get path do |response|
|
173
|
-
assert response.ok?
|
174
|
-
assert_equal "http://example.org#{path}", last_request.url
|
175
|
-
assert_equal "Archive articles:#{path}", response.body
|
176
|
-
end
|
177
|
-
|
178
|
-
get "#{path}/" do
|
179
|
-
follow_redirect!
|
180
|
-
assert last_response.ok?
|
181
|
-
assert_equal "http://example.org#{path}", last_request.url
|
182
|
-
assert_equal "Archive articles:#{path}", last_response.body
|
183
|
-
end
|
184
|
-
end
|
185
|
-
|
186
|
-
def test_should_return_ok_in_about_path
|
187
|
-
get app.about_path do |response|
|
188
|
-
assert response.ok?
|
189
|
-
assert_equal "http://example.org#{@locations[:about_path]}", last_request.url
|
190
|
-
assert_equal "About:#{@locations[:about_path]}", response.body
|
191
|
-
end
|
192
|
-
|
193
|
-
get app.about_path "/" do
|
194
|
-
follow_redirect!
|
195
|
-
assert last_response.ok?
|
196
|
-
assert_equal "http://example.org#{@locations[:about_path]}", last_request.url
|
197
|
-
end
|
198
|
-
end
|
199
|
-
|
200
|
-
def test_should_return_ok_in_search_path
|
201
|
-
get "#{app.search_path}?keywords=ruby" do |response|
|
202
|
-
assert response.ok?
|
203
|
-
assert_equal "http://example.org#{@locations[:search_path]}?keywords=ruby", last_request.url
|
204
|
-
assert_equal "Find articles:#{@locations[:search_path]}?keywords=ruby", response.body.split("\n")[0]
|
205
|
-
assert_equal "<a href=\"#{@locations[:search_path]}\" title=\"Search\">Search</a>", response.body.split("\n")[1]
|
206
|
-
assert_equal "<a href=\"#{@locations[:search_path]}?keywords=ruby\" title=\"Search\">Search</a>", response.body.split("\n")[2]
|
207
|
-
end
|
208
|
-
end
|
209
|
-
|
210
|
-
def test_should_check_path_method_with_array_params
|
211
|
-
get "#{app.drafts_path}" do |response|
|
212
|
-
assert response.ok?
|
213
|
-
assert_equal "Unpublished:#{@locations[:drafts_path]}#{@locations[:posts_path]}", response.body.split("\n")[0]
|
214
|
-
assert_equal "<a href=\"#{@locations[:drafts_path]}#{@locations[:posts_path]}\" title=\"Unpublished\">Unpublished</a>", response.body.split("\n")[1]
|
215
|
-
end
|
216
|
-
end
|
217
|
-
end
|
218
|
-
|