Sha256: 205aacb98ab19e2050f6088ebf5d34d97fadbdb22af88bbf9d1b98a2f259204f

Contents?: true

Size: 1.89 KB

Versions: 6

Compression:

Stored size: 1.89 KB

Contents

require File.dirname(__FILE__) + '/spec_helper'
require File.dirname(__FILE__) + '/../lib/g5k/sinatra/helpers'

describe G5K::Sinatra::Helpers do
  class Request
    include G5K::Sinatra::Helpers
    def initialize(options = {})
      @format = options[:format]
    end
    def params; {:format => @format}; end
  end
  
  it "should throw a 406 error if it does not provide the requested format" do
    req = Request.new(:format => 'json')
    req.should_receive(:throw).with(:halt, [406, "The accepted types are: xml, js"])
    req.provides(:xml, :js)
  end
  
  it "should not throw a 406 error if it can provide the requested format" do
    req = Request.new(:format => 'json')
    req.should_not_receive(:throw)
    req.provides(:xml, :json)
  end
  
  it "should format the error with the given parser" do
    require 'json'
    options = {:code => 500, :message => "message", :title => "title"}
    req = Request.new
    req.should_receive(:content_type).with(:json, :charset => 'utf-8')
    req.formatted_error(404, options.merge({:parser => JSON, :format => 'json'})).should == [404, options.to_json]
  end
  
  it "should format the error even when no parser is given" do
    options = {:code => 500, :message => "message", :title => "title"}
    req = Request.new
    req.should_receive(:content_type).with(:txt, :charset => 'utf-8')
    req.formatted_error(404, options).should == [404, "code=500;message=message;title=title"]
  end
  
  it "should log the error if a logger is defined in the Sinatra options" do
    options = {:code => 500, :message => "message", :title => "title"}
    module Sinatra
      module Application
      end
    end
    logger = mock('logger')
    req = Request.new
    req.stub!(:content_type)
    req.stub!(:throw)
    Sinatra::Application.should_receive(:logger).and_return(logger)
    logger.should_receive(:error).with(options.inspect)
    req.formatted_error(404, options)    
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
cryx-g5k-0.2.10 spec/sinatra_helpers_spec.rb
cryx-g5k-0.2.11 spec/sinatra_helpers_spec.rb
cryx-g5k-0.2.6 spec/sinatra_helpers_spec.rb
cryx-g5k-0.2.7 spec/sinatra_helpers_spec.rb
cryx-g5k-0.2.8 spec/sinatra_helpers_spec.rb
cryx-g5k-0.2.9 spec/sinatra_helpers_spec.rb