require "controller/spec_helper" describe "HttpController" do with_environment with_http before :all do HttpController = Rad::HttpController rad.after :environment do rad.conveyors.web do |web| web.use Rad::Processors::HttpWriter web.use Rad::Processors::ControllerCaller end end end after :all do remove_constants %w( HttpController ContentTypeSpec StatusSpec StatusShortcutsSpec LocationSpec RequestMethod ForbidGetSpec ) end describe 'render' do it "should take :content_type option" do class ::ContentTypeSpec inherit HttpController def action render inline: "some content", content_type: Mime.js end end wcall(ContentTypeSpec, :action) response.content_type.should == "application/javascript" end it "should take :status option" do class ::StatusSpec inherit HttpController def action render inline: "some content", status: 220 end end wcall(StatusSpec, :action) response.status.should == 220 end it "should take shortcuts to status codes" do class ::StatusShortcutsSpec inherit HttpController def ok render :ok end def failed render :failed end end wcall(StatusShortcutsSpec, :ok) response.status.should == 200 wcall(StatusShortcutsSpec, :failed) response.status.should == 500 end it "should take :location option" do class ::LocationSpec inherit HttpController def action render location: "/" end end # mock class ::LocationSpec def redirect_to location self.class.location = location end class << self attr_accessor :location end end wcall(LocationSpec, :action, format: 'html') LocationSpec.location.should == '/' end end it "should specify request method" do class ::RequestMethod inherit HttpController def action workspace.request.post?.should == true end end workspace, params = {env: {'REQUEST_METHOD' => 'POST'}}, {} wcall(RequestMethod, :action, workspace, params) end it "should protect methods from GET request" do class ::ForbidGetSpec inherit HttpController allow_get_for :get_action def get_action; end def post_action; end end workspace = {env: {'REQUEST_METHOD' => 'GET'}} wcall(ForbidGetSpec, :get_action, workspace, {}) lambda{ wcall(ForbidGetSpec, :post_action, workspace, {}) }.should raise_error(/not allowed/) end end