pendragon 0.6.2 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -2
- data/Gemfile +7 -0
- data/Gemfile.lock +27 -83
- data/README.md +69 -192
- data/Rakefile +3 -18
- data/benchmark.rb +31 -0
- data/lib/pendragon.rb +60 -44
- data/lib/pendragon/constants.rb +27 -0
- data/lib/pendragon/errors.rb +72 -0
- data/lib/pendragon/linear.rb +9 -0
- data/lib/pendragon/realism.rb +49 -0
- data/lib/pendragon/router.rb +295 -142
- data/lib/pendragon/version.rb +1 -1
- data/pendragon.gemspec +1 -6
- data/test/helper.rb +9 -84
- data/test/router/test_linear.rb +6 -0
- data/test/router/test_realism.rb +6 -0
- data/test/supports/shared_examples_for_routing.rb +277 -0
- data/test/test_router.rb +40 -0
- metadata +22 -101
- data/lib/pendragon/configuration.rb +0 -42
- data/lib/pendragon/engine/compiler.rb +0 -74
- data/lib/pendragon/engine/recognizer.rb +0 -72
- data/lib/pendragon/error.rb +0 -71
- data/lib/pendragon/matcher.rb +0 -93
- data/lib/pendragon/padrino.rb +0 -15
- data/lib/pendragon/padrino/ext/class_methods.rb +0 -318
- data/lib/pendragon/padrino/ext/instance_methods.rb +0 -63
- data/lib/pendragon/padrino/route.rb +0 -50
- data/lib/pendragon/padrino/router.rb +0 -34
- data/lib/pendragon/route.rb +0 -124
- data/test/compile_helper.rb +0 -3
- data/test/padrino_test.rb +0 -2113
- data/test/pendragon_configuration.rb +0 -32
- data/test/pendragon_test.rb +0 -229
@@ -1,32 +0,0 @@
|
|
1
|
-
require File.expand_path('../../lib/pendragon', __FILE__)
|
2
|
-
$:.unshift(File.dirname(__FILE__))
|
3
|
-
require 'helper'
|
4
|
-
|
5
|
-
describe Pendragon::Configuration do
|
6
|
-
setup do
|
7
|
-
@pendragon = pendragon{|config| }
|
8
|
-
end
|
9
|
-
|
10
|
-
describe "auto_rack_format" do
|
11
|
-
should "set `true` as default value" do
|
12
|
-
@pendragon.get("/"){ "hey" }
|
13
|
-
get "/"
|
14
|
-
assert_equal "hey", body
|
15
|
-
assert_equal true, @pendragon.configuration.auto_rack_format?
|
16
|
-
end
|
17
|
-
|
18
|
-
should "not serialize for rack format if `auto_rack_format` is false" do
|
19
|
-
@pendragon = pendragon do |config|
|
20
|
-
config.auto_rack_format = false
|
21
|
-
end
|
22
|
-
|
23
|
-
@pendragon.get("/"){ "hey" }
|
24
|
-
assert_raises(Rack::Lint::LintError){ get "/" }
|
25
|
-
|
26
|
-
@pendragon.post("/"){ [200, {'Content-Type' => 'text/html;charset=utf-8'}, ["hey"]] }
|
27
|
-
post "/"
|
28
|
-
assert_equal "hey", body
|
29
|
-
assert_equal false, @pendragon.configuration.auto_rack_format?
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
data/test/pendragon_test.rb
DELETED
@@ -1,229 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
require File.expand_path('../../lib/pendragon', __FILE__)
|
3
|
-
$:.unshift(File.dirname(__FILE__))
|
4
|
-
require 'helper'
|
5
|
-
|
6
|
-
describe Pendragon do
|
7
|
-
setup{ @pendragon = pendragon{|config| config.enable_compiler = defined?(ENABLE_COMPILER) } }
|
8
|
-
|
9
|
-
describe "normal routing" do
|
10
|
-
before(:each){ @pendragon.reset! }
|
11
|
-
|
12
|
-
should "basic route" do
|
13
|
-
@pendragon.add(:get, "/"){ "index" }
|
14
|
-
@pendragon.add(:get, "/users"){ "users" }
|
15
|
-
get("/")
|
16
|
-
assert_equal "index", body
|
17
|
-
get("/users")
|
18
|
-
assert_equal "users", body
|
19
|
-
end
|
20
|
-
|
21
|
-
should "ignore trailing delimiters for basic route" do
|
22
|
-
@pendragon.add(:get, "/"){ "index" }
|
23
|
-
@pendragon.add(:get, "/users"){ "users" }
|
24
|
-
|
25
|
-
get("")
|
26
|
-
assert_equal "index", body
|
27
|
-
get("/users/")
|
28
|
-
assert_equal "users", body
|
29
|
-
end
|
30
|
-
|
31
|
-
should "use pattern" do
|
32
|
-
@pendragon.add(:get, "/:id"){|params| "show #{params[:id]}" }
|
33
|
-
@pendragon.add(:get, "/:id.:ext"){|params| "show #{params[:id]}.#{params[:ext]}" }
|
34
|
-
|
35
|
-
get("/1")
|
36
|
-
assert_equal "show 1", body
|
37
|
-
get(URI.escape("/あいうえお"))
|
38
|
-
assert_equal "show あいうえお", body
|
39
|
-
get("/foo")
|
40
|
-
assert_equal "show foo", body
|
41
|
-
get("/1.json")
|
42
|
-
assert_equal "show 1.json", body
|
43
|
-
get("/foo.xml")
|
44
|
-
assert_equal "show foo.xml", body
|
45
|
-
end
|
46
|
-
|
47
|
-
should "use capture" do
|
48
|
-
id_route = @pendragon.add(:get, "/:id"){|params| "show #{params[:id]}" }
|
49
|
-
id_route.capture[:id] = /\d+/
|
50
|
-
|
51
|
-
id_with_ext_route = @pendragon.add(:get, "/:id.:ext"){|params| "show #{params[:id]}.#{params[:ext]}" }
|
52
|
-
id_with_ext_route.capture[:id] = /(foo|bar)/
|
53
|
-
id_with_ext_route.capture[:ext] = %w[html json]
|
54
|
-
|
55
|
-
get("/1")
|
56
|
-
assert_equal "show 1", body
|
57
|
-
get("/foo")
|
58
|
-
assert_equal "Not Found", body
|
59
|
-
get("/foo.json")
|
60
|
-
assert_equal "show foo.json", body
|
61
|
-
get("/baz.json")
|
62
|
-
assert_equal "Not Found", body
|
63
|
-
get("/foo.html")
|
64
|
-
assert_equal "show foo.html", body
|
65
|
-
get("/foo.xml")
|
66
|
-
assert_equal "Not Found", body
|
67
|
-
end
|
68
|
-
|
69
|
-
should "support verb methods" do
|
70
|
-
@pendragon.get("/"){ "get" }
|
71
|
-
@pendragon.post("/"){ "post" }
|
72
|
-
@pendragon.delete("/"){ "delete" }
|
73
|
-
@pendragon.put("/"){ "put" }
|
74
|
-
@pendragon.head("/"){ }
|
75
|
-
get("/")
|
76
|
-
assert_equal "get", body
|
77
|
-
post("/")
|
78
|
-
assert_equal "post", body
|
79
|
-
delete("/")
|
80
|
-
assert_equal "delete", body
|
81
|
-
put("/")
|
82
|
-
assert_equal "put", body
|
83
|
-
head("/")
|
84
|
-
assert_equal "", body
|
85
|
-
end
|
86
|
-
|
87
|
-
should "support for correct options" do
|
88
|
-
named_route = @pendragon.get("/name/:name", name: :named_route){}
|
89
|
-
incorrect_route = @pendragon.get("/router", router: :incorrect!){}
|
90
|
-
status_route = @pendragon.get("/router", status: 200){}
|
91
|
-
assert_equal :named_route, named_route.name
|
92
|
-
assert_equal "/name/foo", @pendragon.path(:named_route, name: :foo)
|
93
|
-
assert_equal true, incorrect_route.instance_variable_get(:@router).instance_of?(Pendragon::Router)
|
94
|
-
end
|
95
|
-
|
96
|
-
should "allow to throw :pass for routing like journey" do
|
97
|
-
foo = nil
|
98
|
-
@pendragon.get("/"){ foo = "yay"; throw :pass }
|
99
|
-
@pendragon.get("/"){ foo }
|
100
|
-
get "/"
|
101
|
-
assert_equal "yay", body
|
102
|
-
end
|
103
|
-
|
104
|
-
should "recognize the route if non-cut pattern is matched" do
|
105
|
-
@pendragon.get("/hey/"){ "say" }
|
106
|
-
@pendragon.get("/hey"){ "yay" }
|
107
|
-
get "/hey/"
|
108
|
-
assert_equal "say", body
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
describe "route options" do
|
113
|
-
should "support for :capture option" do
|
114
|
-
capture = {foo: /\d+/, bar: "bar"}
|
115
|
-
route = @pendragon.get("/:foo/:bar", capture: capture){}
|
116
|
-
assert_equal route.capture, capture
|
117
|
-
assert_equal nil, route.match("/foo/bar")
|
118
|
-
assert_equal nil, route.match("/123/baz")
|
119
|
-
assert_equal true, route.match("/123/bar").instance_of?(MatchData)
|
120
|
-
end
|
121
|
-
|
122
|
-
should "support for :name option" do
|
123
|
-
route = @pendragon.get("/name/:name", name: :named_route){}
|
124
|
-
assert_equal :named_route, route.name
|
125
|
-
assert_equal "/name/foo", @pendragon.path(:named_route, name: :foo)
|
126
|
-
end
|
127
|
-
|
128
|
-
should "not support for :router option" do
|
129
|
-
route = @pendragon.get("/router", router: :incorrect!){}
|
130
|
-
assert_equal true, route.instance_variable_get(:@router).instance_of?(Pendragon::Router)
|
131
|
-
end
|
132
|
-
|
133
|
-
should "support for :order option" do
|
134
|
-
@pendragon.get("/", order: 2){ "three" }
|
135
|
-
@pendragon.get("/", order: 0){ "one" }
|
136
|
-
@pendragon.get("/", order: 1){ "two" }
|
137
|
-
request = Rack::MockRequest.env_for("/")
|
138
|
-
assert_equal ["one", "two", "three"], @pendragon.recognize(request).map{|route, _| route.call }
|
139
|
-
end
|
140
|
-
|
141
|
-
should "support for :status option" do
|
142
|
-
@pendragon.get("/", status: 201){ "hey" }
|
143
|
-
get "/"
|
144
|
-
assert_equal 201, status
|
145
|
-
end
|
146
|
-
|
147
|
-
should "support for :header option" do
|
148
|
-
header = {"Content-Type" => "text/plain;"}
|
149
|
-
@pendragon.get("/", header: header){ "hey" }
|
150
|
-
get "/"
|
151
|
-
assert_equal header.merge("Content-Length" => "3"), headers
|
152
|
-
end
|
153
|
-
end
|
154
|
-
|
155
|
-
describe "regexp routing" do
|
156
|
-
before(:each){ @pendragon.reset! }
|
157
|
-
|
158
|
-
should "basic route of regexp" do
|
159
|
-
@pendragon.add(:get, /\/(\d+)/){|params| params[:captures].join(",") }
|
160
|
-
@pendragon.add(:get, /\/(foo|bar)(baz)?/){|params| params[:captures].compact.join(",") }
|
161
|
-
|
162
|
-
get("/123")
|
163
|
-
assert_equal "123", body
|
164
|
-
get("/foo")
|
165
|
-
assert_equal "foo", body
|
166
|
-
get("/foobaz")
|
167
|
-
assert_equal "foo,baz", body
|
168
|
-
end
|
169
|
-
end
|
170
|
-
|
171
|
-
describe "generate path" do
|
172
|
-
before(:each){ @pendragon.reset! }
|
173
|
-
|
174
|
-
should "basic route" do
|
175
|
-
index = @pendragon.add(:get, "/", :name => :index){}
|
176
|
-
foo_bar = @pendragon.add(:post, "/foo/bar", :name => :foo_bar){}
|
177
|
-
users = @pendragon.add(:get, "/users/:user_id", :name => :users){}
|
178
|
-
|
179
|
-
assert_equal "/", @pendragon.path(:index)
|
180
|
-
assert_equal "/foo/bar", @pendragon.path(:foo_bar)
|
181
|
-
assert_equal "/users/1", @pendragon.path(:users, :user_id => 1)
|
182
|
-
assert_equal "/users/1?query=string", @pendragon.path(:users, :user_id => 1, :query => "string")
|
183
|
-
end
|
184
|
-
|
185
|
-
should "regexp" do
|
186
|
-
index = @pendragon.add(:get, /.+?/, :name => :index){}
|
187
|
-
foo_bar = @pendragon.add(:post, /\d+/, :name => :foo_bar){}
|
188
|
-
|
189
|
-
assert_equal /.+?/, @pendragon.path(:index)
|
190
|
-
assert_equal /\d+/, @pendragon.path(:foo_bar)
|
191
|
-
end
|
192
|
-
end
|
193
|
-
|
194
|
-
describe "#new allows block" do
|
195
|
-
should "#new support for block." do
|
196
|
-
foo = @pendragon.add(:get, "/", :name => :foo){"foo"}
|
197
|
-
bar = @pendragon.add(:post, "/", :name => :bar){"bar"}
|
198
|
-
get("/")
|
199
|
-
assert_equal "foo", body
|
200
|
-
post("/")
|
201
|
-
assert_equal "bar", body
|
202
|
-
assert_equal "/", @app.path(:foo)
|
203
|
-
assert_equal "/", @app.path(:bar)
|
204
|
-
end
|
205
|
-
end
|
206
|
-
|
207
|
-
describe "splat" do
|
208
|
-
should "support splat pattern" do
|
209
|
-
@pendragon.get("/hey/*"){|s| "#{s[:splat][0]} splat!" }
|
210
|
-
get "/hey/hello"
|
211
|
-
assert_equal "hello splat!", body
|
212
|
-
end
|
213
|
-
|
214
|
-
should "support splat pattern including slash" do
|
215
|
-
@pendragon.get("/hey/*"){|s| "#{s[:splat][0]} splat!" }
|
216
|
-
get "/hey/hello/world"
|
217
|
-
assert_equal "hello/world splat!", body
|
218
|
-
end
|
219
|
-
end
|
220
|
-
|
221
|
-
describe "header" do
|
222
|
-
should "set Allow header when occur 405" do
|
223
|
-
@pendragon.get("/"){}
|
224
|
-
@pendragon.put("/"){}
|
225
|
-
post "/"
|
226
|
-
assert_equal "GET, PUT", response.header['Allow']
|
227
|
-
end
|
228
|
-
end
|
229
|
-
end
|