braintree 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. data/lib/braintree.rb +36 -46
  2. data/lib/braintree/address.rb +14 -14
  3. data/lib/braintree/base_module.rb +3 -3
  4. data/lib/braintree/configuration.rb +16 -16
  5. data/lib/braintree/credit_card.rb +39 -30
  6. data/lib/braintree/credit_card_verification.rb +4 -3
  7. data/lib/braintree/customer.rb +22 -22
  8. data/lib/braintree/digest.rb +2 -8
  9. data/lib/braintree/error_codes.rb +16 -3
  10. data/lib/braintree/error_result.rb +5 -5
  11. data/lib/braintree/exceptions.rb +58 -0
  12. data/lib/braintree/http.rb +7 -7
  13. data/lib/braintree/paged_collection.rb +14 -14
  14. data/lib/braintree/ssl_expiration_check.rb +5 -1
  15. data/lib/braintree/subscription.rb +110 -0
  16. data/lib/braintree/successful_result.rb +3 -3
  17. data/lib/braintree/test/credit_card_numbers.rb +1 -1
  18. data/lib/braintree/test/transaction_amounts.rb +18 -0
  19. data/lib/braintree/transaction.rb +52 -25
  20. data/lib/braintree/transaction/address_details.rb +2 -2
  21. data/lib/braintree/transaction/credit_card_details.rb +12 -4
  22. data/lib/braintree/transaction/customer_details.rb +9 -1
  23. data/lib/braintree/transaction/status_details.rb +1 -1
  24. data/lib/braintree/transparent_redirect.rb +15 -15
  25. data/lib/braintree/util.rb +7 -7
  26. data/lib/braintree/validation_error.rb +1 -1
  27. data/lib/braintree/validation_error_collection.rb +6 -6
  28. data/lib/braintree/version.rb +3 -3
  29. data/lib/braintree/xml/generator.rb +2 -2
  30. data/lib/braintree/xml/libxml.rb +1 -1
  31. data/lib/braintree/xml/parser.rb +1 -1
  32. data/spec/integration/braintree/address_spec.rb +12 -12
  33. data/spec/integration/braintree/credit_card_spec.rb +189 -37
  34. data/spec/integration/braintree/customer_spec.rb +35 -35
  35. data/spec/integration/braintree/http_spec.rb +3 -3
  36. data/spec/integration/braintree/subscription_spec.rb +362 -0
  37. data/spec/integration/braintree/transaction_spec.rb +130 -58
  38. data/spec/integration/spec_helper.rb +1 -1
  39. data/spec/spec_helper.rb +4 -4
  40. data/spec/unit/braintree/address_spec.rb +6 -6
  41. data/spec/unit/braintree/base_module_spec.rb +18 -0
  42. data/spec/unit/braintree/configuration_spec.rb +10 -10
  43. data/spec/unit/braintree/credit_card_spec.rb +15 -15
  44. data/spec/unit/braintree/credit_card_verification_spec.rb +4 -2
  45. data/spec/unit/braintree/customer_spec.rb +8 -8
  46. data/spec/unit/braintree/digest_spec.rb +4 -0
  47. data/spec/unit/braintree/http_spec.rb +2 -2
  48. data/spec/unit/braintree/paged_collection_spec.rb +12 -12
  49. data/spec/unit/braintree/ssl_expiration_check_spec.rb +18 -9
  50. data/spec/unit/braintree/transaction/credit_card_details_spec.rb +15 -0
  51. data/spec/unit/braintree/transaction/customer_details_spec.rb +19 -0
  52. data/spec/unit/braintree/transaction_spec.rb +14 -14
  53. data/spec/unit/braintree/transparent_redirect_spec.rb +11 -11
  54. data/spec/unit/braintree/util_spec.rb +8 -8
  55. data/spec/unit/braintree/validation_error_collection_spec.rb +6 -6
  56. data/spec/unit/braintree/validation_error_spec.rb +2 -2
  57. data/spec/unit/braintree/xml/libxml_spec.rb +5 -5
  58. data/spec/unit/braintree/xml_spec.rb +16 -16
  59. data/spec/unit/braintree_spec.rb +11 -0
  60. metadata +8 -2
@@ -56,23 +56,32 @@ describe Braintree::SSLExpirationCheck do
56
56
 
