Sha256: 17f93abae2d8cafb307489121683196ccd8afaff8b4d9bf6dc00f03b79007495

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

require 'helper'

class MashifyTest < Test::Unit::TestCase
  context 'when used' do
    setup do
      @stubs = Faraday::Adapter::Test::Stubs.new
      @conn  = Faraday::Connection.new do |builder|
        builder.adapter :test, @stubs
        builder.use Faraday::Response::ParseJson
        builder.use Faraday::Response::Mashify
      end
    end

    should 'create a Hashie::Mash from the body' do
      @stubs.get('/hash') {[200, {'content-type' => 'application/json; charset=utf-8'}, '{"name":"Wynn Netherland","username":"pengwynn"}']}
      me = @conn.get("/hash").body
      assert_equal 'Wynn Netherland', me.name
      assert_equal 'pengwynn', me.username
    end

    should 'handle arrays' do
      @stubs.get('/array') {[200, {'content-type' => 'application/json; charset=utf-8'}, '[{"username":"pengwynn"},{"username":"jnunemaker"}]']}
      us = @conn.get("/array").body
      assert_equal 'pengwynn', us.first.username
      assert_equal 'jnunemaker', us.last.username
    end

    should 'handle arrays of non-hashes' do
      @stubs.get('/array/simple') {[200, {'content-type' => 'application/json; charset=utf-8'}, "[123, 456]"]}
      values = @conn.get("/array/simple").body
      assert_equal 123, values.first
      assert_equal 456, values.last
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
faraday_middleware-0.3.0 test/mashify_test.rb