github.com/blend/go-sdk@v1.20220411.3/r2/opt_posted_files_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package r2
     9  
    10  import (
    11  	"testing"
    12  
    13  	"github.com/blend/go-sdk/assert"
    14  	"github.com/blend/go-sdk/webutil"
    15  )
    16  
    17  func TestOptPostedFiles(t *testing.T) {
    18  	its := assert.New(t)
    19  
    20  	r := New(TestURL, OptPostedFiles(
    21  		webutil.PostedFile{
    22  			Key:      "form-key",
    23  			FileName: "file.txt",
    24  			Contents: []byte("this is a test"),
    25  		},
    26  		webutil.PostedFile{
    27  			Key:      "form-key-2",
    28  			FileName: "file2.txt",
    29  			Contents: []byte("this is a test2"),
    30  		},
    31  	))
    32  	its.NotNil(r.Request.Body)
    33  
    34  	files, err := webutil.PostedFiles(r.Request)
    35  	its.Nil(err)
    36  	its.Len(files, 2)
    37  
    38  	its.AnyCount(files, 1, func(v interface{}) bool {
    39  		file := v.(webutil.PostedFile)
    40  		return file.Key == "form-key" &&
    41  			file.FileName == "file.txt" &&
    42  			string(file.Contents) == "this is a test"
    43  	})
    44  
    45  	its.AnyCount(files, 1, func(v interface{}) bool {
    46  		file := v.(webutil.PostedFile)
    47  		return file.Key == "form-key-2" &&
    48  			file.FileName == "file2.txt" &&
    49  			string(file.Contents) == "this is a test2"
    50  	})
    51  }