57
57
  # We assume that testing logging for one is good enough for all, so we won't duplicate those tests from above
58
58
  it "checks the sandbox cert" do
59
- Braintree::SSLExpirationCheck.stub(:sandbox_expiration_date).and_return(Date.today)
60
- output = StringIO.new
61
- Braintree::Configuration.logger = Logger.new(output)
62
- Braintree::Configuration.logger.level = Logger::WARN
59
+ Braintree::SSLExpirationCheck.stub(:sandbox_expiration_date).and_return(Date.today)
60
+ output = StringIO.new
61
+ Braintree::Configuration.logger = Logger.new(output)
62
+ Braintree::Configuration.logger.level = Logger::WARN
63
63
 
64
- Braintree::SSLExpirationCheck.check_dates
64
+ Braintree::SSLExpirationCheck.check_dates
65
65
 
66
- output.string.should match(/\[Braintree\] The SSL Certificate for the Sandbox environment will expire on \d{4}-\d{2}-\d{2}\. Please check for an updated client library\./)
66
+ output.string.should match(/\[Braintree\] The SSL Certificate for the Sandbox environment will expire on \d{4}-\d{2}-\d{2}\. Please check for an updated client library\./)
67
67
  end
68
68
 
69
- xit "Patrick -- waiting on a production box -- checks the production server cert"
69
+ it "checks the production server cert" do
70
+ Braintree::SSLExpirationCheck.stub(:production_expiration_date).and_return(Date.today)
71
+ output = StringIO.new
72
+ Braintree::Configuration.logger = Logger.new(output)
73
+ Braintree::Configuration.logger.level = Logger::WARN
74
+
75
+ Braintree::SSLExpirationCheck.check_dates
76
+
77
+ output.string.should match(/\[Braintree\] The SSL Certificate for the Production environment will expire on \d{4}-\d{2}-\d{2}\. Please check for an updated client library\./)
78
+ end
70
79
  end
71
80
 
72
81
  describe "production_expiration_date" do
73
- xit "Patrick -- waiting on a production box -- is the date the production cert expires" do
82
+ it "is the date the production cert expires" do
74
83
  Braintree::SSLExpirationCheck.production_expiration_date.should be_a(Date)
75
- Braintree::SSLExpirationCheck.qa_expiration_date.should == fetch_expiration_date(PRODUCTION)
84
+ Braintree::SSLExpirationCheck.production_expiration_date.should == fetch_expiration_date(PRODUCTION)
76
85
  end
77
86
  end
78
87
 
@@ -11,6 +11,21 @@ describe Braintree::Transaction::CreditCardDetails do
11
11
  end
12
12
  end
13
13
 
