pancake 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.textile +95 -0
- data/Rakefile +56 -0
- data/TODO +17 -0
- data/bin/jeweler +19 -0
- data/bin/pancake-gen +17 -0
- data/bin/rubyforge +19 -0
- data/lib/pancake/bootloaders.rb +180 -0
- data/lib/pancake/configuration.rb +145 -0
- data/lib/pancake/constants.rb +5 -0
- data/lib/pancake/core_ext/class.rb +44 -0
- data/lib/pancake/core_ext/object.rb +22 -0
- data/lib/pancake/core_ext/symbol.rb +15 -0
- data/lib/pancake/defaults/configuration.rb +22 -0
- data/lib/pancake/defaults/middlewares.rb +1 -0
- data/lib/pancake/errors.rb +61 -0
- data/lib/pancake/generators/base.rb +12 -0
- data/lib/pancake/generators/micro_generator.rb +17 -0
- data/lib/pancake/generators/short_generator.rb +17 -0
- data/lib/pancake/generators/stack_generator.rb +17 -0
- data/lib/pancake/generators/templates/common/dotgitignore +22 -0
- data/lib/pancake/generators/templates/common/dothtaccess +17 -0
- data/lib/pancake/generators/templates/micro/%stack_name%/%stack_name%.rb.tt +8 -0
- data/lib/pancake/generators/templates/micro/%stack_name%/config.ru.tt +12 -0
- data/lib/pancake/generators/templates/micro/%stack_name%/pancake.init.tt +1 -0
- data/lib/pancake/generators/templates/micro/%stack_name%/public/.empty_directory +0 -0
- data/lib/pancake/generators/templates/micro/%stack_name%/tmp/.empty_directory +0 -0
- data/lib/pancake/generators/templates/micro/%stack_name%/views/root.html.haml +1 -0
- data/lib/pancake/generators/templates/short/%stack_name%/LICENSE.tt +20 -0
- data/lib/pancake/generators/templates/short/%stack_name%/README.tt +7 -0
- data/lib/pancake/generators/templates/short/%stack_name%/Rakefile.tt +50 -0
- data/lib/pancake/generators/templates/short/%stack_name%/VERSION.tt +1 -0
- data/lib/pancake/generators/templates/short/%stack_name%/lib/%stack_name%/%stack_name%.rb.tt +6 -0
- data/lib/pancake/generators/templates/short/%stack_name%/lib/%stack_name%/config.ru.tt +10 -0
- data/lib/pancake/generators/templates/short/%stack_name%/lib/%stack_name%/mounts/.empty_directory +0 -0
- data/lib/pancake/generators/templates/short/%stack_name%/lib/%stack_name%/public/.empty_directory +0 -0
- data/lib/pancake/generators/templates/short/%stack_name%/lib/%stack_name%/tmp/.empty_directory +0 -0
- data/lib/pancake/generators/templates/short/%stack_name%/lib/%stack_name%/views/root.html.haml +2 -0
- data/lib/pancake/generators/templates/short/%stack_name%/lib/%stack_name%.rb.tt +5 -0
- data/lib/pancake/generators/templates/short/%stack_name%/pancake.init.tt +1 -0
- data/lib/pancake/generators/templates/short/%stack_name%/spec/%stack_name%_spec.rb.tt +7 -0
- data/lib/pancake/generators/templates/short/%stack_name%/spec/spec_helper.rb.tt +9 -0
- data/lib/pancake/generators/templates/stack/%stack_name%/LICENSE.tt +20 -0
- data/lib/pancake/generators/templates/stack/%stack_name%/README.tt +7 -0
- data/lib/pancake/generators/templates/stack/%stack_name%/Rakefile.tt +50 -0
- data/lib/pancake/generators/templates/stack/%stack_name%/VERSION.tt +1 -0
- data/lib/pancake/generators/templates/stack/%stack_name%/lib/%stack_name%/config/environments/development.rb.tt +18 -0
- data/lib/pancake/generators/templates/stack/%stack_name%/lib/%stack_name%/config/environments/production.rb.tt +18 -0
- data/lib/pancake/generators/templates/stack/%stack_name%/lib/%stack_name%/config/router.rb.tt +6 -0
- data/lib/pancake/generators/templates/stack/%stack_name%/lib/%stack_name%/config.ru.tt +12 -0
- data/lib/pancake/generators/templates/stack/%stack_name%/lib/%stack_name%/gems/cache/.empty_directory +0 -0
- data/lib/pancake/generators/templates/stack/%stack_name%/lib/%stack_name%/mounts/.empty_directory +0 -0
- data/lib/pancake/generators/templates/stack/%stack_name%/lib/%stack_name%/public/.empty_directory +0 -0
- data/lib/pancake/generators/templates/stack/%stack_name%/lib/%stack_name%/tmp/.empty_directory +0 -0
- data/lib/pancake/generators/templates/stack/%stack_name%/lib/%stack_name%.rb.tt +3 -0
- data/lib/pancake/generators/templates/stack/%stack_name%/pancake.init.tt +1 -0
- data/lib/pancake/generators/templates/stack/%stack_name%/spec/%stack_name%_spec.rb.tt +7 -0
- data/lib/pancake/generators/templates/stack/%stack_name%/spec/spec_helper.rb.tt +9 -0
- data/lib/pancake/generators.rb +8 -0
- data/lib/pancake/hooks/inheritable_inner_classes.rb +60 -0
- data/lib/pancake/hooks/on_inherit.rb +34 -0
- data/lib/pancake/logger.rb +200 -0
- data/lib/pancake/master.rb +123 -0
- data/lib/pancake/middleware.rb +347 -0
- data/lib/pancake/middlewares/logger.rb +16 -0
- data/lib/pancake/middlewares/static.rb +38 -0
- data/lib/pancake/mime_types.rb +265 -0
- data/lib/pancake/mixins/publish/action_options.rb +104 -0
- data/lib/pancake/mixins/publish.rb +125 -0
- data/lib/pancake/mixins/render/render.rb +168 -0
- data/lib/pancake/mixins/render/template.rb +23 -0
- data/lib/pancake/mixins/render/view_context.rb +21 -0
- data/lib/pancake/mixins/render.rb +109 -0
- data/lib/pancake/mixins/request_helper.rb +100 -0
- data/lib/pancake/mixins/stack_helper.rb +46 -0
- data/lib/pancake/mixins/url.rb +10 -0
- data/lib/pancake/paths.rb +218 -0
- data/lib/pancake/router.rb +99 -0
- data/lib/pancake/stack/app.rb +10 -0
- data/lib/pancake/stack/bootloader.rb +79 -0
- data/lib/pancake/stack/configuration.rb +44 -0
- data/lib/pancake/stack/middleware.rb +0 -0
- data/lib/pancake/stack/router.rb +21 -0
- data/lib/pancake/stack/stack.rb +66 -0
- data/lib/pancake/stacks/short/bootloaders.rb +13 -0
- data/lib/pancake/stacks/short/controller.rb +116 -0
- data/lib/pancake/stacks/short/default/views/base.html.haml +5 -0
- data/lib/pancake/stacks/short/stack.rb +187 -0
- data/lib/pancake/stacks/short.rb +3 -0
- data/lib/pancake.rb +58 -0
- data/spec/helpers/helpers.rb +20 -0
- data/spec/helpers/matchers.rb +25 -0
- data/spec/pancake/bootloaders_spec.rb +109 -0
- data/spec/pancake/configuration_spec.rb +177 -0
- data/spec/pancake/constants_spec.rb +7 -0
- data/spec/pancake/defaults/configuration_spec.rb +58 -0
- data/spec/pancake/fixtures/foo_stack/pancake.init +0 -0
- data/spec/pancake/fixtures/middlewares/other_public/two.html +1 -0
- data/spec/pancake/fixtures/middlewares/public/foo#bar.html +1 -0
- data/spec/pancake/fixtures/middlewares/public/one.html +1 -0
- data/spec/pancake/fixtures/paths/controllers/controller1.rb +0 -0
- data/spec/pancake/fixtures/paths/controllers/controller2.rb +0 -0
- data/spec/pancake/fixtures/paths/controllers/controller3.rb +0 -0
- data/spec/pancake/fixtures/paths/models/model1.rb +0 -0
- data/spec/pancake/fixtures/paths/models/model2.rb +0 -0
- data/spec/pancake/fixtures/paths/models/model3.rb +0 -0
- data/spec/pancake/fixtures/paths/stack/controllers/controller1.rb +0 -0
- data/spec/pancake/fixtures/paths/stack/models/model3.rb +0 -0
- data/spec/pancake/fixtures/paths/stack/views/view1.erb +0 -0
- data/spec/pancake/fixtures/paths/stack/views/view1.rb +0 -0
- data/spec/pancake/fixtures/paths/stack/views/view2.erb +0 -0
- data/spec/pancake/fixtures/paths/stack/views/view2.haml +0 -0
- data/spec/pancake/fixtures/render_templates/context_template.html.erb +2 -0
- data/spec/pancake/fixtures/render_templates/erb_template.html.erb +1 -0
- data/spec/pancake/fixtures/render_templates/erb_template.json.erb +1 -0
- data/spec/pancake/fixtures/render_templates/haml_template.html.haml +1 -0
- data/spec/pancake/fixtures/render_templates/haml_template.xml.haml +1 -0
- data/spec/pancake/fixtures/render_templates/templates/context.erb +2 -0
- data/spec/pancake/fixtures/render_templates/view_context/capture_erb.erb +5 -0
- data/spec/pancake/fixtures/render_templates/view_context/capture_haml.haml +4 -0
- data/spec/pancake/fixtures/render_templates/view_context/concat_erb.erb +2 -0
- data/spec/pancake/fixtures/render_templates/view_context/concat_haml.haml +2 -0
- data/spec/pancake/fixtures/render_templates/view_context/context.erb +3 -0
- data/spec/pancake/fixtures/render_templates/view_context/context2.erb +3 -0
- data/spec/pancake/fixtures/render_templates/view_context/helper_methods.erb +3 -0
- data/spec/pancake/fixtures/render_templates/view_context/inherited_erb_from_haml.erb +5 -0
- data/spec/pancake/fixtures/render_templates/view_context/inherited_erb_level_0.erb +5 -0
- data/spec/pancake/fixtures/render_templates/view_context/inherited_erb_level_1.erb +5 -0
- data/spec/pancake/fixtures/render_templates/view_context/inherited_haml_from_erb.haml +4 -0
- data/spec/pancake/fixtures/render_templates/view_context/inherited_haml_level_0.haml +4 -0
- data/spec/pancake/fixtures/render_templates/view_context/inherited_haml_level_1.haml +4 -0
- data/spec/pancake/fixtures/render_templates/view_context/nested_content_level_0.haml +6 -0
- data/spec/pancake/fixtures/render_templates/view_context/nested_content_level_1.haml +4 -0
- data/spec/pancake/fixtures/render_templates/view_context/nested_inner.erb +1 -0
- data/spec/pancake/fixtures/render_templates/view_context/nested_outer.erb +3 -0
- data/spec/pancake/fixtures/render_templates/view_context/super_erb_from_erb_0.erb +5 -0
- data/spec/pancake/fixtures/render_templates/view_context/super_erb_from_erb_1.erb +6 -0
- data/spec/pancake/fixtures/render_templates/view_context/super_erb_from_haml_0.erb +5 -0
- data/spec/pancake/fixtures/render_templates/view_context/super_erb_from_haml_1.erb +6 -0
- data/spec/pancake/fixtures/render_templates/view_context/super_haml_from_erb_0.haml +4 -0
- data/spec/pancake/fixtures/render_templates/view_context/super_haml_from_erb_1.haml +5 -0
- data/spec/pancake/fixtures/render_templates/view_context/super_haml_from_haml_0.haml +5 -0
- data/spec/pancake/fixtures/render_templates/view_context/super_haml_from_haml_1.haml +5 -0
- data/spec/pancake/fixtures/stacks/short/foobar/other_root/views/base.html.haml +4 -0
- data/spec/pancake/fixtures/stacks/short/foobar/views/basic.html.haml +1 -0
- data/spec/pancake/fixtures/stacks/short/foobar/views/inherited_from_base.html.haml +5 -0
- data/spec/pancake/hooks/on_inherit_spec.rb +65 -0
- data/spec/pancake/inheritance_spec.rb +100 -0
- data/spec/pancake/middleware_spec.rb +401 -0
- data/spec/pancake/middlewares/logger_spec.rb +29 -0
- data/spec/pancake/middlewares/static_spec.rb +83 -0
- data/spec/pancake/mime_types_spec.rb +234 -0
- data/spec/pancake/mixins/publish_spec.rb +94 -0
- data/spec/pancake/mixins/render/template_spec.rb +69 -0
- data/spec/pancake/mixins/render/view_context_spec.rb +248 -0
- data/spec/pancake/mixins/render_spec.rb +56 -0
- data/spec/pancake/mixins/request_helper_spec.rb +27 -0
- data/spec/pancake/mixins/stack_helper_spec.rb +46 -0
- data/spec/pancake/pancake_spec.rb +90 -0
- data/spec/pancake/paths_spec.rb +210 -0
- data/spec/pancake/stack/app_spec.rb +28 -0
- data/spec/pancake/stack/bootloader_spec.rb +41 -0
- data/spec/pancake/stack/middleware_spec.rb +0 -0
- data/spec/pancake/stack/router_spec.rb +282 -0
- data/spec/pancake/stack/stack_configuration_spec.rb +101 -0
- data/spec/pancake/stack/stack_spec.rb +60 -0
- data/spec/pancake/stacks/short/controller_spec.rb +322 -0
- data/spec/pancake/stacks/short/middlewares_spec.rb +22 -0
- data/spec/pancake/stacks/short/router_spec.rb +136 -0
- data/spec/pancake/stacks/short/stack_spec.rb +64 -0
- data/spec/spec_helper.rb +23 -0
- metadata +294 -0
@@ -0,0 +1,322 @@
|
|
1
|
+
require File.join(File.expand_path(File.dirname(__FILE__)),'..', '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
describe Pancake::Stacks::Short::Controller do
|
4
|
+
|
5
|
+
before do
|
6
|
+
class ::ShortFoo < Pancake::Stacks::Short
|
7
|
+
add_root(__FILE__)
|
8
|
+
add_root(File.expand_path(File.dirname(__FILE__)), "..", "fixtures", "stacks", "short", "foobar")
|
9
|
+
class Controller
|
10
|
+
def do_dispatch!
|
11
|
+
dispatch!
|
12
|
+
end
|
13
|
+
|
14
|
+
publish
|
15
|
+
def show; "show"; end
|
16
|
+
|
17
|
+
publish
|
18
|
+
def index; "index"; end
|
19
|
+
|
20
|
+
protected
|
21
|
+
def a_protected_method; "protected"; end
|
22
|
+
|
23
|
+
private
|
24
|
+
def a_private_method; "private"; end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
after do
|
30
|
+
clear_constants "ShortFoo", :ShortBar
|
31
|
+
end
|
32
|
+
|
33
|
+
def app
|
34
|
+
ShortFoo.stackup
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should have a Controller" do
|
38
|
+
Pancake::Stacks::Short.constants.map(&:to_s).should include("Controller")
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should inherit the subclass controller from the parent controller" do
|
42
|
+
ShortFoo::Controller.should inherit_from(Pancake::Stacks::Short::Controller)
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "dispatching an action" do
|
46
|
+
before do
|
47
|
+
@controller = ShortFoo::Controller.new(env_for)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should call the 'show' action" do
|
51
|
+
@controller.params["action"] = "show"
|
52
|
+
result = @controller.do_dispatch!
|
53
|
+
result[0].should == 200
|
54
|
+
result[2].body.join.should == "show"
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should raise a Pancake::Response::NotFound exception when an action is now found" do
|
58
|
+
@controller.params["action"] = :does_not_exist
|
59
|
+
result = @controller.do_dispatch!
|
60
|
+
result[0].should == 404
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should not dispatch to a protected method" do
|
64
|
+
@controller.params["action"] = "a_protected_method"
|
65
|
+
result = @controller.do_dispatch!
|
66
|
+
result[0].should == 404
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should not dispatch to a private method" do
|
70
|
+
@controller.params["action"] = "a_private_method"
|
71
|
+
result = @controller.do_dispatch!
|
72
|
+
result[0].should == 404
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should raise an exception if pancake has told it not to handle errors" do
|
76
|
+
Pancake.should_receive(:handle_errors?).and_return(false)
|
77
|
+
@controller.params["action"] = "a_private_method"
|
78
|
+
lambda do
|
79
|
+
@controller.do_dispatch!
|
80
|
+
end.should raise_error
|
81
|
+
end
|
82
|
+
|
83
|
+
describe "helper in methods" do
|
84
|
+
before do
|
85
|
+
module PancakeTestHelper
|
86
|
+
def some_helper_method
|
87
|
+
"foo"
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
class ShortFoo
|
92
|
+
class Controller
|
93
|
+
include PancakeTestHelper
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
after do
|
98
|
+
clear_constants "PancakeTestHelper"
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should not call a helper method" do
|
102
|
+
@controller.params["action"] = "some_helper_method"
|
103
|
+
result = @controller.do_dispatch!
|
104
|
+
result[0].should == 404
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
describe "accept type negotiations" do
|
111
|
+
before do
|
112
|
+
class ::ShortBar < Pancake::Stacks::Short
|
113
|
+
roots << Pancake.get_root(__FILE__)
|
114
|
+
# makes the dispatch method public
|
115
|
+
def do_dispatch!
|
116
|
+
dispatch!
|
117
|
+
end
|
118
|
+
|
119
|
+
provides :json, :xml, :text
|
120
|
+
|
121
|
+
get "/foo/bar(.:format)" do
|
122
|
+
"format #{content_type.inspect}"
|
123
|
+
end
|
124
|
+
end # ShortBar
|
125
|
+
end # before
|
126
|
+
|
127
|
+
def app
|
128
|
+
ShortBar.stackup
|
129
|
+
end
|
130
|
+
|
131
|
+
it "should get json by default" do
|
132
|
+
result = get "/foo/bar", {}, "HTTP_ACCEPT" => "application/json"
|
133
|
+
result.status.should == 200
|
134
|
+
result.headers["Content-Type"].should == "application/json"
|
135
|
+
result.body.to_s.should == "format :json"
|
136
|
+
end
|
137
|
+
|
138
|
+
it "should get xml when specified" do
|
139
|
+
result = get "/foo/bar.xml"
|
140
|
+
result.status.should == 200
|
141
|
+
result.headers["Content-Type"].should == "application/xml"
|
142
|
+
result.body.to_s.should == "format :xml"
|
143
|
+
end
|
144
|
+
|
145
|
+
it "should get json when specified with */*" do
|
146
|
+
result = get "/foo/bar", {}, "HTTP_ACCEPT" => "*/*"
|
147
|
+
result.status.should == 200
|
148
|
+
result.body.to_s.should == "format :json"
|
149
|
+
result.headers["Content-Type"].should == "application/json"
|
150
|
+
end
|
151
|
+
|
152
|
+
it "should get the default when specified with */*" do
|
153
|
+
result = get "/foo/bar", {}, "HTTP_ACCEPT" => "application/xml,*/*"
|
154
|
+
result.status.should == 200
|
155
|
+
result.body.to_s.should == "format :json"
|
156
|
+
result.headers["Content-Type"].should == "application/json"
|
157
|
+
end
|
158
|
+
|
159
|
+
it "should use the format in preference to the content type" do
|
160
|
+
result = get "/foo/bar.xml", {}, "HTTP_ACCEPT" => "*/*"
|
161
|
+
result.status.should == 200
|
162
|
+
result.body.to_s.should == "format :xml"
|
163
|
+
result.headers["Content-Type"].should == "application/xml"
|
164
|
+
end
|
165
|
+
|
166
|
+
it "should get json by default" do
|
167
|
+
result = get "/foo/bar"
|
168
|
+
result.status.should == 200
|
169
|
+
result.body.to_s.should == "format :json"
|
170
|
+
result.headers["Content-Type"].should == "application/json"
|
171
|
+
end
|
172
|
+
|
173
|
+
it "should correctly negotiate different scenarios" do
|
174
|
+
r = get "/foo/bar", {}, {}
|
175
|
+
r.body.should == "format :json"
|
176
|
+
r = get "/foo/bar.xml", {}, {}
|
177
|
+
r.body.should == "format :xml"
|
178
|
+
r = get "/foo/bar", {}, {}
|
179
|
+
r.body.should == "format :json"
|
180
|
+
r = get "/foo/bar", {}, "HTTP_ACCEPT" => "application/xml"
|
181
|
+
r.body.should == "format :xml"
|
182
|
+
r = get "/foo/bar.json"
|
183
|
+
r.body.should == "format :json"
|
184
|
+
end
|
185
|
+
|
186
|
+
it "should negotiate based on extension" do
|
187
|
+
r = get "/foo/bar"
|
188
|
+
r.body.should == "format :json"
|
189
|
+
r = get "/foo/bar.text"
|
190
|
+
r.body.should == "format :text"
|
191
|
+
r = get "/foo/bar.xml"
|
192
|
+
r.body.should == "format :xml"
|
193
|
+
r = get "/foo/bar.txt"
|
194
|
+
r.body.should == "format :text"
|
195
|
+
end
|
196
|
+
|
197
|
+
it "should not provide a response to a format that is not provided" do
|
198
|
+
r = get "/foo/bar.svg"
|
199
|
+
r.status.should == 406
|
200
|
+
end
|
201
|
+
end # Accept type negotiations
|
202
|
+
|
203
|
+
describe "error handling" do
|
204
|
+
before do
|
205
|
+
class ::ShortFoo
|
206
|
+
provides :html, :xml
|
207
|
+
|
208
|
+
get "/foo(.:format)" do
|
209
|
+
"HERE"
|
210
|
+
end
|
211
|
+
|
212
|
+
get "/bad" do
|
213
|
+
raise "This is bad"
|
214
|
+
end
|
215
|
+
|
216
|
+
get "/template/:name" do
|
217
|
+
render params[:name]
|
218
|
+
end
|
219
|
+
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
describe "default error handling" do
|
224
|
+
def app
|
225
|
+
ShortFoo.stackup
|
226
|
+
end
|
227
|
+
|
228
|
+
it "should handle a NotFound by default" do
|
229
|
+
result = get "/does_not_exist"
|
230
|
+
result.status.should == 404
|
231
|
+
result.body.should include(Pancake::Errors::NotFound.description)
|
232
|
+
end
|
233
|
+
|
234
|
+
it "should return a 500 status for a Random Error by wrapping it in a Pancake::Errors::Server" do
|
235
|
+
result = get "/bad"
|
236
|
+
result.status.should == 500
|
237
|
+
result.body.should include(Pancake::Errors::Server.description)
|
238
|
+
end
|
239
|
+
|
240
|
+
it "should handle a NotAcceptable error" do
|
241
|
+
result = get "/foo.no_format_i_know_of"
|
242
|
+
result.status.should == 406
|
243
|
+
result.body.should include(Pancake::Errors::NotAcceptable.description)
|
244
|
+
end
|
245
|
+
|
246
|
+
it "should return 406 for a format that is in pancake but not in the group" do
|
247
|
+
r = get "/foo.svg"
|
248
|
+
r.status.should == 406
|
249
|
+
end
|
250
|
+
|
251
|
+
|
252
|
+
end
|
253
|
+
|
254
|
+
describe "custom error handling" do
|
255
|
+
before do
|
256
|
+
ShortFoo.handle_exception do |error|
|
257
|
+
out = ""
|
258
|
+
out << "CUSTOM "
|
259
|
+
out << error.name
|
260
|
+
out << ": "
|
261
|
+
out << error.description
|
262
|
+
end
|
263
|
+
|
264
|
+
ShortFoo.get "/bad" do
|
265
|
+
raise "Really Bad"
|
266
|
+
end
|
267
|
+
end
|
268
|
+
|
269
|
+
after do
|
270
|
+
ShortFoo.handle_exception(&Pancake::Stacks::Short::Controller::DEFAULT_EXCEPTION_HANDLER)
|
271
|
+
end
|
272
|
+
|
273
|
+
it "should handle Pancake::Errors::NotFound errors" do
|
274
|
+
r = get "/not_a_thing"
|
275
|
+
r.status.should == 404
|
276
|
+
r.body.should include("CUSTOM")
|
277
|
+
r.body.should include(Pancake::Errors::NotFound.description)
|
278
|
+
end
|
279
|
+
|
280
|
+
it "should handle an unknown server error" do
|
281
|
+
r = get "/bad"
|
282
|
+
r.status.should == 500
|
283
|
+
r.body.should include("CUSTOM")
|
284
|
+
r.body.should include(Pancake::Errors::Server.description)
|
285
|
+
end
|
286
|
+
|
287
|
+
it "should let me do stuff on an instance level inside the handle exception" do
|
288
|
+
ShortFoo.handle_exception do |error|
|
289
|
+
self.status = 123
|
290
|
+
"BOOO!"
|
291
|
+
end
|
292
|
+
|
293
|
+
r = get "/bad"
|
294
|
+
r.status.should == 123
|
295
|
+
r.body.should == "BOOO!"
|
296
|
+
end
|
297
|
+
|
298
|
+
end
|
299
|
+
|
300
|
+
describe "rendering" do
|
301
|
+
|
302
|
+
it "should render a template" do
|
303
|
+
result = get "/template/basic"
|
304
|
+
result.body.should include("basic template")
|
305
|
+
end
|
306
|
+
|
307
|
+
it "should inherit from a base view provided by short stacks" do
|
308
|
+
File.file?(File.join(File.expand_path(File.dirname(__FILE__)), "..", "fixtures", "stacks", "short", "foobar", "views", "base.html.haml")).should be_false
|
309
|
+
result = get "/template/inherited_from_base"
|
310
|
+
result.body.should include("inherited from base")
|
311
|
+
result.body.should include("Pancake")
|
312
|
+
end
|
313
|
+
|
314
|
+
it "should allow me to overwrite the base tempalte in later roots" do
|
315
|
+
ShortFoo.add_root(File.expand_path(File.dirname(__FILE__)), "..", "fixtures", "stacks", "short", "foobar", "other_root")
|
316
|
+
result = get "/template/inherited_from_base"
|
317
|
+
result.body.should include("inherited from base")
|
318
|
+
result.body.should include("Not the default pancake")
|
319
|
+
end
|
320
|
+
end
|
321
|
+
end
|
322
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../../spec_helper'
|
2
|
+
|
3
|
+
describe "short stack middleware" do
|
4
|
+
before do
|
5
|
+
class ::FooBar < Pancake::Stacks::Short
|
6
|
+
add_root(__FILE__)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
after do
|
11
|
+
clear_constants :FooBar
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should include the Pancake::Middlewares::Static middleware" do
|
15
|
+
FooBar.stackup
|
16
|
+
middleware = FooBar.middlewares.detect do |m|
|
17
|
+
m.middleware == Pancake::Middlewares::Static
|
18
|
+
end
|
19
|
+
middleware.should_not be_nil
|
20
|
+
middleware.args.should == [FooBar]
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
require File.join(File.expand_path(File.dirname(__FILE__)),'..', '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
describe Pancake::Stacks::Short, "routes" do
|
4
|
+
before do
|
5
|
+
class ::RoutedShortStack < Pancake::Stacks::Short
|
6
|
+
roots << Pancake.get_root(__FILE__)
|
7
|
+
|
8
|
+
get "/", :_name => :root do
|
9
|
+
"get - /"
|
10
|
+
end
|
11
|
+
|
12
|
+
get "/foo", :_name => :foo do
|
13
|
+
"get - foo"
|
14
|
+
end
|
15
|
+
|
16
|
+
get "/bar", :_name => :bar do
|
17
|
+
"get - bar"
|
18
|
+
end
|
19
|
+
|
20
|
+
post "/foo" do
|
21
|
+
"post - foo"
|
22
|
+
end
|
23
|
+
|
24
|
+
post "/bar" do
|
25
|
+
"post - bar"
|
26
|
+
end
|
27
|
+
|
28
|
+
put "/foo" do
|
29
|
+
"put - foo"
|
30
|
+
end
|
31
|
+
|
32
|
+
put "/bar" do
|
33
|
+
"put - bar"
|
34
|
+
end
|
35
|
+
|
36
|
+
delete "/foo" do
|
37
|
+
"delete - foo"
|
38
|
+
end
|
39
|
+
|
40
|
+
delete "/bar" do
|
41
|
+
"delete - bar"
|
42
|
+
end
|
43
|
+
|
44
|
+
get "/baz/:var(/:date)" do
|
45
|
+
"done: var == #{params[:var]} : date == #{params[:date]}"
|
46
|
+
end.name(:baz)
|
47
|
+
end
|
48
|
+
@app = RoutedShortStack.stackup
|
49
|
+
end
|
50
|
+
after do
|
51
|
+
clear_constants "RoutedShortStack", "FooApp", "BarApp"
|
52
|
+
end
|
53
|
+
|
54
|
+
def app
|
55
|
+
@app
|
56
|
+
end
|
57
|
+
|
58
|
+
%w(foo bar).each do |item|
|
59
|
+
%w(get post put delete).each do |method|
|
60
|
+
it "should #{method} /#{item}" do
|
61
|
+
result = self.send(method, "/#{item}")
|
62
|
+
result.status.should == 200
|
63
|
+
result.body.should == "#{method} - #{item}"
|
64
|
+
end
|
65
|
+
end # get post put delete
|
66
|
+
end # foo bar
|
67
|
+
|
68
|
+
it "should return a not found if we try to get an action that has not been defined" do
|
69
|
+
result = get "/blah"
|
70
|
+
result.status.should == 404
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
it "should handle tricky routes" do
|
75
|
+
result = get "/baz/hassox"
|
76
|
+
result.status.should == 200
|
77
|
+
result.body.should == "done: var == hassox : date == "
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should handle tricky routes with optional parameters" do
|
81
|
+
result = get "/baz/hassox/2009-08-21"
|
82
|
+
result.status.should == 200
|
83
|
+
result.body.should == "done: var == hassox : date == 2009-08-21"
|
84
|
+
end
|
85
|
+
|
86
|
+
describe "url generation" do
|
87
|
+
it "should generate a simple named url" do
|
88
|
+
Pancake.url(RoutedShortStack, :foo).should == "/foo"
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should generate a complex named url" do
|
92
|
+
Pancake.url(RoutedShortStack, :baz, :var => "bar").should == "/baz/bar"
|
93
|
+
Pancake.url(RoutedShortStack, :baz, :var => "bar", :date => "today").should == "/baz/bar/today"
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should generate it's url when it's nested" do
|
97
|
+
class ::FooApp < Pancake::Stack; end
|
98
|
+
FooApp.router.mount(RoutedShortStack.stackup, "/short/stack")
|
99
|
+
Pancake.url(RoutedShortStack, :foo).should == "/short/stack/foo"
|
100
|
+
Pancake.url(RoutedShortStack, :baz, :var => "var", :date => "today").should == "/short/stack/baz/var/today"
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
describe "inherited route generation" do
|
105
|
+
before do
|
106
|
+
class ::FooApp < RoutedShortStack; end
|
107
|
+
@app = FooApp.stackup
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should generate an inherited simple url" do
|
111
|
+
Pancake.url(FooApp, :foo).should == "/foo"
|
112
|
+
end
|
113
|
+
|
114
|
+
it "should generate a complex url" do
|
115
|
+
Pancake.url(FooApp, :baz, :var => "the_var", :date => "today").should == "/baz/the_var/today"
|
116
|
+
end
|
117
|
+
|
118
|
+
%w(foo bar).each do |item|
|
119
|
+
%w(get post put delete).each do |method|
|
120
|
+
it "should #{method} /#{item}" do
|
121
|
+
result = self.send(method, "/#{item}")
|
122
|
+
result.status.should == 200
|
123
|
+
result.body.should == "#{method} - #{item}"
|
124
|
+
end
|
125
|
+
end # get post put delete
|
126
|
+
end # foo bar
|
127
|
+
|
128
|
+
it "should match the nested route without affecting the parent" do
|
129
|
+
class ::BarApp < Pancake::Stack; end
|
130
|
+
BarApp.router.mount(FooApp.stackup, "/mount/point")
|
131
|
+
|
132
|
+
Pancake.url(FooApp, :foo).should == "/mount/point/foo"
|
133
|
+
Pancake.url(RoutedShortStack, :foo).should == "/foo"
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../../spec_helper'
|
2
|
+
|
3
|
+
describe Pancake::Stacks::Short do
|
4
|
+
|
5
|
+
before do
|
6
|
+
$captures = []
|
7
|
+
class ::ShortMiddle
|
8
|
+
def initialize(app)
|
9
|
+
@app = app
|
10
|
+
end
|
11
|
+
|
12
|
+
def call(env)
|
13
|
+
$captures << ShortMiddle
|
14
|
+
@app.call(env)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class ::ShortFoo < Pancake::Stacks::Short
|
19
|
+
roots << Pancake.get_root(__FILE__)
|
20
|
+
add_root(__FILE__, "..", "..", "fixtures", "stacks", "short", "foobar")
|
21
|
+
add_root(__FILE__, "..", "..", "fixtures", "stacks", "short", "foobar", "other_root")
|
22
|
+
use ShortMiddle
|
23
|
+
|
24
|
+
|
25
|
+
get "/foo" do
|
26
|
+
"HERE"
|
27
|
+
end
|
28
|
+
|
29
|
+
get "/" do
|
30
|
+
$captures << self.class
|
31
|
+
render :inherited_from_base
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
after do
|
37
|
+
clear_constants :ShortFoo, :ShortMiddle, :OtherFoo
|
38
|
+
end
|
39
|
+
|
40
|
+
def app
|
41
|
+
ShortFoo.stackup
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should go through the middleware to get to the actions" do
|
45
|
+
get "/foo"
|
46
|
+
$captures.should == [ShortMiddle]
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "inheritance" do
|
50
|
+
before do
|
51
|
+
class ::OtherFoo < ShortFoo; end
|
52
|
+
ShortFoo.router.mount(OtherFoo.stackup, "/other")
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should render the same template in the child as it does in the parent" do
|
56
|
+
get "/"
|
57
|
+
$captures.pop.should == ShortFoo::Controller
|
58
|
+
last_response.should match(/inherited from base/)
|
59
|
+
get "/other/"
|
60
|
+
$captures.pop.should == OtherFoo::Controller
|
61
|
+
last_response.should match(/inherited from base/)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
$TESTING=true
|
2
|
+
require 'rubygems'
|
3
|
+
require 'date'
|
4
|
+
require 'rack'
|
5
|
+
require 'rack/test'
|
6
|
+
require 'spec/rake/spectask'
|
7
|
+
require 'spec'
|
8
|
+
require 'haml'
|
9
|
+
require 'json'
|
10
|
+
|
11
|
+
ENV['RACK_ENV'] = "test"
|
12
|
+
|
13
|
+
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
|
14
|
+
require 'pancake'
|
15
|
+
|
16
|
+
Dir[File.join(File.dirname(__FILE__), "helpers", "**/*.rb")].each{|f| require f}
|
17
|
+
|
18
|
+
|
19
|
+
Spec::Runner.configure do |config|
|
20
|
+
config.include(Pancake::Matchers)
|
21
|
+
config.include(Pancake::Spec::Helpers)
|
22
|
+
config.include(Rack::Test::Methods)
|
23
|
+
end
|