github.com/gospider007/requests@v0.0.0-20240506025355-c73d46169a23/test/request/file_test.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/gospider007/requests"
     9  )
    10  
    11  func TestSendFileWithReader(t *testing.T) {
    12  	resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{
    13  		Form: map[string]any{
    14  			"file": requests.File{
    15  				Content:     bytes.NewBuffer([]byte("test")), //support: io.Reader, string, []byte
    16  				FileName:    "test.txt",
    17  				ContentType: "text/plain",
    18  			},
    19  		},
    20  	})
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  	jsonData, err := resp.Json()
    25  	if err != nil {
    26  		t.Fatal(err)
    27  	}
    28  	if !strings.HasPrefix(jsonData.Get("headers.Content-Type").String(), "multipart/form-data") {
    29  		t.Fatal("json data error")
    30  	}
    31  	if jsonData.Get("files.file").String() != "test" {
    32  		t.Fatal("json data error")
    33  	}
    34  }