merb-core 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (246) hide show
  1. data/LICENSE +20 -0
  2. data/README +21 -0
  3. data/Rakefile +285 -0
  4. data/TODO +0 -0
  5. data/bin/merb +8 -0
  6. data/bin/merb-specs +5 -0
  7. data/docs/bootloading.dox +57 -0
  8. data/docs/documentation_standards +40 -0
  9. data/docs/new_render_api +51 -0
  10. data/lib/merb-core.rb +304 -0
  11. data/lib/merb-core/autoload.rb +29 -0
  12. data/lib/merb-core/bootloader.rb +601 -0
  13. data/lib/merb-core/config.rb +284 -0
  14. data/lib/merb-core/constants.rb +43 -0
  15. data/lib/merb-core/controller/abstract_controller.rb +531 -0
  16. data/lib/merb-core/controller/exceptions.rb +257 -0
  17. data/lib/merb-core/controller/merb_controller.rb +214 -0
  18. data/lib/merb-core/controller/mime.rb +88 -0
  19. data/lib/merb-core/controller/mixins/controller.rb +262 -0
  20. data/lib/merb-core/controller/mixins/render.rb +324 -0
  21. data/lib/merb-core/controller/mixins/responder.rb +464 -0
  22. data/lib/merb-core/controller/template.rb +205 -0
  23. data/lib/merb-core/core_ext.rb +12 -0
  24. data/lib/merb-core/core_ext/class.rb +192 -0
  25. data/lib/merb-core/core_ext/hash.rb +422 -0
  26. data/lib/merb-core/core_ext/kernel.rb +304 -0
  27. data/lib/merb-core/core_ext/mash.rb +154 -0
  28. data/lib/merb-core/core_ext/object.rb +136 -0
  29. data/lib/merb-core/core_ext/object_space.rb +14 -0
  30. data/lib/merb-core/core_ext/rubygems.rb +28 -0
  31. data/lib/merb-core/core_ext/set.rb +41 -0
  32. data/lib/merb-core/core_ext/string.rb +69 -0
  33. data/lib/merb-core/dispatch/cookies.rb +92 -0
  34. data/lib/merb-core/dispatch/dispatcher.rb +233 -0
  35. data/lib/merb-core/dispatch/exceptions.html.erb +297 -0
  36. data/lib/merb-core/dispatch/request.rb +560 -0
  37. data/lib/merb-core/dispatch/router.rb +141 -0
  38. data/lib/merb-core/dispatch/router/behavior.rb +777 -0
  39. data/lib/merb-core/dispatch/router/cached_proc.rb +52 -0
  40. data/lib/merb-core/dispatch/router/route.rb +212 -0
  41. data/lib/merb-core/dispatch/session.rb +28 -0
  42. data/lib/merb-core/dispatch/session/cookie.rb +166 -0
  43. data/lib/merb-core/dispatch/session/memcached.rb +161 -0
  44. data/lib/merb-core/dispatch/session/memory.rb +234 -0
  45. data/lib/merb-core/gem_ext/erubis.rb +19 -0
  46. data/lib/merb-core/logger.rb +230 -0
  47. data/lib/merb-core/plugins.rb +25 -0
  48. data/lib/merb-core/rack.rb +15 -0
  49. data/lib/merb-core/rack/adapter.rb +42 -0
  50. data/lib/merb-core/rack/adapter/ebb.rb +22 -0
  51. data/lib/merb-core/rack/adapter/evented_mongrel.rb +24 -0
  52. data/lib/merb-core/rack/adapter/fcgi.rb +16 -0
  53. data/lib/merb-core/rack/adapter/irb.rb +108 -0
  54. data/lib/merb-core/rack/adapter/mongrel.rb +25 -0
  55. data/lib/merb-core/rack/adapter/runner.rb +27 -0
  56. data/lib/merb-core/rack/adapter/thin.rb +27 -0
  57. data/lib/merb-core/rack/adapter/webrick.rb +35 -0
  58. data/lib/merb-core/rack/application.rb +77 -0
  59. data/lib/merb-core/rack/handler/mongrel.rb +97 -0
  60. data/lib/merb-core/server.rb +184 -0
  61. data/lib/merb-core/test.rb +10 -0
  62. data/lib/merb-core/test/helpers.rb +9 -0
  63. data/lib/merb-core/test/helpers/controller_helper.rb +8 -0
  64. data/lib/merb-core/test/helpers/multipart_request_helper.rb +175 -0
  65. data/lib/merb-core/test/helpers/request_helper.rb +257 -0
  66. data/lib/merb-core/test/helpers/route_helper.rb +33 -0
  67. data/lib/merb-core/test/helpers/view_helper.rb +121 -0
  68. data/lib/merb-core/test/matchers.rb +9 -0
  69. data/lib/merb-core/test/matchers/controller_matchers.rb +269 -0
  70. data/lib/merb-core/test/matchers/route_matchers.rb +136 -0
  71. data/lib/merb-core/test/matchers/view_matchers.rb +293 -0
  72. data/lib/merb-core/test/run_specs.rb +38 -0
  73. data/lib/merb-core/test/tasks/spectasks.rb +39 -0
  74. data/lib/merb-core/test/test_ext/hpricot.rb +32 -0
  75. data/lib/merb-core/test/test_ext/object.rb +14 -0
  76. data/lib/merb-core/vendor/facets.rb +2 -0
  77. data/lib/merb-core/vendor/facets/dictionary.rb +433 -0
  78. data/lib/merb-core/vendor/facets/inflect.rb +211 -0
  79. data/lib/merb-core/version.rb +11 -0
  80. data/spec/private/config/adapter_spec.rb +32 -0
  81. data/spec/private/config/config_spec.rb +139 -0
  82. data/spec/private/config/environment_spec.rb +13 -0
  83. data/spec/private/config/spec_helper.rb +1 -0
  84. data/spec/private/core_ext/hash_spec.rb +506 -0
  85. data/spec/private/core_ext/kernel_spec.rb +46 -0
  86. data/spec/private/core_ext/object_spec.rb +39 -0
  87. data/spec/private/core_ext/set_spec.rb +26 -0
  88. data/spec/private/core_ext/string_spec.rb +9 -0
  89. data/spec/private/dispatch/cookies_spec.rb +107 -0
  90. data/spec/private/dispatch/dispatch_spec.rb +26 -0
  91. data/spec/private/dispatch/fixture/app/controllers/application.rb +4 -0
  92. data/spec/private/dispatch/fixture/app/controllers/exceptions.rb +27 -0
  93. data/spec/private/dispatch/fixture/app/controllers/foo.rb +21 -0
  94. data/spec/private/dispatch/fixture/app/helpers/global_helpers.rb +8 -0
  95. data/spec/private/dispatch/fixture/app/views/exeptions/client_error.html.erb +37 -0
  96. data/spec/private/dispatch/fixture/app/views/exeptions/internal_server_error.html.erb +216 -0
  97. data/spec/private/dispatch/fixture/app/views/exeptions/not_acceptable.html.erb +38 -0
  98. data/spec/private/dispatch/fixture/app/views/exeptions/not_found.html.erb +40 -0
  99. data/spec/private/dispatch/fixture/app/views/foo/bar.html.erb +0 -0
  100. data/spec/private/dispatch/fixture/app/views/layout/application.html.erb +11 -0
  101. data/spec/private/dispatch/fixture/config/environments/development.rb +6 -0
  102. data/spec/private/dispatch/fixture/config/environments/production.rb +5 -0
  103. data/spec/private/dispatch/fixture/config/environments/test.rb +6 -0
  104. data/spec/private/dispatch/fixture/config/init.rb +45 -0
  105. data/spec/private/dispatch/fixture/config/rack.rb +1 -0
  106. data/spec/private/dispatch/fixture/config/router.rb +35 -0
  107. data/spec/private/dispatch/fixture/log/development.log +1 -0
  108. data/spec/private/dispatch/fixture/log/merb.4000.pid +1 -0
  109. data/spec/private/dispatch/fixture/log/merb_test.log +2040 -0
  110. data/spec/private/dispatch/fixture/log/production.log +1 -0
  111. data/spec/private/dispatch/fixture/merb.4000.pid +1 -0
  112. data/spec/private/dispatch/fixture/public/images/merb.jpg +0 -0
  113. data/spec/private/dispatch/fixture/public/merb.fcgi +4 -0
  114. data/spec/private/dispatch/fixture/public/stylesheets/master.css +119 -0
  115. data/spec/private/dispatch/route_params_spec.rb +24 -0
  116. data/spec/private/dispatch/spec_helper.rb +1 -0
  117. data/spec/private/plugins/plugin_spec.rb +81 -0
  118. data/spec/private/rack/application_spec.rb +43 -0
  119. data/spec/public/DEFINITIONS +11 -0
  120. data/spec/public/abstract_controller/controllers/alt_views/layout/application.erb +1 -0
  121. data/spec/public/abstract_controller/controllers/alt_views/layout/merb/test/fixtures/abstract/render_string_controller_layout.erb +1 -0
  122. data/spec/public/abstract_controller/controllers/alt_views/layout/merb/test/fixtures/abstract/render_template_controller_layout.erb +1 -0
  123. data/spec/public/abstract_controller/controllers/alt_views/merb/test/fixtures/abstract/display_object_with_multiple_roots/index.erb +1 -0
  124. data/spec/public/abstract_controller/controllers/alt_views/merb/test/fixtures/abstract/display_object_with_multiple_roots/show.erb +1 -0
  125. data/spec/public/abstract_controller/controllers/alt_views/merb/test/fixtures/abstract/render_template_multiple_roots/index.erb +1 -0
  126. data/spec/public/abstract_controller/controllers/alt_views/partial/basic_partial_with_multiple_roots/_partial.erb +1 -0
  127. data/spec/public/abstract_controller/controllers/alt_views/render_template_multiple_roots_and_custom_location/index.erb +1 -0
  128. data/spec/public/abstract_controller/controllers/alt_views/render_template_multiple_roots_inherited/index.erb +1 -0
  129. data/spec/public/abstract_controller/controllers/display.rb +54 -0
  130. data/spec/public/abstract_controller/controllers/filters.rb +167 -0
  131. data/spec/public/abstract_controller/controllers/helpers.rb +31 -0
  132. data/spec/public/abstract_controller/controllers/partial.rb +106 -0
  133. data/spec/public/abstract_controller/controllers/render.rb +86 -0
  134. data/spec/public/abstract_controller/controllers/views/helpers/capture/index.erb +1 -0
  135. data/spec/public/abstract_controller/controllers/views/helpers/concat/index.erb +1 -0
  136. data/spec/public/abstract_controller/controllers/views/layout/alt.erb +1 -0
  137. data/spec/public/abstract_controller/controllers/views/layout/custom.erb +1 -0
  138. data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/display_object/index.erb +1 -0
  139. data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/display_object_with_action/new.erb +1 -0
  140. data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/render_template/index.erb +1 -0
  141. data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/render_template_app_layout/index.erb +0 -0
  142. data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/render_template_custom_layout/index.erb +1 -0
  143. data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/render_template_multiple_roots/index.erb +1 -0
  144. data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/render_template_multiple_roots/show.erb +1 -0
  145. data/spec/public/abstract_controller/controllers/views/partial/another_directory/_partial.erb +1 -0
  146. data/spec/public/abstract_controller/controllers/views/partial/basic_partial/_partial.erb +1 -0
  147. data/spec/public/abstract_controller/controllers/views/partial/basic_partial/index.erb +1 -0
  148. data/spec/public/abstract_controller/controllers/views/partial/basic_partial_with_multiple_roots/index.erb +1 -0
  149. data/spec/public/abstract_controller/controllers/views/partial/nested_partial/_first.erb +1 -0
  150. data/spec/public/abstract_controller/controllers/views/partial/nested_partial/_second.erb +1 -0
  151. data/spec/public/abstract_controller/controllers/views/partial/nested_partial/index.erb +1 -0
  152. data/spec/public/abstract_controller/controllers/views/partial/partial_in_another_directory/index.erb +1 -0
  153. data/spec/public/abstract_controller/controllers/views/partial/partial_with_both/_collection.erb +1 -0
  154. data/spec/public/abstract_controller/controllers/views/partial/partial_with_both/index.erb +1 -0
  155. data/spec/public/abstract_controller/controllers/views/partial/partial_with_collections/_collection.erb +1 -0
  156. data/spec/public/abstract_controller/controllers/views/partial/partial_with_collections/index.erb +1 -0
  157. data/spec/public/abstract_controller/controllers/views/partial/partial_with_collections_and_as/_collection.erb +1 -0
  158. data/spec/public/abstract_controller/controllers/views/partial/partial_with_collections_and_as/index.erb +1 -0
  159. data/spec/public/abstract_controller/controllers/views/partial/partial_with_locals/_variables.erb +1 -0
  160. data/spec/public/abstract_controller/controllers/views/partial/partial_with_locals/index.erb +1 -0
  161. data/spec/public/abstract_controller/controllers/views/partial/partial_with_with_and_locals/_both.erb +1 -0
  162. data/spec/public/abstract_controller/controllers/views/partial/partial_with_with_and_locals/index.erb +1 -0
  163. data/spec/public/abstract_controller/controllers/views/partial/with_as_partial/_with_partial.erb +1 -0
  164. data/spec/public/abstract_controller/controllers/views/partial/with_as_partial/index.erb +1 -0
  165. data/spec/public/abstract_controller/controllers/views/partial/with_nil_partial/_with_partial.erb +1 -0
  166. data/spec/public/abstract_controller/controllers/views/partial/with_nil_partial/index.erb +1 -0
  167. data/spec/public/abstract_controller/controllers/views/partial/with_partial/_with_partial.erb +1 -0
  168. data/spec/public/abstract_controller/controllers/views/partial/with_partial/index.erb +1 -0
  169. data/spec/public/abstract_controller/controllers/views/test_display/foo.html.erb +1 -0
  170. data/spec/public/abstract_controller/controllers/views/test_render/foo.html.erb +0 -0
  171. data/spec/public/abstract_controller/controllers/views/wonderful/index.erb +1 -0
  172. data/spec/public/abstract_controller/display_spec.rb +33 -0
  173. data/spec/public/abstract_controller/filter_spec.rb +80 -0
  174. data/spec/public/abstract_controller/helper_spec.rb +13 -0
  175. data/spec/public/abstract_controller/partial_spec.rb +53 -0
  176. data/spec/public/abstract_controller/render_spec.rb +70 -0
  177. data/spec/public/abstract_controller/spec_helper.rb +27 -0
  178. data/spec/public/boot_loader/boot_loader_spec.rb +33 -0
  179. data/spec/public/boot_loader/spec_helper.rb +1 -0
  180. data/spec/public/controller/base_spec.rb +31 -0
  181. data/spec/public/controller/controllers/base.rb +41 -0
  182. data/spec/public/controller/controllers/display.rb +40 -0
  183. data/spec/public/controller/controllers/responder.rb +67 -0
  184. data/spec/public/controller/controllers/url.rb +7 -0
  185. data/spec/public/controller/controllers/views/layout/custom.html.erb +1 -0
  186. data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/class_provides/index.html.erb +1 -0
  187. data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/class_provides/index.xml.erb +1 -0
  188. data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/display_with_template/index.html.erb +1 -0
  189. data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/html_default/index.html.erb +1 -0
  190. data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/layout/custom.html.erb +1 -0
  191. data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/local_provides/index.html.erb +1 -0
  192. data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/local_provides/index.xml.erb +1 -0
  193. data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/multi_provides/index.html.erb +1 -0
  194. data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/multi_provides/index.js.erb +1 -0
  195. data/spec/public/controller/display_spec.rb +34 -0
  196. data/spec/public/controller/log/merb.4000.pid +1 -0
  197. data/spec/public/controller/responder_spec.rb +95 -0
  198. data/spec/public/controller/spec_helper.rb +9 -0
  199. data/spec/public/controller/url_spec.rb +152 -0
  200. data/spec/public/directory_structure/directory/app/controllers/application.rb +3 -0
  201. data/spec/public/directory_structure/directory/app/controllers/base.rb +13 -0
  202. data/spec/public/directory_structure/directory/app/controllers/custom.rb +19 -0
  203. data/spec/public/directory_structure/directory/app/views/base/template.html.erb +1 -0
  204. data/spec/public/directory_structure/directory/app/views/wonderful/template.erb +1 -0
  205. data/spec/public/directory_structure/directory/config/router.rb +3 -0
  206. data/spec/public/directory_structure/directory/log/merb.4000.pid +1 -0
  207. data/spec/public/directory_structure/directory/log/merb_test.log +265 -0
  208. data/spec/public/directory_structure/directory/merb.4000.pid +1 -0
  209. data/spec/public/directory_structure/directory_spec.rb +44 -0
  210. data/spec/public/logger/logger_spec.rb +175 -0
  211. data/spec/public/logger/spec_helper.rb +1 -0
  212. data/spec/public/reloading/directory/app/controllers/application.rb +3 -0
  213. data/spec/public/reloading/directory/app/controllers/reload.rb +6 -0
  214. data/spec/public/reloading/directory/config/init.rb +2 -0
  215. data/spec/public/reloading/directory/log/merb.4000.pid +1 -0
  216. data/spec/public/reloading/directory/log/merb_test.log +59 -0
  217. data/spec/public/reloading/directory/merb.4000.pid +1 -0
  218. data/spec/public/reloading/reload_spec.rb +80 -0
  219. data/spec/public/request/multipart_spec.rb +15 -0
  220. data/spec/public/request/request_spec.rb +207 -0
  221. data/spec/public/router/default_spec.rb +21 -0
  222. data/spec/public/router/deferred_spec.rb +22 -0
  223. data/spec/public/router/namespace_spec.rb +113 -0
  224. data/spec/public/router/nested_resources_spec.rb +34 -0
  225. data/spec/public/router/resource_spec.rb +45 -0
  226. data/spec/public/router/resources_spec.rb +57 -0
  227. data/spec/public/router/spec_helper.rb +72 -0
  228. data/spec/public/router/special_spec.rb +44 -0
  229. data/spec/public/router/string_spec.rb +61 -0
  230. data/spec/public/template/template_spec.rb +92 -0
  231. data/spec/public/template/templates/error.html.erb +2 -0
  232. data/spec/public/template/templates/template.html.erb +1 -0
  233. data/spec/public/template/templates/template.html.myt +1 -0
  234. data/spec/public/test/controller_matchers_spec.rb +378 -0
  235. data/spec/public/test/controllers/controller_assertion_mock.rb +7 -0
  236. data/spec/public/test/controllers/dispatch_controller.rb +11 -0
  237. data/spec/public/test/controllers/spec_helper_controller.rb +30 -0
  238. data/spec/public/test/multipart_request_helper_spec.rb +159 -0
  239. data/spec/public/test/multipart_upload_text_file.txt +1 -0
  240. data/spec/public/test/request_helper_spec.rb +153 -0
  241. data/spec/public/test/route_helper_spec.rb +54 -0
  242. data/spec/public/test/route_matchers_spec.rb +133 -0
  243. data/spec/public/test/view_helper_spec.rb +96 -0
  244. data/spec/public/test/view_matchers_spec.rb +107 -0
  245. data/spec/spec_helper.rb +71 -0
  246. metadata +488 -0
