github.com/stripe/stripe-go/v76@v76.25.0/source_test.go (about) 1 package stripe 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 assert "github.com/stretchr/testify/require" 8 ) 9 10 func TestSource_UnmarshalJSON(t *testing.T) { 11 { 12 // We build the JSON object manually here because it's key that the 13 // `object` field is included so that the source knows what type to 14 // decode 15 data := []byte(`{"id":"src_123", "type":"ach_debit", "ach_debit":{"bank_name":"bar"}}`) 16 17 var v Source 18 err := json.Unmarshal(data, &v) 19 assert.NoError(t, err) 20 assert.Equal(t, "src_123", v.ID) 21 assert.Equal(t, "ach_debit", v.Type) 22 23 assert.Equal(t, "bar", v.ACHDebit.BankName) 24 } 25 26 // Test a degenerate case without a type key (this shouldn't happen, but 27 // also shouldn't crash) 28 { 29 data := []byte(`{"id":"src_123", "type":"ach_debit"}`) 30 31 var v Source 32 err := json.Unmarshal(data, &v) 33 assert.NoError(t, err) 34 assert.Equal(t, "src_123", v.ID) 35 assert.Equal(t, "ach_debit", v.Type) 36 } 37 }