Sha256: 61e180ef787a3f3d7d623e90aba9b4719926f47b48edb0eaa199668a5c622716

Contents?: true

Size: 1.86 KB

Versions: 20

Compression:

Stored size: 1.86 KB

Contents

require File.expand_path("spec_helper", File.dirname(File.dirname(__FILE__)))

describe "json plugin" do
  before do
    c = Class.new do
      def to_json
        '[1]'
      end
    end

    app(:bare) do
      plugin :json, :classes=>[Array, Hash, c]

      route do |r|
        r.is 'array' do
          [1, 2, 3]
        end

        r.is "hash" do
          {'a'=>'b'}
        end

        r.is 'c' do
          c.new
        end

        r.is 'd' do
          response['Content-Type'] = 'foo'
          c.new
        end
      end
    end
  end

  it "should use a json content type for a json response" do
    header('Content-Type', "/array").must_equal 'application/json'
    header('Content-Type', "/hash").must_equal 'application/json'
    header('Content-Type', "/c").must_equal 'application/json'
    header('Content-Type').must_equal 'text/html'
  end

  it "should not override existing content type for a json response" do
    header('Content-Type', "/d").must_equal 'foo'
  end

  it "should convert objects to json" do
    body('/array').gsub(/\s/, '').must_equal '[1,2,3]'
    body('/hash').gsub(/\s/, '').must_equal '{"a":"b"}'
    body('/c').must_equal '[1]'
    body.must_equal ''
  end

  it "should work when subclassing" do
    @app = Class.new(app)
    app.route{[1]}
    body.must_equal '[1]'
  end

  it "should accept custom serializers" do
    app.plugin :json, :serializer => proc{|o| o.inspect}
    body("/hash").must_equal '{"a"=>"b"}'
  end

  it "should give serializer the request if :include_request is set" do
    app.plugin :json,
      :include_request => true,
      :serializer => lambda{|o,r| "#{o['a']}:#{r.path_info}"}

    body("/hash").must_equal 'b:/hash'
  end

  it "should allow custom content type for a response" do
    app.plugin :json, :content_type => "application/xml"
    header('Content-Type', "/array").must_equal 'application/xml'
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
roda-2.29.0 spec/plugin/json_spec.rb
roda-2.28.0 spec/plugin/json_spec.rb
roda-2.27.0 spec/plugin/json_spec.rb
roda-2.26.0 spec/plugin/json_spec.rb
roda-2.25.0 spec/plugin/json_spec.rb
roda-2.24.0 spec/plugin/json_spec.rb
roda-2.23.0 spec/plugin/json_spec.rb
roda-2.22.0 spec/plugin/json_spec.rb
roda-2.21.0 spec/plugin/json_spec.rb
roda-2.20.0 spec/plugin/json_spec.rb
roda-2.19.0 spec/plugin/json_spec.rb
roda-2.18.0 spec/plugin/json_spec.rb
roda-2.17.0 spec/plugin/json_spec.rb
roda-2.16.0 spec/plugin/json_spec.rb
roda-2.15.0 spec/plugin/json_spec.rb
roda-2.14.0 spec/plugin/json_spec.rb
roda-2.13.0 spec/plugin/json_spec.rb
roda-2.12.0 spec/plugin/json_spec.rb
roda-2.11.0 spec/plugin/json_spec.rb
roda-2.10.0 spec/plugin/json_spec.rb