@@ -0,0 +1,92 @@
1
+ # ==== Public Template API
2
+ # Merb::Template.register_extensions(engine<Class>, extenstions<Array[String]>)
3
+ #
4
+ # ==== Semipublic Template API
5
+ # Merb::Template.engine_for(path<String>)
6
+ # Merb::Template.template_name(path<String>)
7
+ # Merb::Template.inline_template(path<String>, mod<Module>)
8
+ #
9
+ # ==== Requirements for a new Template Engine
10
+ # A Template Engine must have at least a single class method called compile_template
11
+ # with the following parameters:
12
+ # * path<String>:: the full path to the template being compiled
13
+ # * name<String>:: the name of the method that will be inlined
14
+ # * mod<Module>:: the module that the method will be inlined into
15
+ require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
16
+
17
+ # A small structure to hold the templates so we can test the templating system in isolation
18
+ # from the framework
19
+
20
+ module Merb::Test::Fixtures
21
+ # This is a fake templating engine that just copies the text of the template
22
+ # exactly from the file
23
+
24
+ class MyTemplateEngine
25
+
26
+ def self.compile_template(path, name, mod)
27
+ text = File.read(path)
28
+ table = { "\r"=>"\\r", "\n"=>"\\n", "\t"=>"\\t", '"'=>'\\"', "\\"=>"\\\\" }
29
+ text = (text.split("\n").map {|x| '"' + (x.gsub(/[\r\n\t"\\]/) { |m| table[m] }) + '"'}).join(" +\n")
30
+ mod.class_eval <<-EOS, path
31
+ def #{name}
32
+ #{text}
33
+ end
34
+ EOS
35
+ end
36
+
37
+ module Mixin
38
+ end
39
+ end
40
+
41
+ module MyHelpers
42
+ end
43
+
44
+ class Environment
45
+ include MyHelpers
46
+ end
47
+ end
48
+
49
+ describe Merb::Template do
50
+
51
+ # @public
52
+ it "should accept template-type registrations via #register_extensions" do
53
+ Merb::Template.register_extensions(Merb::Test::Fixtures::MyTemplateEngine, %w[myt])
54
+ Merb::Template.engine_for("foo.myt").should == Merb::Test::Fixtures::MyTemplateEngine
55
+ end
56
+
57
+ # @semipublic
58
+
59
+ def rendering_template(template_path)
60
+ Merb::Template.inline_template(template_path, Merb::Test::Fixtures::MyHelpers)
61
+ Merb::Test::Fixtures::Environment.new.
62
+ send(Merb::Template.template_name(template_path))
63
+ end
64
+ alias_method :render_template, :rendering_template
65
+
66
+ it "should compile and inline templates via #inline methods for custom languages" do
67
+ template_path = File.dirname(__FILE__) / "templates" / "template.html.myt"
68
+ rendering_template(template_path).should == "Hello world!"
69
+ end
70
+
71
+ it "should compile and inline templates via #inline_template for erubis" do
72
+ template_path = File.dirname(__FILE__) / "templates" / "template.html.erb"
73
+ rendering_template(template_path).should == "Hello world!"
74
+ end
75
+
76
+ it "should know how to correctly report errors" do
77
+ template_path = File.dirname(__FILE__) / "templates" / "error.html.erb"
78
+ running { render_template(template_path) }.should raise_error(NameError, /`foo'/)
79
+ begin
80
+ render_template(template_path)
81
+ rescue Exception => e
82
+ e.backtrace.first.match(/\/([^:\/]*:\d*)/)[1].should == "error.html.erb:2"
83
+ end
84
+ end
85
+
86
+ it "should find the full template name for a path via #template_for" do
87
+ template_path = File.dirname(__FILE__) / "templates" / "template.html.erb"
88
+ name = Merb::Template.inline_template(template_path, Merb::Test::Fixtures::MyHelpers)
89
+ Merb::Test::Fixtures::Environment.new.should respond_to(name)
90
+ end
91
+
92
+ end
@@ -0,0 +1,2 @@
1
+ Hello
2
+ <%= foo %>
@@ -0,0 +1,378 @@
1
+ require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
2
+
3
+ Merb.start :environment => 'test', :log_level => :fatal
4
+
5
+ class TestController < Merb::Controller
6
+ attr_accessor :redirect_to
7
+ def redirect_action; redirect(@redirect_to || "/"); end
8
+ def success_action; end
9
+ def missing_action; render("i can has errorz", :status => 404); end
10
+ end
11
+
12
+ describe Merb::Test::Rspec::ControllerMatchers do
13
+ include Merb::Test::ControllerHelper
14
+ before(:each) do
15
+ Merb::Router.prepare do |r|
16
+ r.match("/redirect").to(:controller => "test_controller", :action => "redirect_action")
17
+ r.match("/success").to(:controller => "test_controller", :action => "success_action")
18
+ r.match("/missing").to(:controller => "test_controller", :action => "missing_action")
19
+ end
20
+ end
21
+
22
+ describe "#redirect" do
23
+ it "should work with the result of a dispatch_to helper call" do
24
+ dispatch_to(TestController, :redirect_action).should redirect
25
+ end
26
+
27
+ it "should work with the result of a get helper call" do
28
+ get("/redirect").should redirect
29
+ end
30
+
31
+ it "should work with a redirection code" do
32
+ dispatch_to(TestController, :redirect_action).status.should redirect
33
+ end
34
+ end
35
+
36
+ describe "#redirect_to" do
37
+ it "should work with the result of a dispatch_to helper call" do
38
+ dispatch_to(TestController, :redirect_action).should redirect_to("/")
39
+ dispatch_to(TestController, :redirect_action){ |controller| controller.redirect_to = "http://example.com/" }.should redirect_to("http://example.com/")
40
+ end
41
+
42
+ it "should work with the result of a get helper call" do
43
+ get("/redirect"){|controller| controller.redirect_to = "http://example.com/" }.should redirect_to("http://example.com/")
44
+ end
45
+ end
46
+
47
+ describe "#respond_successfully" do
48
+ it "should work with the result of a dispatch_to helper call" do
49
+ dispatch_to(TestController, :success_action).should respond_successfully
50
+ end
51
+
52
+ it "should work with the result of a get helper call" do
53
+ get("/success").should respond_successfully
54
+ end
55
+
56
+ it "should work with a redirection code" do
57
+ dispatch_to(TestController, :success_action).status.should be_successful
58
+ end
59
+ end
60
+
61
+ describe "#be_missing" do
62
+ it "should work with the result of a dispatch_to helper call" do
63
+ dispatch_to(TestController, :missing_action).should be_missing
64
+ end
65
+
66
+ it "should work with the result of a get helper call" do
67
+ get("/missing").should be_client_error
68
+ end
69
+
70
+ it "should work with a redirection code" do
71
+ dispatch_to(TestController, :missing_action).status.should be_missing
72
+ end
73
+ end
74
+ end
75
+
76
+ module Merb::Test::Rspec
77
+ module ControllerMatchers
78
+ class RedirectableTarget
79
+ attr_accessor :status, :headers
80
+ def initialize; @headers = {}; end
81
+ end
82
+
83
+ describe BeRedirect do
84
+ before(:each) do
85
+ @target = RedirectableTarget.new
86
+ end
87
+
88
+ it "should match a 301 'Moved Permanently' redirect code" do
89
+ BeRedirect.new.matches?(301).should be_true
90
+ end
91
+
92
+ it "should match a 302 'Found' redirect code" do
93
+ BeRedirect.new.matches?(302).should be_true
94
+ end
95
+
96
+ it "should match a 303 'See Other' redirect code" do
97
+ BeRedirect.new.matches?(303).should be_true
98
+ end
99
+
100
+ it "should match a 304 'Not Modified' redirect code" do
101
+ BeRedirect.new.matches?(304).should be_true
102
+ end
103
+
104
+ it "should match a 307 'Temporary Redirect' redirect code" do
105
+ BeRedirect.new.matches?(307).should be_true
106
+ end
107
+
108
+ it "should match a target with a valid redirect code" do
109
+ @target.status = 301
110
+
111
+ BeRedirect.new.matches?(@target).should be_true
112
+ end
113
+
114
+ it "should not match a target with an unused redirect code" do
115
+ @target.status = 399
116
+
117
+ BeRedirect.new.matches?(@target).should_not be_true
118
+ end
119
+
120
+ it "should not match a target with a non redirect code" do
121
+ @target.status = 200
122
+
123
+ BeRedirect.new.matches?(@target).should_not be_true
124
+ end
125
+
126
+ describe "#failure_message" do
127
+ it "should be 'expected to redirect' when the target is a status code" do
128
+ matcher = BeRedirect.new
129
+ matcher.matches?(200)
130
+ matcher.failure_message.should == "expected to redirect"
131
+ end
132
+
133
+ it "should be 'expected Foo#bar to redirect' when the target's controller is Foo and action is bar" do
134
+ matcher = BeRedirect.new
135
+ @target.stub!(:controller_name).and_return :Foo
136
+ @target.stub!(:action_name).and_return :bar
137
+ matcher.matches?(@target)
138
+ matcher.failure_message.should == "expected Foo#bar to redirect"
139
+ end
140
+ end
141
+
142
+ describe "#negative_failure_message" do
143
+ it "should be 'expected not to redirect' when the target is a status code" do
144
+ matcher = BeRedirect.new
145
+ matcher.matches?(200)
146
+ matcher.negative_failure_message.should == "expected not to redirect"
147
+ end
148
+
149
+ it "should be 'expected Foo#bar to redirect' when the target's controller is Foo and action is bar" do
150
+ matcher = BeRedirect.new
151
+ @target.stub!(:controller_name).and_return :Foo
152
+ @target.stub!(:action_name).and_return :bar
153
+ matcher.matches?(@target)
154
+ matcher.negative_failure_message.should == "expected Foo#bar not to redirect"
155
+ end
156
+ end
157
+ end
158
+
159
+ describe RedirectTo do
160
+ before(:each) do
161
+ @target = RedirectableTarget.new
162
+ end
163
+
164
+ it "should match a target if the status code is 300 level and the locations match" do
165
+ @target.status = 301
166
+ @target.headers['Location'] = "http://example.com/"
167
+
168
+ RedirectTo.new("http://example.com/").matches?(@target).should be_true
169
+ end
170
+
171
+ it "should not match a target if the status code is not 300 level but the locations match" do
172
+ @target.status = 404
173
+ @target.headers['Location'] = "http://example.com/"
174
+
175
+ RedirectTo.new("http://example.com/").matches?(@target).should_not be_true
176
+ end
177
+
178
+ it "should not match a target if the status code is 300 level but the locations do not match" do
179
+ @target.status = 301
180
+ @target.headers['Location'] = "http://merbivore.com/"
181
+
182
+ RedirectTo.new("http://example.com/").matches?(@target).should_not be_true
183
+ end
184
+
185
+ describe "#failure_message" do
186
+ it "should be 'expected Foo#bar to redirect to <http://expected.com/>, but was <http://target.com/>' when the expected url is http://expected.com/ and the target url is http://target.com/" do
187
+ @target.stub!(:controller_name).and_return :Foo
188
+ @target.stub!(:action_name).and_return :bar
189
+ @target.status = 301
190
+ @target.headers['Location'] = "http://target.com/"
191
+ matcher = RedirectTo.new("http://expected.com/")
192
+ matcher.matches?(@target)
193
+ matcher.failure_message.should == "expected Foo#bar to redirect to <http://expected.com/>, but was <http://target.com/>"
194
+ end
195
+
196
+ it "should be 'expected Foo#bar to redirect, but there was no redirection' when the target is not redirected" do
197
+ @target.stub!(:controller_name).and_return :Foo
198
+ @target.stub!(:action_name).and_return :bar
199
+ @target.status = 200
200
+ @target.headers['Location'] = "http://target.com/"
201
+ matcher = RedirectTo.new("http://expected.com/")
202
+ matcher.matches?(@target)
203
+ matcher.failure_message.should == "expected Foo#bar to redirect to <http://expected.com/>, but there was no redirection"
204
+ end
205
+ end
206
+
207
+ describe "#negative_failure_message" do
208
+ it "should be 'expected Foo#bar not to redirect to <http://expected.com/>, but it did anyways" do
209
+ @target.stub!(:controller_name).and_return :Foo
210
+ @target.stub!(:action_name).and_return :bar
211
+ @target.status = 200
212
+ @target.headers['Location'] = "http://target.com/"
213
+ matcher = RedirectTo.new("http://expected.com/")
214
+ matcher.matches?(@target)
215
+ matcher.negative_failure_message.should == "expected Foo#bar not to redirect to <http://expected.com/>, but did anyway"
216
+ end
217
+ end
218
+ end
219
+
220
+ describe BeSuccess do
221
+ before(:each) do
222
+ @target = RedirectableTarget.new
223
+ end
224
+
225
+ it "should match a target with a 200 'OK' status code" do
226
+ BeSuccess.new.matches?(200).should be_true
227
+ end
228
+
229
+ it "should match a target with a 201 'Created' status code" do
230
+ BeSuccess.new.matches?(201).should be_true
231
+ end
232
+
233
+ it "should match a target with a 202 'Accepted' status code" do
234
+ BeSuccess.new.matches?(202).should be_true
235
+ end
236
+
237
+ it "should match a target with a 203 'Non-Authoritative Information' status code" do
238
+ BeSuccess.new.matches?(203).should be_true
239
+ end
240
+
241
+ it "should match a target with a 204 'No Content' status code" do
242
+ BeSuccess.new.matches?(204).should be_true
243
+ end
244
+
245
+ it "should match a target with a 205 'Reset Content' status code" do
246
+ BeSuccess.new.matches?(205).should be_true
247
+ end
248
+
249
+ it "should match a target with a 206 'Partial Content' status code" do
250
+ BeSuccess.new.matches?(206).should be_true
251
+ end
252
+
253
+ it "should match a target with a 207 'Multi-Status' status code" do
254
+ BeSuccess.new.matches?(207).should be_true
255
+ end
256
+
257
+ it "should not match a target with an unused 200 level status code" do
258
+ BeSuccess.new.matches?(299).should_not be_true
259
+ end
260
+
261
+ it "should not match a target with a non 200 level status code" do
262
+ BeSuccess.new.matches?(301).should_not be_true
263
+ end
264
+
265
+ describe "#failure_message" do
266
+ it "should be 'expected to be successful but was 300' when the target is status code 300" do
267
+ matcher = BeSuccess.new
268
+ matcher.matches?(300)
269
+ matcher.failure_message.should == "expected to be successful but was 300"
270
+ end
271
+
272
+ it "should be 'expected Foo#bar to be successful but was 404' when the target is controller-ish" do
273
+ @target.stub!(:controller_name).and_return :Foo
274
+ @target.stub!(:action_name).and_return :bar
275
+ @target.status = 404
276
+ matcher = BeSuccess.new
277
+ matcher.matches?(@target)
278
+ matcher.failure_message.should == "expected Foo#bar to be successful but was 404"
279
+ end
280
+ end
281
+
282
+ describe "#negative_failure_message" do
283
+ it "should be 'expected not to be successful but it was' when the target is a 200 status code" do
284
+ matcher = BeSuccess.new
285
+ matcher.matches?(200)
286
+ matcher.negative_failure_message.should == "expected not to be successful but it was 200"
287
+ end
288
+
289
+ it "should be 'expected Foo#bar not to be successful but it was 200' when the target is controller-ish" do
290
+ @target.stub!(:controller_name).and_return :Foo
291
+ @target.stub!(:action_name).and_return :bar
292
+ @target.status = 200
293
+ matcher = BeSuccess.new
294
+ matcher.matches?(@target)
295
+ matcher.negative_failure_message.should == "expected Foo#bar not to be successful but it was 200"
296
+ end
297
+ end
298
+ end
299
+
300
+ describe BeMissing do
301
+ before(:each) do
302
+ @target = RedirectableTarget.new
303
+ end
304
+
305
+ it "should match a 400 'Bad Request'" do
306
+ BeMissing.new.matches?(400).should be_true
307
+ end
308
+
309
+ it "should match a 401 'Unauthorized'" do
310
+ BeMissing.new.matches?(401).should be_true
311
+ end
312
+
313
+ it "should match a 403 'Forbidden'" do
314
+ BeMissing.new.matches?(403).should be_true
315
+ end
316
+
317
+ it "should match a 404 'Not Found'" do
318
+ BeMissing.new.matches?(404).should be_true
319
+ end
320
+
321
+ it "should match a 409 'Conflict'" do
322
+ BeMissing.new.matches?(409).should be_true
323
+ end
324
+
325
+ it "should match a target with a valid client side error code" do
326
+ @target.status = 404
327
+
328
+ BeMissing.new.matches?(@target).should be_true
329
+ end
330
+
331
+ it "should not match a target with an unused client side error code" do
332
+ @target.status = 499
333
+
334
+ BeMissing.new.matches?(@target).should_not be_true
335
+ end
336
+
337
+ it "should not match a target with a non client side error code" do
338
+ @target.status = 200
339
+
340
+ BeMissing.new.matches?(@target).should_not be_true
341
+ end
342
+
343
+ describe "#failure_message" do
344
+ it "should be 'expected to be missing but was 300' when the target is status code 300" do
345
+ matcher = BeMissing.new
346
+ matcher.matches?(300)
347
+ matcher.failure_message.should == "expected to be missing but was 300"
348
+ end
349
+
350
+ it "should be 'expected Foo#bar to be successful but was 301' when the target is controller-ish" do
351
+ @target.stub!(:controller_name).and_return :Foo
352
+ @target.stub!(:action_name).and_return :bar
353
+ @target.status = 301
354
+ matcher = BeMissing.new
355
+ matcher.matches?(@target)
356
+ matcher.failure_message.should == "expected Foo#bar to be missing but was 301"
357
+ end
358
+ end
359
+
360
+ describe "#negative_failure_message" do
361
+ it "should be 'expected not to be successful but it was' when the target is a 400 status code" do
362
+ matcher = BeMissing.new
363
+ matcher.matches?(400)
364
+ matcher.negative_failure_message.should == "expected not to be missing but it was 400"
365
+ end
366
+
367
+ it "should be 'expected Foo#bar not to be missing but it was 404' when the target is controller-ish" do
368
+ @target.stub!(:controller_name).and_return :Foo
369
+ @target.stub!(:action_name).and_return :bar
370
+ @target.status = 404
371
+ matcher = BeMissing.new
372
+ matcher.matches?(@target)
373
+ matcher.negative_failure_message.should == "expected Foo#bar not to be missing but it was 404"
374
+ end
375
+ end
376
+ end
377
+ end
378
+ end