stripe-ruby/test/stripe/invoice_test.rb
Brandur 2bc471d501 Fix encoding of arrays that are sent in query strings
As discussed in #608, we currently have a problem where Faraday
deconstructs a query string that we encode and strips out any of the
array index numbers that we added. It's not too clear on why it does
this, but it appears to be built in at a pretty low level and hard to
change.

I spent a little time on this and it turns out that we can avoid the bad
code by depending on Faraday's `params` accessor on a request instead of
trying to do the encoding ourselves. Webmock has a pretty hard time
detecting the difference, but you can see some before and after encoding
here.

Before:

```
I, [2017-12-06T17:41:23.083942 #36737]  INFO -- : get
http://localhost:12111/v1/invoices/upcoming?customer=cus_123&subscription_items%5B%5D%5Bplan%5D=gold&subscription_items%5B%5D%5Bquantity%5D=2
```

After:

```
I, [2017-12-06T17:42:12.727752 #37158]  INFO -- : get
http://localhost:12111/v1/invoices/upcoming?customer=cus_123&subscription_items%5B0%5D%5Bplan%5D=gold&subscription_items%5B0%5D%5Bquantity%5D=2
```

Honestly, some of this can still use a lot of cleanup: it's weird that
we manually encode parameters ourselves for bodies, but not for queries;
but I think this'll fix the problem for now.

Fixes #608.
2017-12-07 10:51:07 -08:00

112 lines
3.5 KiB
Ruby

require File.expand_path("../../test_helper", __FILE__)
module Stripe
class InvoiceTest < Test::Unit::TestCase
should "be listable" do
invoices = Stripe::Invoice.list
assert_requested :get, "#{Stripe.api_base}/v1/invoices"
assert invoices.data.is_a?(Array)
assert invoices.first.is_a?(Stripe::Invoice)
end
should "be retrievable" do
invoice = Stripe::Invoice.retrieve("in_123")
assert_requested :get, "#{Stripe.api_base}/v1/invoices/in_123"
assert invoice.is_a?(Stripe::Invoice)
end
should "be creatable" do
invoice = Stripe::Invoice.create(
customer: "cus_123"
)
assert_requested :post, "#{Stripe.api_base}/v1/invoices"
assert invoice.is_a?(Stripe::Invoice)
end
should "be saveable" do
invoice = Stripe::Invoice.retrieve("in_123")
invoice.metadata["key"] = "value"
invoice.save
assert_requested :post, "#{Stripe.api_base}/v1/invoices/#{invoice.id}"
end
should "be updateable" do
invoice = Stripe::Invoice.update("in_123", metadata: { key: "value" })
assert_requested :post, "#{Stripe.api_base}/v1/invoices/in_123"
assert invoice.is_a?(Stripe::Invoice)
end
context "#pay" do
should "pay invoice" do
invoice = Stripe::Invoice.retrieve("in_123")
invoice = invoice.pay
assert_requested :post,
"#{Stripe.api_base}/v1/invoices/#{invoice.id}/pay"
assert invoice.is_a?(Stripe::Invoice)
end
should "pay invoice with a specific source" do
invoice = Stripe::Invoice.retrieve("in_123")
invoice = invoice.pay(
source: "src_123"
)
assert_requested :post,
"#{Stripe.api_base}/v1/invoices/#{invoice.id}/pay",
body: {
source: "src_123",
}
assert invoice.is_a?(Stripe::Invoice)
end
end
context "#upcoming" do
should "retrieve upcoming invoices" do
invoice = Stripe::Invoice.upcoming(
customer: "cus_123",
subscription: "sub_123"
)
assert_requested :get, "#{Stripe.api_base}/v1/invoices/upcoming",
query: {
customer: "cus_123",
subscription: "sub_123",
}
assert invoice.is_a?(Stripe::Invoice)
end
should "retrieve upcoming invoices with items" do
items = [{
plan: "gold",
quantity: 2,
},]
invoice = Stripe::Invoice.upcoming(
customer: "cus_123",
subscription_items: items
)
assert_requested :get, "#{Stripe.api_base}/v1/invoices/upcoming",
query: {
customer: "cus_123",
subscription_items: [
{ plan: "gold", quantity: "2" },
],
}
assert invoice.is_a?(Stripe::Invoice)
end
should "be callable with an empty string" do
invoice = Stripe::Invoice.upcoming(
coupon: "",
customer: "cus_123"
)
assert_requested :get, "#{Stripe.api_base}/v1/invoices/upcoming",
query: {
coupon: "",
customer: "cus_123",
}
assert invoice.is_a?(Stripe::Invoice)
end
end
end
end