github.com/stripe/stripe-go/v76@v76.25.0/sourcetransaction_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 TestSourceTransaction_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(`{"type":"ach_credit_transfer", "ach_credit_transfer":{"routing_number":"bar"}}`)
    16  
    17  		var v SourceTransaction
    18  		err := json.Unmarshal(data, &v)
    19  		assert.NoError(t, err)
    20  		assert.Equal(t, "ach_credit_transfer", v.Type)
    21  
    22  		assert.Equal(t, "bar", v.ACHCreditTransfer.RoutingNumber)
    23  	}
    24  
    25  	// Test a degenerate case without a type key (this shouldn't happen, but
    26  	// also shouldn't crash)
    27  	{
    28  		data := []byte(`{"type":"ach"}`)
    29  
    30  		var v SourceTransaction
    31  		err := json.Unmarshal(data, &v)
    32  		assert.NoError(t, err)
    33  		assert.Equal(t, "ach", v.Type)
    34  	}
    35  }