Sha256: 692a5e3607b58ec29957d193d03f5b62a98e59e712fb1e2558c0045092a2024e

Contents?: true

Size: 1.73 KB

Versions: 20

Compression:

Stored size: 1.73 KB

Contents

require 'rest-core/test'

describe RC::JsonResponse do
  describe 'app' do
    app = RC::JsonResponse.new(RC::Dry.new, true)
    bad = 'bad json'

    would 'do nothing' do
      expected = {RC::RESPONSE_BODY => nil,
                  RC::REQUEST_HEADERS => {'Accept' => 'application/json'}}
      app.call({}){ |response| response.should.eq(expected) }
    end

    would 'decode' do
      expected = {RC::RESPONSE_BODY => {},
                  RC::REQUEST_HEADERS => {'Accept' => 'application/json'}}
      app.call(RC::RESPONSE_BODY => '{}') do |response|
        response.should.eq(expected)
      end
    end

    would 'give proper parse error' do
      app.call(RC::RESPONSE_BODY => bad) do |response|
        err = response[RC::FAIL].first
        err.should.kind_of?(RC::Json.const_get(:ParseError))
        err.should.kind_of?(RC::JsonResponse::ParseError)
      end
    end

    would 'give me original text' do
      app.call(RC::RESPONSE_BODY => bad) do |response|
        err = response[RC::FAIL].first
        err.message    .should.include?(bad)
        err.body       .should.eq(bad)
        err.cause.class.should.eq(RC::Json.const_get(:ParseError))
      end
    end
  end

  describe 'client' do
    client = RC::Builder.client do
      use RC::JsonResponse, true
      run Class.new{
        def call env
          yield(env.merge(RC::RESPONSE_BODY => '{}'))
        end
      }
    end

    would 'do nothing' do
      expected = '{}'
      client.new(:json_response => false).get(''){ |response|
        response.should.eq(expected)
      }.get('').should.eq(expected)
    end

    would 'decode' do
      expected = {}
      client.new.get(''){ |response|
        response.should.eq(expected)
      }.get('').should.eq(expected)
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
rest-core-4.0.0 test/test_json_response.rb
rest-core-3.6.0 test/test_json_response.rb
rest-core-3.5.92 test/test_json_response.rb
rest-core-3.5.91 test/test_json_response.rb
rest-core-3.5.9 test/test_json_response.rb
rest-core-3.5.8 test/test_json_response.rb
rest-core-3.5.7 test/test_json_response.rb
rest-core-3.5.6 test/test_json_response.rb
rest-core-3.5.5 test/test_json_response.rb
rest-core-3.5.4 test/test_json_response.rb
rest-core-3.5.3 test/test_json_response.rb
rest-core-3.5.2 test/test_json_response.rb
rest-core-3.5.1 test/test_json_response.rb
rest-core-3.5.0 test/test_json_response.rb
rest-core-3.4.1 test/test_json_response.rb
rest-core-3.4.0 test/test_json_response.rb
rest-core-3.3.3 test/test_json_response.rb
rest-core-3.3.2 test/test_json_response.rb
rest-core-3.3.1 test/test_json_response.rb
rest-core-3.3.0 test/test_json_response.rb