pancake 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (172) hide show
  1. data/LICENSE +20 -0
  2. data/README.textile +95 -0
  3. data/Rakefile +56 -0
  4. data/TODO +17 -0
  5. data/bin/jeweler +19 -0
  6. data/bin/pancake-gen +17 -0
  7. data/bin/rubyforge +19 -0
  8. data/lib/pancake/bootloaders.rb +180 -0
  9. data/lib/pancake/configuration.rb +145 -0
  10. data/lib/pancake/constants.rb +5 -0
  11. data/lib/pancake/core_ext/class.rb +44 -0
  12. data/lib/pancake/core_ext/object.rb +22 -0
  13. data/lib/pancake/core_ext/symbol.rb +15 -0
  14. data/lib/pancake/defaults/configuration.rb +22 -0
  15. data/lib/pancake/defaults/middlewares.rb +1 -0
  16. data/lib/pancake/errors.rb +61 -0
  17. data/lib/pancake/generators/base.rb +12 -0
  18. data/lib/pancake/generators/micro_generator.rb +17 -0
  19. data/lib/pancake/generators/short_generator.rb +17 -0
  20. data/lib/pancake/generators/stack_generator.rb +17 -0
  21. data/lib/pancake/generators/templates/common/dotgitignore +22 -0
  22. data/lib/pancake/generators/templates/common/dothtaccess +17 -0
  23. data/lib/pancake/generators/templates/micro/%stack_name%/%stack_name%.rb.tt +8 -0
  24. data/lib/pancake/generators/templates/micro/%stack_name%/config.ru.tt +12 -0
  25. data/lib/pancake/generators/templates/micro/%stack_name%/pancake.init.tt +1 -0
  26. data/lib/pancake/generators/templates/micro/%stack_name%/public/.empty_directory +0 -0
  27. data/lib/pancake/generators/templates/micro/%stack_name%/tmp/.empty_directory +0 -0
  28. data/lib/pancake/generators/templates/micro/%stack_name%/views/root.html.haml +1 -0
  29. data/lib/pancake/generators/templates/short/%stack_name%/LICENSE.tt +20 -0
  30. data/lib/pancake/generators/templates/short/%stack_name%/README.tt +7 -0
  31. data/lib/pancake/generators/templates/short/%stack_name%/Rakefile.tt +50 -0
  32. data/lib/pancake/generators/templates/short/%stack_name%/VERSION.tt +1 -0
  33. data/lib/pancake/generators/templates/short/%stack_name%/lib/%stack_name%/%stack_name%.rb.tt +6 -0
  34. data/lib/pancake/generators/templates/short/%stack_name%/lib/%stack_name%/config.ru.tt +10 -0
  35. data/lib/pancake/generators/templates/short/%stack_name%/lib/%stack_name%/mounts/.empty_directory +0 -0
  36. data/lib/pancake/generators/templates/short/%stack_name%/lib/%stack_name%/public/.empty_directory +0 -0
  37. data/lib/pancake/generators/templates/short/%stack_name%/lib/%stack_name%/tmp/.empty_directory +0 -0
  38. data/lib/pancake/generators/templates/short/%stack_name%/lib/%stack_name%/views/root.html.haml +2 -0
  39. data/lib/pancake/generators/templates/short/%stack_name%/lib/%stack_name%.rb.tt +5 -0
  40. data/lib/pancake/generators/templates/short/%stack_name%/pancake.init.tt +1 -0
  41. data/lib/pancake/generators/templates/short/%stack_name%/spec/%stack_name%_spec.rb.tt +7 -0
  42. data/lib/pancake/generators/templates/short/%stack_name%/spec/spec_helper.rb.tt +9 -0
  43. data/lib/pancake/generators/templates/stack/%stack_name%/LICENSE.tt +20 -0
  44. data/lib/pancake/generators/templates/stack/%stack_name%/README.tt +7 -0
  45. data/lib/pancake/generators/templates/stack/%stack_name%/Rakefile.tt +50 -0
  46. data/lib/pancake/generators/templates/stack/%stack_name%/VERSION.tt +1 -0
  47. data/lib/pancake/generators/templates/stack/%stack_name%/lib/%stack_name%/config/environments/development.rb.tt +18 -0
  48. data/lib/pancake/generators/templates/stack/%stack_name%/lib/%stack_name%/config/environments/production.rb.tt +18 -0
  49. data/lib/pancake/generators/templates/stack/%stack_name%/lib/%stack_name%/config/router.rb.tt +6 -0
  50. data/lib/pancake/generators/templates/stack/%stack_name%/lib/%stack_name%/config.ru.tt +12 -0
  51. data/lib/pancake/generators/templates/stack/%stack_name%/lib/%stack_name%/gems/cache/.empty_directory +0 -0
  52. data/lib/pancake/generators/templates/stack/%stack_name%/lib/%stack_name%/mounts/.empty_directory +0 -0
  53. data/lib/pancake/generators/templates/stack/%stack_name%/lib/%stack_name%/public/.empty_directory +0 -0
  54. data/lib/pancake/generators/templates/stack/%stack_name%/lib/%stack_name%/tmp/.empty_directory +0 -0
  55. data/lib/pancake/generators/templates/stack/%stack_name%/lib/%stack_name%.rb.tt +3 -0
  56. data/lib/pancake/generators/templates/stack/%stack_name%/pancake.init.tt +1 -0
  57. data/lib/pancake/generators/templates/stack/%stack_name%/spec/%stack_name%_spec.rb.tt +7 -0
  58. data/lib/pancake/generators/templates/stack/%stack_name%/spec/spec_helper.rb.tt +9 -0
  59. data/lib/pancake/generators.rb +8 -0
  60. data/lib/pancake/hooks/inheritable_inner_classes.rb +60 -0
  61. data/lib/pancake/hooks/on_inherit.rb +34 -0
  62. data/lib/pancake/logger.rb +200 -0
  63. data/lib/pancake/master.rb +123 -0
  64. data/lib/pancake/middleware.rb +347 -0
  65. data/lib/pancake/middlewares/logger.rb +16 -0
  66. data/lib/pancake/middlewares/static.rb +38 -0
  67. data/lib/pancake/mime_types.rb +265 -0
  68. data/lib/pancake/mixins/publish/action_options.rb +104 -0
  69. data/lib/pancake/mixins/publish.rb +125 -0
  70. data/lib/pancake/mixins/render/render.rb +168 -0
  71. data/lib/pancake/mixins/render/template.rb +23 -0
  72. data/lib/pancake/mixins/render/view_context.rb +21 -0
  73. data/lib/pancake/mixins/render.rb +109 -0
  74. data/lib/pancake/mixins/request_helper.rb +100 -0
  75. data/lib/pancake/mixins/stack_helper.rb +46 -0
  76. data/lib/pancake/mixins/url.rb +10 -0
  77. data/lib/pancake/paths.rb +218 -0
  78. data/lib/pancake/router.rb +99 -0
  79. data/lib/pancake/stack/app.rb +10 -0
  80. data/lib/pancake/stack/bootloader.rb +79 -0
  81. data/lib/pancake/stack/configuration.rb +44 -0
  82. data/lib/pancake/stack/middleware.rb +0 -0
  83. data/lib/pancake/stack/router.rb +21 -0
  84. data/lib/pancake/stack/stack.rb +66 -0
  85. data/lib/pancake/stacks/short/bootloaders.rb +13 -0
  86. data/lib/pancake/stacks/short/controller.rb +116 -0
  87. data/lib/pancake/stacks/short/default/views/base.html.haml +5 -0
  88. data/lib/pancake/stacks/short/stack.rb +187 -0
  89. data/lib/pancake/stacks/short.rb +3 -0
  90. data/lib/pancake.rb +58 -0
  91. data/spec/helpers/helpers.rb +20 -0
  92. data/spec/helpers/matchers.rb +25 -0
  93. data/spec/pancake/bootloaders_spec.rb +109 -0
  94. data/spec/pancake/configuration_spec.rb +177 -0
  95. data/spec/pancake/constants_spec.rb +7 -0
  96. data/spec/pancake/defaults/configuration_spec.rb +58 -0
  97. data/spec/pancake/fixtures/foo_stack/pancake.init +0 -0
  98. data/spec/pancake/fixtures/middlewares/other_public/two.html +1 -0
  99. data/spec/pancake/fixtures/middlewares/public/foo#bar.html +1 -0
  100. data/spec/pancake/fixtures/middlewares/public/one.html +1 -0
  101. data/spec/pancake/fixtures/paths/controllers/controller1.rb +0 -0
  102. data/spec/pancake/fixtures/paths/controllers/controller2.rb +0 -0
  103. data/spec/pancake/fixtures/paths/controllers/controller3.rb +0 -0
  104. data/spec/pancake/fixtures/paths/models/model1.rb +0 -0
  105. data/spec/pancake/fixtures/paths/models/model2.rb +0 -0
  106. data/spec/pancake/fixtures/paths/models/model3.rb +0 -0
  107. data/spec/pancake/fixtures/paths/stack/controllers/controller1.rb +0 -0
  108. data/spec/pancake/fixtures/paths/stack/models/model3.rb +0 -0
  109. data/spec/pancake/fixtures/paths/stack/views/view1.erb +0 -0
  110. data/spec/pancake/fixtures/paths/stack/views/view1.rb +0 -0
  111. data/spec/pancake/fixtures/paths/stack/views/view2.erb +0 -0
  112. data/spec/pancake/fixtures/paths/stack/views/view2.haml +0 -0
  113. data/spec/pancake/fixtures/render_templates/context_template.html.erb +2 -0
  114. data/spec/pancake/fixtures/render_templates/erb_template.html.erb +1 -0
  115. data/spec/pancake/fixtures/render_templates/erb_template.json.erb +1 -0
  116. data/spec/pancake/fixtures/render_templates/haml_template.html.haml +1 -0
  117. data/spec/pancake/fixtures/render_templates/haml_template.xml.haml +1 -0
  118. data/spec/pancake/fixtures/render_templates/templates/context.erb +2 -0
  119. data/spec/pancake/fixtures/render_templates/view_context/capture_erb.erb +5 -0
  120. data/spec/pancake/fixtures/render_templates/view_context/capture_haml.haml +4 -0
  121. data/spec/pancake/fixtures/render_templates/view_context/concat_erb.erb +2 -0
  122. data/spec/pancake/fixtures/render_templates/view_context/concat_haml.haml +2 -0
  123. data/spec/pancake/fixtures/render_templates/view_context/context.erb +3 -0
  124. data/spec/pancake/fixtures/render_templates/view_context/context2.erb +3 -0
  125. data/spec/pancake/fixtures/render_templates/view_context/helper_methods.erb +3 -0
  126. data/spec/pancake/fixtures/render_templates/view_context/inherited_erb_from_haml.erb +5 -0
  127. data/spec/pancake/fixtures/render_templates/view_context/inherited_erb_level_0.erb +5 -0
  128. data/spec/pancake/fixtures/render_templates/view_context/inherited_erb_level_1.erb +5 -0
  129. data/spec/pancake/fixtures/render_templates/view_context/inherited_haml_from_erb.haml +4 -0
  130. data/spec/pancake/fixtures/render_templates/view_context/inherited_haml_level_0.haml +4 -0
  131. data/spec/pancake/fixtures/render_templates/view_context/inherited_haml_level_1.haml +4 -0
  132. data/spec/pancake/fixtures/render_templates/view_context/nested_content_level_0.haml +6 -0
  133. data/spec/pancake/fixtures/render_templates/view_context/nested_content_level_1.haml +4 -0
  134. data/spec/pancake/fixtures/render_templates/view_context/nested_inner.erb +1 -0
  135. data/spec/pancake/fixtures/render_templates/view_context/nested_outer.erb +3 -0
  136. data/spec/pancake/fixtures/render_templates/view_context/super_erb_from_erb_0.erb +5 -0
  137. data/spec/pancake/fixtures/render_templates/view_context/super_erb_from_erb_1.erb +6 -0
  138. data/spec/pancake/fixtures/render_templates/view_context/super_erb_from_haml_0.erb +5 -0
  139. data/spec/pancake/fixtures/render_templates/view_context/super_erb_from_haml_1.erb +6 -0
  140. data/spec/pancake/fixtures/render_templates/view_context/super_haml_from_erb_0.haml +4 -0
  141. data/spec/pancake/fixtures/render_templates/view_context/super_haml_from_erb_1.haml +5 -0
  142. data/spec/pancake/fixtures/render_templates/view_context/super_haml_from_haml_0.haml +5 -0
  143. data/spec/pancake/fixtures/render_templates/view_context/super_haml_from_haml_1.haml +5 -0
  144. data/spec/pancake/fixtures/stacks/short/foobar/other_root/views/base.html.haml +4 -0
  145. data/spec/pancake/fixtures/stacks/short/foobar/views/basic.html.haml +1 -0
  146. data/spec/pancake/fixtures/stacks/short/foobar/views/inherited_from_base.html.haml +5 -0
  147. data/spec/pancake/hooks/on_inherit_spec.rb +65 -0
  148. data/spec/pancake/inheritance_spec.rb +100 -0
  149. data/spec/pancake/middleware_spec.rb +401 -0
  150. data/spec/pancake/middlewares/logger_spec.rb +29 -0
  151. data/spec/pancake/middlewares/static_spec.rb +83 -0
  152. data/spec/pancake/mime_types_spec.rb +234 -0
  153. data/spec/pancake/mixins/publish_spec.rb +94 -0
  154. data/spec/pancake/mixins/render/template_spec.rb +69 -0
  155. data/spec/pancake/mixins/render/view_context_spec.rb +248 -0
  156. data/spec/pancake/mixins/render_spec.rb +56 -0
  157. data/spec/pancake/mixins/request_helper_spec.rb +27 -0
  158. data/spec/pancake/mixins/stack_helper_spec.rb +46 -0
  159. data/spec/pancake/pancake_spec.rb +90 -0
  160. data/spec/pancake/paths_spec.rb +210 -0
  161. data/spec/pancake/stack/app_spec.rb +28 -0
  162. data/spec/pancake/stack/bootloader_spec.rb +41 -0
  163. data/spec/pancake/stack/middleware_spec.rb +0 -0
  164. data/spec/pancake/stack/router_spec.rb +282 -0
  165. data/spec/pancake/stack/stack_configuration_spec.rb +101 -0
  166. data/spec/pancake/stack/stack_spec.rb +60 -0
  167. data/spec/pancake/stacks/short/controller_spec.rb +322 -0
  168. data/spec/pancake/stacks/short/middlewares_spec.rb +22 -0
  169. data/spec/pancake/stacks/short/router_spec.rb +136 -0
  170. data/spec/pancake/stacks/short/stack_spec.rb +64 -0
  171. data/spec/spec_helper.rb +23 -0
  172. metadata +294 -0
