Sha256: 174366b815336e7da675e281e9a9533a04267b6b9ead1a21e4796cf6af2ce90f

Contents?: true

Size: 1.58 KB

Versions: 22

Compression:

Stored size: 1.58 KB

Contents

require File.dirname(__FILE__) + '/spec_helper'

describe Hash, "to_xml_attributes" do
  before do
    @hash = { :one => "ONE", "two" => "TWO" }
  end

  it "should turn the hash into xml attributes" do
    attrs = @hash.to_xml_attributes
    attrs.should match(/one="ONE"/m)
    attrs.should match(/two="TWO"/m)
  end

  it 'should preserve _ in hash keys' do
    attrs = {
      :some_long_attribute => "with short value",
      :crash               => :burn,
      :merb                => "uses extlib"
    }.to_xml_attributes

    attrs.should =~ /some_long_attribute="with short value"/
    attrs.should =~ /merb="uses extlib"/
    attrs.should =~ /crash="burn"/
  end
end


describe Hash, 'to_params' do
  {
    { "foo" => "bar", "baz" => "bat" } => "foo=bar&baz=bat",
    { "foo" => [ "bar", "baz" ] } => "foo[]=bar&foo[]=baz",
    { "foo" => [ {"bar" => "1"}, {"bar" => 2} ] } => "foo[][bar]=1&foo[][bar]=2",
    { "foo" => { "bar" => [ {"baz" => 1}, {"baz" => "2"}  ] } } => "foo[bar][][baz]=1&foo[bar][][baz]=2",
    { "foo" => {"1" => "bar", "2" => "baz"} } => "foo[1]=bar&foo[2]=baz"
  }.each do |hash, params|
    it "should covert hash: #{hash.inspect} to params: #{params.inspect}" do
      hash.to_params.split('&').sort.should == params.split('&').sort
    end
  end

  it 'should not leave a trailing &' do
    { :name => 'Bob', :address => { :street => '111 Ruby Ave.', :city => 'Ruby Central', :phones => ['111-111-1111', '222-222-2222'] } }.to_params.should_not match(/&$/)
  end
  
  it 'should URL encode unsafe characters' do
    {:q => "?&\" +"}.to_params.should == "q=%3F%26%22%20%2B"
  end
  
end

Version data entries

22 entries across 22 versions & 9 rubygems

Version Path
alexvollmer-httparty-0.3.1 spec/hash_spec.rb
alexvollmer-httparty-0.4.3 spec/hash_spec.rb
cluon-httparty-0.4.3 spec/hash_spec.rb
jnunemaker-httparty-0.3.1 spec/hash_spec.rb
jnunemaker-httparty-0.4.0 spec/hash_spec.rb
jnunemaker-httparty-0.4.1 spec/hash_spec.rb
jnunemaker-httparty-0.4.2 spec/hash_spec.rb
jnunemaker-httparty-0.4.3 spec/hash_spec.rb
kerryb-httparty-0.3.2 spec/hash_spec.rb
kerryb-httparty-0.3.3 spec/hash_spec.rb
kerryb-httparty-0.4.3 spec/hash_spec.rb
leh-httparty-0.3.1.1 spec/hash_spec.rb
mfilej-httparty-0.4.3 spec/hash_spec.rb
timriley-httparty-0.3.1 spec/hash_spec.rb
timriley-httparty-0.3.2 spec/hash_spec.rb
voxdolo-httparty-0.3.1 spec/hash_spec.rb
voxdolo-httparty-0.4.1 spec/hash_spec.rb
httparty-0.4.0 spec/hash_spec.rb
httparty-0.3.1 spec/hash_spec.rb
httparty-0.4.1 spec/hash_spec.rb