14
+ describe "inspect" do
15
+ it "inspects" do
16
+ details = Braintree::Transaction::CreditCardDetails.new(
17
+ :bin => "123456",
18
+ :card_type => "Visa",
19
+ :expiration_month => "05",
20
+ :expiration_year => "2012",
21
+ :last_4 => "6789",
22
+ :token => "token",
23
+ :customer_location => "US"
24
+ )
25
+ details.inspect.should == %(#<token: "token", bin: "123456", last_4: "6789", card_type: "Visa", expiration_date: "05/2012", customer_location: "US">)
26
+ end
27
+ end
28
+
14
29
  describe "masked_number" do
15
30
  it "concatenates the bin, some *'s, and the last_4" do
16
31
  details = Braintree::Transaction::CreditCardDetails.new(
@@ -0,0 +1,19 @@
1
+ require File.dirname(__FILE__) + "/../../spec_helper"
2
+
3
+ describe Braintree::Transaction::CustomerDetails do
4
+ describe "inspect" do
5
+ it "inspects" do
6
+ details = Braintree::Transaction::CustomerDetails.new(
7
+ :id => "id",
8
+ :first_name => "Amy",
9
+ :last_name => "Smith",
10
+ :email => "amy.smith@example.com",
11
+ :company => "Smith Co.",
12
+ :website => "http://www.example.com",
13
+ :phone => "6145551234",
14
+ :fax => "3125551234"
15
+ )
16
+ details.inspect.should == %(#<id: "id", first_name: "Amy", last_name: "Smith", email: "amy.smith@example.com", company: "Smith Co.", website: "http://www.example.com", phone: "6145551234", fax: "3125551234">)
17
+ end
18
+ end
19
+ end
@@ -30,7 +30,7 @@ describe Braintree::Transaction do
30
30
  end.to raise_error(ArgumentError, "transaction_id is invalid")
31
31
  end
32
32
  end
33
-
33
+
34
34
  describe "initialize" do
35
35
  it "sets up customer attributes in customer_details" do
36
36
  transaction = Braintree::Transaction._new(
@@ -54,7 +54,7 @@ describe Braintree::Transaction do
54
54
  transaction.customer_details.phone.should == "1-999-652-4189 x56883"
55
55
  transaction.customer_details.fax.should == "012-161-8055"
56
56
  end
57
-
57
+
58
58
  it "sets up credit card attributes in credit_card_details" do
59
59
  transaction = Braintree::Transaction._new(
60
60
  :credit_card => {
@@ -64,7 +64,7 @@ describe Braintree::Transaction do
64
64
  :card_type => "Visa",
65
65
  :expiration_month => "08",
66
66
  :expiration_year => "2009",
67
- :issuer_location => "US"
67
+ :customer_location => "US"
68
68
  }
69
69
  )
70
70
  transaction.credit_card_details.token.should == "mzg2"
@@ -73,14 +73,14 @@ describe Braintree::Transaction do
73
73
  transaction.credit_card_details.card_type.should == "Visa"
74
74
  transaction.credit_card_details.expiration_month.should == "08"
75
75
  transaction.credit_card_details.expiration_year.should == "2009"
76
- transaction.credit_card_details.issuer_location.should == "US"
76
+ transaction.credit_card_details.customer_location.should == "US"
77
77
  end
78
78
 
79
79
  it "sets up history attributes in status_history" do
80
80
  time = Time.utc(2010,1,14)
81
81
  transaction = Braintree::Transaction._new(
82
82
  :status_history => [
83
- { :timestamp => time, :amount => "12.00", :transaction_source => "API",
83
+ { :timestamp => time, :amount => "12.00", :transaction_source => "API",
84
84
  :user => "larry", :status => "authorized" },
85
85
  { :timestamp => Time.utc(2010,1,15), :amount => "12.00", :transaction_source => "API",
86
86
  :user => "curly", :status => "scheduled_for_settlement"}
@@ -100,7 +100,7 @@ describe Braintree::Transaction do
100
100
  )
101
101
  end
102
102
  end
103
-
103
+
104
104
  describe "inspect" do
105
105
  it "includes the id, type, amount, and status first" do
106
106
  transaction = Braintree::Transaction._new(
@@ -110,28 +110,28 @@ describe Braintree::Transaction do
110
110
  :status => "authorized"
111
111
  )
112
112
  output = transaction.inspect
113
- output.should include(%Q(#<Braintree::Transaction id: "1234", type: "sale", amount: "100.00", status: "authorized"))
113
+ output.should include(%Q(#<Braintree::Transaction id: "1234", type: "sale", amount: "100.0", status: "authorized"))
114
114
  end
115
115
  end
116
-
116
+
117
117
  describe "==" do
118
118
  it "returns true when it should" do
119
119
  first = Braintree::Transaction._new(:id => 123)
120
120
  second = Braintree::Transaction._new(:id => 123)
121
-
121
+
122
122
  first.should == second
123
123
  second.should == first
124
124
  end
125
-
125
+
126
126
  it "returns false when it should" do
127
127
  first = Braintree::Transaction._new(:id => 123)
128
128
  second = Braintree::Transaction._new(:id => 124)
129
-
129
+
130
130
  first.should_not == second
131
- second.should_not == first
131
+ second.should_not == first
132
132
  end
133
133
  end
134
-
134
+
135
135
  describe "new" do
136
136
  it "is protected" do
137
137
  expect do
@@ -139,7 +139,7 @@ describe Braintree::Transaction do
139
139
  end.to raise_error(NoMethodError, /protected method .new/)
140
140
  end
141
141
  end
142
-
142
+
143
143
  describe "refunded?" do
144
144
  it "is true if the transaciton has been refunded" do
145
145
  transaction = Braintree::Transaction._new(:refund_id => "123")
@@ -25,22 +25,22 @@ describe Braintree::TransparentRedirect do
25
25
  it "returns the parsed query string params if the hash is valid" do
26
26
  query_string_without_hash = "one=1&two=2&http_status=200"
27
27
  hash = Braintree::Digest.hexdigest(query_string_without_hash)
28
-
28
+
29
29
  query_string_with_hash = "#{query_string_without_hash}&hash=#{hash}"
30
30
  result = Braintree::TransparentRedirect.parse_and_validate_query_string query_string_with_hash
31
31
  result.should == {:one => "1", :two => "2", :http_status => "200", :hash => hash}
32
32
  end
33
-
33
+
34
34
  it "raises Braintree::ForgedQueryString if the hash param is not valid" do
35
35
  query_string_without_hash = "one=1&two=2"
36
36
  hash = Digest::SHA1.hexdigest("invalid#{query_string_without_hash}")
37
-
37
+
38
38
  query_string_with_hash = "#{query_string_without_hash}&hash=#{hash}"
39
39
  expect do
40
40
  Braintree::TransparentRedirect.parse_and_validate_query_string query_string_with_hash
41
41
  end.to raise_error(Braintree::ForgedQueryString)
42
42
  end
43
-
43
+
44
44
  it "raises Braintree::ForgedQueryString if hash is missing from the query string" do
45
45
  expect do
46
46
  Braintree::TransparentRedirect.parse_and_validate_query_string "query_string=without_a_hash"
@@ -58,7 +58,7 @@ describe Braintree::TransparentRedirect do
58
58
  Braintree::TransparentRedirect.parse_and_validate_query_string add_hash_to_query_string("http_status=403")
59
59
  end.to raise_error(Braintree::AuthorizationError)
60
60
  end
61
-
61
+
62
62
  it "raises a ServerError if the server 500's" do
63
63
  expect do
64
64
  Braintree::TransparentRedirect.parse_and_validate_query_string add_hash_to_query_string("http_status=500")
@@ -70,7 +70,7 @@ describe Braintree::TransparentRedirect do
70
70
  Braintree::TransparentRedirect.parse_and_validate_query_string add_hash_to_query_string("http_status=503")
71
71
  end.to raise_error(Braintree::DownForMaintenanceError)
72
72
  end
73
-
73
+
74
74
  it "raises an UnexpectedError if some other code is returned" do
75
75
  expect do
76
76
  Braintree::TransparentRedirect.parse_and_validate_query_string add_hash_to_query_string("http_status=600")
@@ -86,7 +86,7 @@ describe Braintree::TransparentRedirect do
86
86
  )
87
87
  end.to raise_error(ArgumentError, "invalid keys: transaction[invalid_key]")
88
88
  end
89
-
89
+
90
90
  it "raises an exception if not given a type" do
91
91
  expect do
92
92
  Braintree::TransparentRedirect.transaction_data(
@@ -95,7 +95,7 @@ describe Braintree::TransparentRedirect do
95
95
  )
96
96
  end.to raise_error(ArgumentError, "expected transaction[type] of sale or credit, was: nil")
97
97
  end
98
-
98
+
99
99
  it "raises an exception if not given a type of sale or credit" do
100
100
  expect do
101
101
  Braintree::TransparentRedirect.transaction_data(
@@ -114,14 +114,14 @@ describe Braintree::TransparentRedirect do
114
114
  )
115
115
  end.to raise_error(ArgumentError, "invalid keys: credit_card[invalid_key]")
116
116
  end
117
-
117
+
118
118
  it "raises an exception if not given a payment_method_token" do
119
119
  expect do
120
120
  Braintree::TransparentRedirect.update_credit_card_data({})
121
121
  end.to raise_error(ArgumentError, "expected params to contain :payment_method_token of payment method to update")
122
122
  end
123
123
  end
124
-
124
+
125
125
  describe "self.update_customer_data" do
126
126
  it "raises an exception if any keys are invalid" do
127
127
  expect do
@@ -145,7 +145,7 @@ describe Braintree::TransparentRedirect do
145
145
  end.to raise_error(ArgumentError, "expected params to contain :redirect_url")
146
146
  end
147
147
  end
148
-
148
+
149
149
  def add_hash_to_query_string(query_string_without_hash)
150
150
  hash = Braintree::TransparentRedirect._hash(query_string_without_hash)
151
151
  query_string_without_hash + "&hash=" + hash
@@ -13,7 +13,7 @@ describe Braintree::Util do
13
13
  Braintree::Util.verify_keys([:allowed], :allowed => "ok", :disallowed => "bad", "also_invalid" => true)
14
14
  end.to raise_error(ArgumentError, "invalid keys: also_invalid, disallowed")
15
15
  end
16
-
16
+
17
17
  it "raises an exception if a nested hash contains an invalid key" do
18
18
  expect do
19
19
  Braintree::Util.verify_keys(
@@ -53,7 +53,7 @@ describe Braintree::Util do
53
53
  }
54
54
  )
55
55
  end.to raise_error(ArgumentError, "invalid keys: allowed[custom_fields][bad_nesting]")
56
- end
56
+ end
57
57
 
58
58
  it "raises an exception if a deeply nested hash contains an invalid key" do
59
59
  expect do
@@ -74,7 +74,7 @@ describe Braintree::Util do
74
74
  end.to raise_error(ArgumentError, "invalid keys: nested[deeply_allowed][real_deep_invalid], nested[nested_invalid], top_level_invalid")
75
75
  end
76
76
  end
77
-
77
+
78
78
  describe "self._flatten_hash_keys" do
79
79
  it "flattens hash keys" do
80
80
  Braintree::Util._flatten_hash_keys(:nested => {
@@ -124,13 +124,13 @@ describe Braintree::Util do
124
124
  hash = {:foo => {:key_one => "value_one", :key_two => "value_two"}}
125
125
  Braintree::Util.hash_to_query_string(hash).should == "foo%5Bkey_one%5D=value_one&foo%5Bkey_two%5D=value_two"
126
126
  end
127
-
127
+
128
128
  it "works for nesting 2 levels deep" do
129
129
  hash = {:foo => {:nested => {:key_one => "value_one", :key_two => "value_two"}}}
130
130
  Braintree::Util.hash_to_query_string(hash).should == "foo%5Bnested%5D%5Bkey_one%5D=value_one&foo%5Bnested%5D%5Bkey_two%5D=value_two"
131
131
  end
132
132
  end
133
-
133
+
134
134
  describe "self.parse_query_string" do
135
135
  it "parses the query string" do
136
136
  query_string = "foo=bar%20baz&hash=a1b2c3"
@@ -150,7 +150,7 @@ describe Braintree::Util do
150
150
  Braintree::Util.raise_exception_for_status_code(403)
151
151
  end.to raise_error(Braintree::AuthorizationError)
152
152
  end
153
-
153
+
154
154
  it "raises a ServerError if the server 500's" do
155
155
  expect do
156
156
  Braintree::Util.raise_exception_for_status_code(500)
@@ -162,7 +162,7 @@ describe Braintree::Util do
162
162
  Braintree::Util.raise_exception_for_status_code(503)
163
163
  end.to raise_error(Braintree::DownForMaintenanceError)
164
164
  end
165
-
165
+
166
166
  it "raises an UnexpectedError if some other code is returned" do
167
167
  expect do
168
168
  Braintree::Util.raise_exception_for_status_code(600)
@@ -170,7 +170,7 @@ describe Braintree::Util do
170
170
  end
171
171
  end
172
172
 
173
-
173
+
174
174
  describe "self.url_encode" do
175
175
  it "url encodes the given text" do
176
176
  Braintree::Util.url_encode("foo?bar").should == "foo%3Fbar"
@@ -1,7 +1,7 @@
1
1
  require File.dirname(__FILE__) + "/../spec_helper"
2
2
 
3
3
  describe Braintree::ValidationErrorCollection do
4
-
4
+
5
5
  describe "initialize" do
6
6
  it "builds an error object given an array of hashes" do
7
7
  hash = {:errors => [{ :attribute => "some model attribute", :code => 1, :message => "bad juju" }]}
@@ -12,7 +12,7 @@ describe Braintree::ValidationErrorCollection do
12
12
  error.message.should == "bad juju"
13
13
  end
14
14
  end
15
-
15
+
16
16
  describe "for" do
17
17
  it "provides access to nested errors" do
18
18
  hash = {
@@ -64,7 +64,7 @@ describe Braintree::ValidationErrorCollection do
64
64
  errors.inspect.should == "#<Braintree::ValidationErrorCollection errors:[(code1) message1], level1:[(code2) message2], level1/level2:[(code3) message3]>"
65
65
  end
66
66
  end
67
-
67
+
68
68
  describe "on" do
69
69
  it "returns an array of errors on the given attribute" do
70
70
  errors = Braintree::ValidationErrorCollection.new(:errors => [
@@ -75,14 +75,14 @@ describe Braintree::ValidationErrorCollection do
75
75
  errors.on("name").size.should == 2
76
76
  errors.on("name").map{ |e| e.code }.should == [1, 2]
77
77
  end
78
-
78
+
79
79
  it "has indifferent access" do
80
80
  errors = Braintree::ValidationErrorCollection.new(:errors => [
81
81
  { :attribute => "name", :code => 3, :message => "is too long" },
82
82
  ])
83
83
  errors.on(:name).size.should == 1
84
84
  errors.on(:name)[0].code.should == 3
85
-
85
+
86
86
  end
87
87
  end
88
88
 
@@ -124,5 +124,5 @@ describe Braintree::ValidationErrorCollection do
124
124
  errors.deep_size.should == 5
125
125
  end
126
126
  end
127
-
127
+
128
128
  end
@@ -3,13 +3,13 @@ require File.dirname(__FILE__) + "/../spec_helper"
3
3
  describe Braintree::ValidationError do
4
4
  describe "initialize" do
5
5
  it "works" do
6
- error = Braintree::ValidationError.new :attribute => "some model attribute", :code => 1, :message => "bad juju"
6
+ error = Braintree::ValidationError.new :attribute => "some model attribute", :code => 1, :message => "bad juju"
7
7
  error.attribute.should == "some model attribute"
8
8
  error.code.should == 1
9
9
  error.message.should == "bad juju"
10
10
  end
11
11
  end
12
-
12
+
13
13
  describe "inspect" do
14
14
  it "is pretty" do
15
15
  error = Braintree::ValidationError.new :attribute => "number", :code => "123456", :message => "Number is bad juju."
@@ -6,7 +6,7 @@ describe Braintree::Xml::Libxml do
6
6
  xml = "<root><foo type=\"integer\">123</foo></root>"
7
7
  Braintree::Xml::Libxml.parse(xml).should == {"root"=>{"foo"=>{"__content__"=>"123", "type"=>"integer"}}}
8
8
  end
9
-
9
+
10
10
  it "works with dashes or underscores" do
11
11
  xml = <<-END
12
12
  <root>
@@ -16,7 +16,7 @@ describe Braintree::Xml::Libxml do
16
16
  END
17
17
  Braintree::Xml::Libxml.parse(xml).should == {"root"=>{"dash-es"=>{}, "under_scores"=>{}}}
18
18
  end
19
-
19
+
20
20
  it "uses nil if nil=true, otherwise uses empty string" do
21
21
  xml = <<-END
22
22
  <root>
@@ -26,7 +26,7 @@ describe Braintree::Xml::Libxml do
26
26
  END
27
27
  Braintree::Xml::Libxml.parse(xml).should == {"root"=>{"a_nil_value"=>{"nil"=>"true"}, "an_empty_string"=>{}}}
28
28
  end
29
-
29
+
30
30
  it "typecasts dates and times" do
31
31
  xml = <<-END
32
32
  <root>
@@ -35,7 +35,7 @@ describe Braintree::Xml::Libxml do
35
35
  END
36
36
  Braintree::Xml::Libxml.parse(xml).should == {"root"=>{"created-at"=>{"__content__"=>"2009-10-28T10:19:49Z", "type"=>"datetime"}}}
37
37
  end
38
-
38
+
39
39
  it "builds an array if type=array" do
40
40
  xml = <<-END
41
41
  <root>
@@ -48,4 +48,4 @@ describe Braintree::Xml::Libxml do
48
48
  Braintree::Xml::Libxml.parse(xml).should == {"root"=>{"customers"=>{"type"=>"array", "customer"=>[{"name"=>{"__content__"=>"Adam"}}, {"name"=>{"__content__"=>"Ben"}}]}}}
49
49
  end
50
50
  end
51
- end
51
+ end
@@ -6,7 +6,7 @@ describe Braintree::Xml do
6
6
  hash = Braintree::Xml.hash_from_xml("<root><foo type=\"integer\">123</foo></root>")
7
7
  hash.should == {:root => {:foo => 123}}
8
8
  end
9
-
9
+
10
10
  it "works with dashes or underscores" do
11
11
  xml = <<-END
12
12
  <root>
@@ -14,11 +14,11 @@ describe Braintree::Xml do
14
14
  <under_scores />
15
15
  </root>
16
16
  END
17
-
17
+
18
18
  hash = Braintree::Xml.hash_from_xml(xml)
19
19
  hash.should == {:root => {:dash_es => "", :under_scores => ""}}
20
20
  end
21
-
21
+
22
22
  it "uses nil if nil=true, otherwise uses empty string" do
23
23
  xml = <<-END
24
24
  <root>
@@ -29,7 +29,7 @@ describe Braintree::Xml do
29
29
  hash = Braintree::Xml.hash_from_xml(xml)
30
30
  hash.should == {:root => {:a_nil_value => nil, :an_empty_string => ""}}
31
31
  end
32
-
32
+
33
33
  it "typecasts dates and times" do
34
34
  hash = Braintree::Xml.hash_from_xml <<-END
35
35
  <root>
@@ -38,7 +38,7 @@ describe Braintree::Xml do
38
38
  END
39
39
  hash.should == {:root => {:created_at => Time.utc(2009, 10, 28, 10, 19, 49)}}
40
40
  end
41
-
41
+
42
42
  it "builds an array if type=array" do
43
43
  hash = Braintree::Xml.hash_from_xml <<-END
44
44
  <root>
@@ -50,7 +50,7 @@ describe Braintree::Xml do
50
50
  END
51
51
  hash.should == {:root => {:customers => [{:name => "Adam"}, {:name => "Ben"}]}}
52
52
  end
53
-
53
+
54
54
  it "turns 1 and true to boolean if type = boolean" do
55
55
  hash = Braintree::Xml.hash_from_xml <<-END
56
56
  <root>
@@ -66,7 +66,7 @@ describe Braintree::Xml do
66
66
  :uncasted_true => "true"
67
67
  }}
68
68
  end
69
-
69
+
70
70
  it "handles values that are arrays of hashes" do
71
71
  hash = Braintree::Xml.hash_from_xml("
72
72
  <container>
@@ -78,45 +78,45 @@ describe Braintree::Xml do
78
78
  hash.should == {:container => {:elem => [{:value => "one"}, {:value => "two"}, {:value => "three"}]}}
79
79
  end
80
80
  end
81
-
81
+
82
82
  describe "self.hash_to_xml" do
83
83
  def verify_to_xml_and_back(hash)
84
84
  Braintree::Xml.hash_from_xml(Braintree::Xml.hash_to_xml(hash)).should == hash
85
85
  end
86
-
86
+
87
87
  it "works for a simple case" do
88
88
  hash = {:root => {:foo => "foo_value", :bar => "bar_value"}}
89
89
  verify_to_xml_and_back hash
90
90
  end
91
-
91
+
92
92
  it "works for arrays" do
93
93
  hash = {:root => {:items => [{:name => "first"}, {:name => "second"}]}}
94
94
  verify_to_xml_and_back hash
95
95
  end
96
-
96
+
97
97
  it "type casts booleans" do
98
98
  hash = {:root => {:string_true => "true", :bool_true => true, :bool_false => false, :string_false => "false"}}
99
99
  verify_to_xml_and_back hash
100
100
  end
101
-
101
+
102
102
  it "type casts time" do
103
103
  hash = {:root => {:a_time => Time.utc(2009, 10, 28, 1, 2, 3), :a_string_that_looks_like_time => "2009-10-28T10:19:49Z"}}
104
104
  verify_to_xml_and_back hash
105
105
  end
106
-
106
+
107
107
  it "can distinguish nil from empty string" do
108
108
  hash = {:root => {:an_empty_string => "", :a_nil_value => nil}}
109
109
  verify_to_xml_and_back hash
110
110
  end
111
-
111
+
112
112
  it "includes the encoding" do
113
113
  xml = Braintree::Xml.hash_to_xml(:root => {:root => "bar"})
114
114
  xml.should include("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
115
115
  end
116
-
116
+
117
117
  it "works for only a root node and a string" do
118
118
  hash = {:id => "123"}
119
119
  verify_to_xml_and_back hash
120
120
  end
121
121
  end
122
- end
122
+ end