test/functional/test_front.rb in spontaneous-0.2.0.alpha2 vs test/functional/test_front.rb in spontaneous-0.2.0.alpha3

- old
+ new

@@ -13,14 +13,16 @@ end def self.startup @site_root = Dir.mktmpdir FileUtils.cp_r(File.expand_path("../../fixtures/public/templates", __FILE__), @site_root) + Spontaneous::Output.write_compiled_scripts = true end def self.shutdown teardown_site + Spontaneous::Output.write_compiled_scripts = false end def setup @site = setup_site(self.class.site_root) Site.publishing_method = :immediate @@ -56,18 +58,21 @@ def session last_request.env['rack.session'] end + def formats(format_list) + format_list.map { |f| Page.format_for(f) } + end + context "Public pages" do setup do Site.publishing_method = :immediate State.delete Content.delete - Change.delete class ::SitePage < Spontaneous::Page layout :default layout :dynamic box :pages @@ -140,27 +145,27 @@ last_response.body.should == "/about.html\n" last_response.content_type.should == "text/html;charset=utf-8" end should "honor the format of the request" do - @about.class.stubs(:formats).returns([:html, :pdf]) + @about.class.outputs :html, :pdf get '/about.pdf' assert last_response.ok? last_response.body.should == "/about.pdf\n" last_response.content_type.should == "application/pdf;charset=utf-8" end should "provide the default format of the page if none is explicitly given" do - @about.class.stubs(:formats).returns([:rss, :html]) + @about.class.outputs :rss, :html get '/about' assert last_response.ok? last_response.content_type.should == ::Rack::Mime.mime_type('.rss') + ";charset=utf-8" last_response.body.should == "/about.rss\n" end should "return a custom content type if one is defined" do - @about.class.formats [{:html => "application/xhtml+xml"}] + @about.class.outputs [:html, {:mimetype => "application/xhtml+xml"}] get '/about' assert last_response.ok? last_response.content_type.should == "application/xhtml+xml;charset=utf-8" last_response.body.should == "/about.html\n" end @@ -169,14 +174,20 @@ should "raise a 404 if asked for a format not provided by the page" do get '/about.time' assert last_response.status == 404 end + should "raise a 404 when accessing a private format" do + @about.class.outputs [:html, {:mimetype => "application/xhtml+xml"}], [:rss, {:private => true}] + get '/about.rss' + assert last_response.status == 404 + end + context "Dynamic pages" do setup do - Page.stubs(:path).with("/about").returns(about) - Page.stubs(:path).with("/news").returns(news) + Spontaneous::Page.stubs(:path).with("/about").returns(about) + Spontaneous::Page.stubs(:path).with("/news").returns(news) end should "default to static behaviour" do SitePage.dynamic?.should be_false page = SitePage.new @@ -252,11 +263,11 @@ should "allow handing POST requests" do SitePage.request :post do show "#news" end post '/about' - assert last_response.status == 200 + assert last_response.status == 200, "Expected status 200 but recieved #{last_response.status}" last_response.body.should == "/news.html\n" end should "allow returning of any status code without altering content" do SitePage.request do @@ -274,10 +285,34 @@ get '/about' assert last_response.status == 200 last_response.body.should == "/about.html\n" last_response.headers["X-Works"].should == "Yes" end + + should "allow passing of template params & a page to the render call" do + SitePage.layout do + "{{ teeth }}" + end + SitePage.request do + render page, :teeth => "white" + end + get '/about' + assert last_response.status == 200 + last_response.body.should == "white" + end + + should "give access to the request params within the controller" do + SitePage.layout { "{{ params[:horse] }}*{{ equine }}" } + SitePage.request :post do + value = params[:horse] + render page, :equine => value + end + post '/about', :horse => "dancing" + assert last_response.status == 200 + last_response.body.should == "dancing*dancing" + end + # should "handle anything that responds to #render(format)" do # show = mock() # show.stubs(:render).returns("mocked") # about.stubs(:request_show).returns(show) # get '/about' @@ -365,42 +400,48 @@ last_response.body.should == "42/peter/example.org\n" end context "caching" do setup do - Spontaneous::Render.cache_templates = true - @cache_file = "#{Spontaneous.revision_dir(1)}/html/dynamic/index.html.rb" + Spontaneous::Output.cache_templates = true + @cache_file = "#{Spontaneous.revision_dir(1)}/dynamic/dynamic.html.rb" + Spontaneous::Output.write_compiled_scripts = true end teardown do - Spontaneous::Render.cache_templates = false + Spontaneous::Output.cache_templates = true end should "use pre-rendered versions of the templates" do dummy_content = 'cached-version/#{session[\'user_id\']}' - dummy_template = File.join(@site.revision_root, "dummy.html.cut") + dummy_template = File.join(@site.revision_root, "current/dynamic/dynamic.html.cut") File.open(dummy_template, 'w') { |f| f.write(dummy_content) } - Spontaneous::Render.stubs(:output_path).returns(dummy_template) + # Spontaneous::Render.stubs(:output_path).returns(dummy_template) get '/dynamic', {'wendy' => 'peter'}, 'rack.session' => { 'user_id' => 42 } last_response.body.should == "cached-version/42" end should "cache templates as ruby files" do - @cache_file = "#{Spontaneous.revision_dir(1)}/dynamic/dynamic/index.html.rb" + dummy_content = 'cached-version/#{session[\'user_id\']}' + dummy_template = File.join(@site.revision_root, "current/dynamic/index.html.cut") + Spontaneous::Output.renderer.write_compiled_scripts = true + File.open(dummy_template, 'w') { |f| f.write(dummy_content) } FileUtils.rm(@cache_file) if File.exists?(@cache_file) File.exists?(@cache_file).should be_false get '/dynamic', {'wendy' => 'peter'}, 'rack.session' => { 'user_id' => 42 } - # puts `ls -l #{File.dirname(@cache_file)}` + File.exists?(@cache_file).should be_true - File.open(@cache_file, 'w') { |f| f.write('_buf << %Q`@cache_filed-version/#{params[\'wendy\']}`;')} + File.open(@cache_file, 'w') { |f| f.write('@__buf << %Q`@cache_filed-version/#{params[\'wendy\']}`;')} + # Force compiled file to have a later timestamp + File.utime(Time.now, Time.now + 1, @cache_file) get '/dynamic', {'wendy' => 'peter'}, 'rack.session' => { 'user_id' => 42 } last_response.body.should == "@cache_filed-version/peter" FileUtils.rm(@cache_file) end should "not cache templates if caching turned off" do - Spontaneous::Render.cache_templates = false + Spontaneous::Output.cache_templates = false FileUtils.rm(@cache_file) if File.exists?(@cache_file) File.exists?(@cache_file).should be_false get '/dynamic', {'wendy' => 'peter'}, 'rack.session' => { 'user_id' => 42 } File.exists?(@cache_file).should be_false end @@ -445,10 +486,11 @@ get "/block" do "Block" end end + Page.stubs(:path).with("/").returns(root) Page.stubs(:path).with("/about").returns(about) Page.stubs(:path).with("/about/now").returns(subpage) end teardown do @@ -462,10 +504,17 @@ get "/about" assert last_response.ok? last_response.body.should == about.render end + should "work on the homepage" do + get "/@comments" + assert last_response.ok? + last_response.body.should == "Success" + end + + should "be recognised" do get "/about/@comments" assert last_response.ok? last_response.body.should == "Success" end @@ -510,19 +559,21 @@ should "be able to generate urls for actions" do about.action_url(:status, "/good").should == "/about/@status/good" end should "pass the format onto the page if the action returns it to the render call" do - about.stubs(:provides_format?).with(:'xml', anything).returns(true) - about.expects(:render).with(:'xml', anything).returns("/about.xml") - about.expects(:render).with(:'html', anything).never + about.class.outputs :html, :xml + about.class.layout do + "${path}.${__format}" + end get "/about/@comments/page.xml" assert last_response.ok? last_response.body.should == "/about.xml" end should "use the format within the action if required" do + about.class.outputs :html, :xml get "/about/@comments/format.xml" assert last_response.ok? last_response.body.should == "xml" end @@ -610,6 +661,6 @@ expiry = DateTime.parse last_response.headers["Expires"] expiry.year.should == (Date.today.year) + 10 end end end -end +end \ No newline at end of file