github.com/stripe/stripe-go/v76@v76.25.0/file_test.go (about)

     1  package stripe
     2  
     3  import (
     4  	"encoding/json"
     5  	"os"
     6  	"testing"
     7  
     8  	assert "github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestFile_UnmarshalJSON(t *testing.T) {
    12  	// Unmarshals from a JSON string
    13  	{
    14  		var v File
    15  		err := json.Unmarshal([]byte(`"file_123"`), &v)
    16  		assert.NoError(t, err)
    17  		assert.Equal(t, "file_123", v.ID)
    18  	}
    19  
    20  	// Unmarshals from a JSON object
    21  	{
    22  		v := File{ID: "file_123"}
    23  		data, err := json.Marshal(&v)
    24  		assert.NoError(t, err)
    25  
    26  		err = json.Unmarshal(data, &v)
    27  		assert.NoError(t, err)
    28  		assert.Equal(t, "file_123", v.ID)
    29  	}
    30  }
    31  
    32  func TestFileParams_GetBody(t *testing.T) {
    33  	f, err := os.Open("file/test_data.pdf")
    34  	if err != nil {
    35  		t.Errorf("Unable to open test file %v\n", err)
    36  	}
    37  
    38  	p := &FileParams{
    39  		FileReader: f,
    40  		Filename:   String(f.Name()),
    41  		FileLinkData: &FileFileLinkDataParams{
    42  			Create: Bool(true),
    43  		},
    44  	}
    45  
    46  	buffer, boundary, err := p.GetBody()
    47  	assert.NoError(t, err)
    48  
    49  	assert.NotEqual(t, 0, buffer.Len())
    50  
    51  	// Copied from the check performed by `multipart.Writer.SetBoundary`. A
    52  	// very basic check that the string we got back indeed looks like a
    53  	// boundary.
    54  	//
    55  	// rfc2046#section-5.1.1
    56  	if len(boundary) < 1 || len(boundary) > 70 {
    57  		t.Errorf("invalid boundary length")
    58  	}
    59  }