@@ -0,0 +1,100 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe "Pancake Inheritance" do
4
+ describe "on inherit hook" do
5
+
6
+ before(:each) do
7
+ clear_constants(:MyFoo, :OtherFoo, :FutherFoo, :SomeFoo, :DeeperFoo)
8
+ class ::MyFoo
9
+ extend Pancake::Hooks::OnInherit
10
+ end
11
+
12
+ ::MyFoo.on_inherit do |base, parent|
13
+ $inherited_capture << {:base => base, :parent => parent}
14
+ end
15
+
16
+ $inherited_capture = []
17
+ end
18
+
19
+ it "should provide on_inherit callbacks" do
20
+ class ::OtherFoo < MyFoo; end
21
+ $inherited_capture.should == [{:base => OtherFoo, :parent => MyFoo}]
22
+ end
23
+
24
+ it "should inherit multiple times" do
25
+ class ::OtherFoo < MyFoo; end
26
+ class ::FurtherFoo < OtherFoo; end
27
+ $inherited_capture.should == [{:base => OtherFoo, :parent => MyFoo}, {:base => FurtherFoo, :parent => OtherFoo}]
28
+ end
29
+
30
+ it "should allow for multiple callbacks" do
31
+ MyFoo.on_inherit{|base, parent| $inherited_capture << :second}
32
+ class ::OtherFoo < MyFoo; end
33
+ $inherited_capture.should == [{:base => OtherFoo, :parent => MyFoo}, :second]
34
+ end
35
+
36
+ it "should allow for multiple callbacks that are inherited" do
37
+ MyFoo.on_inherit{|base, parent| $inherited_capture << :second}
38
+ class ::OtherFoo < MyFoo; end
39
+ $inherited_capture = []
40
+ OtherFoo.on_inherit{|base, parent| $inherited_capture << :inherited}
41
+ class ::SomeFoo < OtherFoo; end
42
+ $inherited_capture.should == [{:base => SomeFoo, :parent => OtherFoo}, :second, :inherited]
43
+ end
44
+
45
+ it "should not pollute the parent with the child inherited hooks" do
46
+ class ::OtherFoo < MyFoo; end
47
+ $inherited_capture = []
48
+ OtherFoo.on_inherit{|b,p| $inherited_captuer << :should_not_be_in_there}
49
+ class ::SomeFoo < MyFoo; end
50
+ $inherited_capture.should == [{:base => SomeFoo, :parent => MyFoo}]
51
+ end
52
+ end # "on inherit hook"
53
+
54
+ describe "inheritable inner classes" do
55
+
56
+ before(:each) do
57
+ clear_constants(:MyFoo, :OtherFoo, :InnerFoo, :SomeFoo, :DeeperFoo)
58
+
59
+ class ::MyFoo
60
+ inheritable_inner_classes :InnerFoo
61
+ class InnerFoo; end
62
+ end
63
+ end
64
+
65
+ it "should inherit the inner class along with the outer class" do
66
+ class ::OtherFoo < MyFoo; end
67
+ OtherFoo::InnerFoo.superclass.should equal(MyFoo::InnerFoo)
68
+ OtherFoo::InnerFoo.should_not equal(MyFoo::InnerFoo)
69
+ end
70
+
71
+ it "should inherit the inner class multiple times" do
72
+ class ::OtherFoo < MyFoo; end
73
+ class ::SomeFoo < OtherFoo; end
74
+ class ::DeeperFoo < SomeFoo; end
75
+
76
+ SomeFoo::InnerFoo.superclass.should equal(OtherFoo::InnerFoo)
77
+ SomeFoo::InnerFoo.should_not equal(OtherFoo::InnerFoo)
78
+ DeeperFoo::InnerFoo.superclass.should equal(SomeFoo::InnerFoo)
79
+ DeeperFoo::InnerFoo.should_not equal(OtherFoo::InnerFoo)
80
+ DeeperFoo::InnerFoo.superclass.should equal(SomeFoo::InnerFoo)
81
+ end
82
+
83
+ it "should allow additional inner classes to be declared without polluting the parent" do
84
+ class ::OtherFoo < MyFoo
85
+ inheritable_inner_classes :SomeDeepFoo
86
+ class SomeDeepFoo; end
87
+ end
88
+
89
+ class ::SomeFoo < MyFoo; end
90
+ class ::DeeperFoo < ::OtherFoo; end
91
+
92
+ SomeFoo::InnerFoo.should inherit_from(MyFoo::InnerFoo)
93
+ SomeFoo.const_defined?(:SomeDeepFoo).should be_false
94
+
95
+ DeeperFoo::InnerFoo.should inherit_from(OtherFoo::InnerFoo)
96
+ DeeperFoo::SomeDeepFoo.should inherit_from(OtherFoo::SomeDeepFoo)
97
+ end
98
+
99
+ end # "inheritable inner classes"
100
+ end
@@ -0,0 +1,401 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe "Pancake::Middleware" do
4
+ before(:all) do
5
+ $pk_mid = Pancake.middlewares.dup
6
+ end
7
+
8
+ after(:all) do
9
+ Pancake.middlewares.replace $pk_mid
10
+ end
11
+
12
+ before(:each) do
13
+ Pancake.middlewares.clear
14
+ def app
15
+ @app
16
+ end
17
+
18
+ def default_env
19
+ Rack::MockRequest.env_for
20
+ end
21
+
22
+ $current_env = {}
23
+
24
+ class ::GeneralMiddleware
25
+ attr_accessor :app
26
+ def initialize(app, opts={});@app = app; end
27
+
28
+ def mark_env(env)
29
+ env["p.s.c"] ||= []
30
+ env["p.s.c"] << self.class
31
+ end
32
+
33
+ def call(env)
34
+ mark_env(env)
35
+ @app.call(env)
36
+ end
37
+ end # GeneralMiddlware
38
+
39
+ class ::FooApp < Pancake::Stack
40
+ def self.new_app_instance; self end
41
+
42
+ def self.call(env)
43
+ $current_env = env
44
+ [200,{"Content-Type" => "text/plain"}, [name]]
45
+ end
46
+ end # FooApp
47
+ FooApp.roots << Pancake.get_root("/tmp")
48
+ end
49
+
50
+ after(:each) do
51
+ clear_constants(:FooApp, :BarApp, :BazApp, :GeneralMiddleware, :BarMiddle, :FooMiddle, :BazMiddle, :PazMiddle)
52
+ end
53
+
54
+ describe "pancake middlewares" do
55
+ before(:each) do
56
+ @root = File.join(Pancake.get_root(__FILE__), "fixtures", "foo_stack")
57
+ @the_app = Proc.new{|e| }
58
+ Pancake::StackMiddleware.reset!
59
+ end
60
+
61
+ it "should allow me to add middleware to pancake" do
62
+ Pancake.use GeneralMiddleware
63
+ @app = Pancake.start(:root => @root){ FooApp.stackup }
64
+ Pancake.middlewares.map{|m| m.middleware}.should == [GeneralMiddleware]
65
+ get "/"
66
+ $current_env["p.s.c"].should include(GeneralMiddleware)
67
+ end
68
+
69
+ it "should allow me to add multiple middlewares to panckae" do
70
+ class ::FooMiddle < GeneralMiddleware; end
71
+ Pancake.use GeneralMiddleware
72
+ FooApp.use FooMiddle
73
+ @app = Pancake.start(:root => @root){ FooApp.stackup }
74
+ get "/"
75
+ $current_env["p.s.c"].should == [GeneralMiddleware, FooMiddle]
76
+ end
77
+
78
+ it "should put the pancake middlewares out in front" do
79
+ class ::FooMiddle < GeneralMiddleware; end
80
+ class ::BarMiddle < GeneralMiddleware; end
81
+ class ::BarApp < FooApp; end
82
+ class ::BazApp < FooApp; end
83
+
84
+ BarApp.use BarMiddle
85
+ BazApp.use FooMiddle
86
+ Pancake.use GeneralMiddleware
87
+
88
+ FooApp.with_router do |r|
89
+ r.mount(BarApp.stackup, "/bar")
90
+ r.mount(BazApp.stackup, "/baz")
91
+ end
92
+
93
+ @app = Pancake.start(:root => @root){ FooApp.stackup }
94
+ get "/baz"
95
+ $current_env["p.s.c"].should == [GeneralMiddleware, FooMiddle]
96
+ get "/bar"
97
+ $current_env["p.s.c"].should == [GeneralMiddleware, BarMiddle]
98
+ end
99
+ end
100
+
101
+ it "should allow me to add middleware" do
102
+ FooApp.class_eval do
103
+ use GeneralMiddleware
104
+ end
105
+ @app = FooApp.stackup
106
+ get "/"
107
+ $current_env["p.s.c"].should include(GeneralMiddleware)
108
+ end
109
+
110
+ it "should allow me to add multiple middlewares" do
111
+ class ::FooMiddle < GeneralMiddleware; end
112
+ FooApp.class_eval do
113
+ use GeneralMiddleware
114
+ use FooMiddle
115
+ end
116
+ @app = FooApp.stackup
117
+ get "/"
118
+ [GeneralMiddleware, FooMiddle].each do |m|
119
+ $current_env["p.s.c"].should include(m)
120
+ end
121
+ end
122
+
123
+ it "should allow you to add middleware from outside the class" do
124
+ FooApp.use GeneralMiddleware
125
+ @app = FooApp.stackup
126
+ get "/"
127
+ $current_env["p.s.c"].should == [GeneralMiddleware]
128
+ end
129
+
130
+ describe "replace middleware" do
131
+ before(:each) do
132
+ FooApp.stack(:replaceable).use(GeneralMiddleware, :some => :option){ :original }
133
+ class FooMiddle < GeneralMiddleware; end
134
+ end
135
+
136
+ it "should replace the middleware with another middleware" do
137
+ orig = FooApp::StackMiddleware[:replaceable]
138
+ orig.middleware.should == GeneralMiddleware
139
+ orig.args.should == [{:some => :option}]
140
+
141
+ FooApp.stack(:replaceable).use(FooMiddle, :foo => :options)
142
+ replaced = FooApp.stack(:replaceable)
143
+ replaced.middleware.should == FooMiddle
144
+ replaced.args.should == [{:foo => :options}]
145
+ end
146
+ end
147
+
148
+ describe "deleteing middleware" do
149
+ before(:each) do
150
+ FooApp.stack(:deleteable).use(GeneralMiddleware, :some => :option){ :original }
151
+ end
152
+
153
+ it "should delete the middleware" do
154
+ orig = FooApp::StackMiddleware[:deleteable]
155
+ orig.should_not be_nil
156
+ orig.middleware.should == GeneralMiddleware
157
+ FooApp.stack(:deleteable).delete!
158
+ FooApp::StackMiddleware[:deleteable].should be_nil
159
+ end
160
+
161
+ it "should not include middleware that depends on being deleted" do
162
+ class FooMiddle < GeneralMiddleware; end
163
+ FooApp.stack(:foo, :after => :deleteable).use(FooMiddle)
164
+ FooApp.middlewares.map{|m| m.middleware}.should == [GeneralMiddleware, FooMiddle]
165
+ FooApp.stack(:deleteable).delete!
166
+ FooApp.middlewares.map{|m| m.middleware}.should == []
167
+ end
168
+ end
169
+
170
+ describe "edit middleware" do
171
+ before(:each) do
172
+ FooApp.stack(:editable).use(GeneralMiddleware,:some => :config)
173
+ end
174
+
175
+ it "should allow me to update settings for middleware" do
176
+ FooApp.stack(:editable).args.should == [{:some => :config}]
177
+ FooApp.stack(:editable).args = [{:foo => :bar}]
178
+ FooApp.stack(:editable).args.should == [{:foo => :bar}]
179
+ end
180
+ end
181
+
182
+
183
+
184
+ describe "Inherited middleware" do
185
+ before(:each) do
186
+ class FooMiddle < GeneralMiddleware; end
187
+ class BarMiddle < GeneralMiddleware; end
188
+ end
189
+
190
+ it "should inherit middleware from it's parent class" do
191
+ FooApp.use GeneralMiddleware
192
+ FooApp.use FooMiddle
193
+ class BarApp < FooApp; end
194
+ BarApp.middlewares.should == FooApp.middlewares
195
+ end
196
+
197
+ it "should not pollute the parent when including new middlewares in the child" do
198
+ FooApp.use GeneralMiddleware
199
+ class BarApp < FooApp; end
200
+ BarApp.use FooMiddle
201
+ BarApp.middlewares.map{|m| m.middleware}.should == [GeneralMiddleware, FooMiddle]
202
+ FooApp.middlewares.map{|m| m.middleware}.should == [GeneralMiddleware]
203
+ end
204
+
205
+ describe "editing inherited middlware" do
206
+ it "should not edit the parent when editing the child" do
207
+ FooApp.use GeneralMiddleware, :some => :option
208
+ class BarApp < FooApp; end
209
+ BarApp.stack(GeneralMiddleware).middleware.should == GeneralMiddleware
210
+ BarApp.stack(GeneralMiddleware).args = [{:foo => :bar}]
211
+ BarApp.stack(GeneralMiddleware).args.should == [{:foo => :bar}]
212
+ FooApp.stack(GeneralMiddleware).args.should == [{:some => :option}]
213
+ end
214
+
215
+ it "should not update the parent when updating a childs config" do
216
+ FooApp.use GeneralMiddleware, :some => :option
217
+ class BarApp < FooApp; end
218
+ BarApp.stack(GeneralMiddleware).args[0][:another] = :option
219
+ FooApp.stack(GeneralMiddleware).args.should == [{:some => :option}]
220
+ BarApp.stack(GeneralMiddleware).args.should == [{:some => :option, :another => :option}]
221
+ end
222
+ end
223
+ end
224
+
225
+ describe "Stacks should inherit middleware" do
226
+ before(:all) do
227
+ $pk_middlewares = Pancake::Stack.middlewares.dup
228
+ end
229
+
230
+ after(:all) do
231
+ Pancake::Stack.middlewares.replace $pk_middlewares
232
+ end
233
+
234
+ before(:each) do
235
+ class FooMiddle < GeneralMiddleware; end
236
+ class BarMiddle < GeneralMiddleware; end
237
+ end
238
+
239
+ after(:each) do
240
+ Pancake::Stack::StackMiddleware.reset!
241
+ end
242
+
243
+ it "should clear the middlewares for the specs" do
244
+ Pancake::Stack.middlewares.should be_blank
245
+ end
246
+
247
+ it "should allow me to set middlewares on Pancake::Stack" do
248
+ Pancake::Stack.use GeneralMiddleware
249
+ Pancake::Stack.middlewares.map{|m| m.middleware}.should == [GeneralMiddleware]
250
+ end
251
+
252
+ it "should carry down middlewares from the Pancake::Stack to inherited stacks" do
253
+ Pancake::Stack.use GeneralMiddleware
254
+ class FooApp < Pancake::Stack; end
255
+ FooApp.use FooMiddle
256
+ Pancake::Stack.middlewares.map{|m| m.middleware}.should == [GeneralMiddleware]
257
+ FooApp.middlewares.map{|m| m.middleware}.should == [GeneralMiddleware, FooMiddle]
258
+ end
259
+
260
+ it "should let me have different middlewares in different children" do
261
+ Pancake::Stack.use GeneralMiddleware
262
+ class FooApp < Pancake::Stack; end
263
+ FooApp.use FooMiddle
264
+ class BarApp < Pancake::Stack; end
265
+ BarApp.use BarMiddle
266
+ FooApp.middlewares.map{|m| m.middleware}.should == [GeneralMiddleware, FooMiddle]
267
+ BarApp.middlewares.map{|m| m.middleware}.should == [GeneralMiddleware, BarMiddle]
268
+ end
269
+
270
+ describe "Stack Middleware Enabled Constant", :shared => true do
271
+
272
+ before(:each) do
273
+ raise "You must set a @konstant for the stack construction spec" unless @konstant
274
+ @konstant::StackMiddleware.reset!
275
+ end
276
+
277
+ describe "named middleware" do
278
+ it "should allow me to name a #{@konstant} middleware" do
279
+ @konstant.stack(:foo).use(GeneralMiddleware)
280
+ @konstant.stack[:foo].middleware.should == GeneralMiddleware
281
+ end
282
+
283
+ it "should implicitly name a middleware" do
284
+ @konstant.stack.use(GeneralMiddleware)
285
+ @konstant.stack[GeneralMiddleware].middleware.should == GeneralMiddleware
286
+ end
287
+ end
288
+
289
+ describe "before/after middleware" do
290
+ before(:each) do
291
+ class ::BazMiddle < GeneralMiddleware; end
292
+ class ::PazMiddle < GeneralMiddleware; end
293
+ end
294
+ # :FooApp, :BarApp, :BazApp, :GeneralMiddleware, :BarMiddle, :FooMiddle, :BazMiddle, :PazMiddle
295
+ it "should allow me to add middleware before other middleware" do
296
+ @konstant.use(GeneralMiddleware)
297
+ @konstant.stack(:bar).use(BarMiddle)
298
+ @konstant.stack(:foo, :before => :bar).use(FooMiddle)
299
+ result = @konstant.middlewares.map{|m| m.middleware}
300
+ result.should == [GeneralMiddleware, FooMiddle, BarMiddle]
301
+ end
302
+
303
+ it "should allow me to add middleware after other middleware " do
304
+ @konstant.use(BarMiddle)
305
+ @konstant.stack(:general).use(GeneralMiddleware)
306
+ @konstant.stack(:foo, :after => BarMiddle).use(FooMiddle)
307
+ result = @konstant.middlewares.map{|m| m.middleware}
308
+ result.should == [ BarMiddle, FooMiddle, GeneralMiddleware]
309
+ end
310
+
311
+ it "should allow me to add middleware arbitrarily and have it in the correct order" do
312
+ @konstant.use(GeneralMiddleware)
313
+ @konstant.stack(:bar).use(BarMiddle)
314
+ @konstant.stack(:foo, :before => :bar).use(FooMiddle)
315
+ @konstant.stack(:baz, :after => :foo).use(BazMiddle)
316
+ @konstant.stack(:paz, :before => GeneralMiddleware).use(PazMiddle)
317
+ result = @konstant.middlewares.map{|m| m.middleware}
318
+ result.should == [PazMiddle, GeneralMiddleware, FooMiddle, BazMiddle, BarMiddle]
319
+ end
320
+ end
321
+ end
322
+
323
+ describe "Pancake Middleware Construction" do
324
+ before(:each) do
325
+ @konstant = Pancake
326
+ end
327
+
328
+ it_should_behave_like("Stack Middleware Enabled Constant")
329
+ end
330
+
331
+ describe "Pancake Stack Middleware" do
332
+ before(:each) do
333
+ @konstant = Pancake::Stack
334
+ end
335
+ it_should_behave_like("Stack Middleware Enabled Constant")
336
+ end
337
+
338
+ describe "An inherited panckae stack app" do
339
+ before(:each) do
340
+ class ::FooApp < Pancake::Stack; end
341
+ @konstant = FooApp
342
+ end
343
+ it_should_behave_like("Stack Middleware Enabled Constant")
344
+ end
345
+
346
+ describe "a deeply inherited stack app" do
347
+ before(:each) do
348
+ class ::FooApp < Pancake::Stack; end
349
+ class ::BarApp < FooApp; end
350
+ @konstant = BarApp
351
+ end
352
+ it_should_behave_like("Stack Middleware Enabled Constant")
353
+ end
354
+
355
+ it "should allow me to inherit middleware from a parent stack" do
356
+ class ::FooApp < Pancake::Stack; end
357
+ FooApp.use(GeneralMiddleware)
358
+ class ::BarApp < FooApp; end
359
+ BarApp.stack(:foo).use(FooMiddle)
360
+ BarApp.stack(:bar, :after => GeneralMiddleware).use(BarMiddle)
361
+ result = BarApp.middlewares.map{|m|m.middleware}
362
+ result.should == [GeneralMiddleware, BarMiddle, FooMiddle]
363
+ end
364
+
365
+ end # Stack Inheritance
366
+
367
+ describe "types of stacks" do
368
+ before(:each) do
369
+ class FooMiddle < GeneralMiddleware; end
370
+ class BarMiddle < GeneralMiddleware; end
371
+ class BazMiddle < GeneralMiddleware; end
372
+
373
+ FooApp.stack(:general, :labels => [:production] ).use(GeneralMiddleware)
374
+ FooApp.stack(:foo, :labels => [:production, :demo] ).use(FooMiddle)
375
+ FooApp.stack(:bar, :labels => [:test] ).use(BarMiddle)
376
+ FooApp.stack(:baz, :labels => [:any] ).use(BazMiddle)
377
+ end
378
+
379
+ it "should differentiate between stack types" do
380
+ FooApp.middlewares(:production).map{|m| m.middleware}.should == [GeneralMiddleware, FooMiddle, BazMiddle]
381
+ FooApp.middlewares(:demo).map{|m| m.middleware}.should == [FooMiddle, BazMiddle]
382
+ FooApp.middlewares(:test).map{|m| m.middleware}.should == [BarMiddle, BazMiddle]
383
+ FooApp.middlewares(:demo, :test).map{|m| m.middleware}.should == [FooMiddle, BarMiddle,BazMiddle]
384
+ end
385
+
386
+ it "should add a middleware that is not declared with any particular label to all stacks" do
387
+ class PazMiddle < GeneralMiddleware; end
388
+ FooApp.stack(:paz).use(PazMiddle)
389
+ FooApp.middlewares(:test).map{|m| m.middleware}.should == [BarMiddle, BazMiddle, PazMiddle]
390
+ FooApp.middlewares(:demo).map{|m| m.middleware}.should == [FooMiddle, BazMiddle, PazMiddle]
391
+ FooApp.middlewares(:production).map{|m| m.middleware}.should == [GeneralMiddleware, FooMiddle, BazMiddle, PazMiddle]
392
+ end
393
+
394
+ it "should not use a middleware if it is dependent on middleware that is not in the correct stack" do
395
+ class PazMiddle < GeneralMiddleware; end
396
+ FooApp.stack(:paz, :after => :bar).use(PazMiddle)
397
+ FooApp.middlewares(:production).map{|m| m.middleware}.should == [GeneralMiddleware, FooMiddle, BazMiddle]
398
+ FooApp.middlewares(:test).map{|m| m.middleware}.should == [BarMiddle, PazMiddle, BazMiddle]
399
+ end
400
+ end
401
+ end
@@ -0,0 +1,29 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe Pancake::Middlewares::Logger do
4
+ before do
5
+ Pancake.stack(:logger).use(Pancake::Middlewares::Logger)
6
+ class ::PancakeSpecLogger
7
+ def self.call(env)
8
+ Rack::Response.new("OK").finish
9
+ end
10
+ end
11
+ end
12
+
13
+ after do
14
+ clear_constants :PancakeSpecLogger
15
+ FileUtils.rm_rf(File.join(Pancake.get_root(__FILE__), "log"))
16
+ end
17
+
18
+ def app
19
+ Pancake.start(:root => Pancake.get_root(__FILE__)){ PancakeSpecLogger }
20
+ end
21
+
22
+ it "should inject a logger into the request env" do
23
+ the_app = app
24
+ env = Rack::MockRequest.env_for("/")
25
+ env['rack.logger'].should be_nil
26
+ the_app.call(env)
27
+ env['rack.logger'].class.should == Pancake::Logger
28
+ end
29
+ end
@@ -0,0 +1,83 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe Pancake::Middlewares::Static do
4
+ before do
5
+ @app = lambda{|e| Rack::Response.new("OK").finish}
6
+ class ::FooBar < Pancake::Stack; end
7
+
8
+ FooBar.roots << File.join(File.expand_path(File.dirname(__FILE__)), "../fixtures/middlewares")
9
+ FooBar.push_paths(:public, ["public", "other_public"])
10
+ end
11
+
12
+ after do
13
+ clear_constants :FooBar
14
+ end
15
+
16
+ it "should require a stack to be passed to initialize" do
17
+ static = Pancake::Middlewares::Static.new(@app, FooBar)
18
+ static.stack.should == FooBar
19
+ end
20
+
21
+ it "should raise an error if not initialized with an object that includes Pancake::Paths" do
22
+ lambda do
23
+ Pancake::Middlewares::Static.new(@app, Object)
24
+ end.should raise_error
25
+ end
26
+
27
+ it "should return the file if one is found in the first root" do
28
+ static = Pancake::Middlewares::Static.new(@app, FooBar)
29
+ env = Rack::MockRequest.env_for("/one.html")
30
+ result = static.call(env)
31
+ result[0].should == 200
32
+ body = result[2].body.map{|e| e}.join
33
+ body.should include("In One")
34
+ end
35
+
36
+ it "should return the file if one is found in any of the roots" do
37
+ static = Pancake::Middlewares::Static.new(@app, FooBar)
38
+ env = Rack::MockRequest.env_for("/two.html")
39
+ result = static.call(env)
40
+ result[0].should == 200
41
+ body = result[2].body.map{|e| e}.join
42
+ body.should include("In Two")
43
+ end
44
+
45
+ it "should pass through to the application if there is no file found" do
46
+ static = Pancake::Middlewares::Static.new(@app, FooBar)
47
+ env = Rack::MockRequest.env_for("/not_here.html")
48
+ result = static.call(env)
49
+ result[0].should == 200
50
+ body = result[2].body.map{|e| e}.join
51
+ body.should == "OK"
52
+ end
53
+
54
+ it "should return a 404 if the file requested is outside the root directory" do
55
+ static = Pancake::Middlewares::Static.new(@app, FooBar)
56
+ file = "/../../../middlewares/static_spec.rb"
57
+ File.exists?(File.join(FooBar.dirs_for(:public).first, file)).should be_true
58
+ env = Rack::MockRequest.env_for(file)
59
+ result = static.call(env)
60
+ result[2].body.map{|e| e}.join.should_not == "OK"
61
+ result[0].should == 404
62
+ end
63
+
64
+ it "should chomp a trailing slash" do
65
+ static = Pancake::Middlewares::Static.new(@app, FooBar)
66
+ file = "/two.html/"
67
+ env = Rack::MockRequest.env_for(file)
68
+ result = static.call(env)
69
+ result[0].should == 200
70
+ body = result[2].body.map{|e| e}.join
71
+ body.should include("In Two")
72
+ end
73
+
74
+ it "should unescape the file name" do
75
+ static = Pancake::Middlewares::Static.new(@app, FooBar)
76
+ file = "/foo%23bar.html"
77
+ env = Rack::MockRequest.env_for(file)
78
+ result = static.call(env)
79
+ result[0].should == 200
80
+ body = result[2].body.map{|e| e}.join
81
+ body.should include("escaped text is #")
82
+ end
83
+ end