github.com/binbinly/pkg@v0.0.11-0.20240321014439-f4fbf666eb0f/client/http/http_test.go (about)

     1  package http
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestHTTPOption(t *testing.T) {
    11  	options := []ClientOption{
    12  		WithHTTPHeader("Accept-Language", "zh-CN,zh;q=0.9"),
    13  		WithHTTPHeader("Content-Type", "text/xml; charset=utf-8"),
    14  		WithHTTPClose(),
    15  		WithHTTPTimeout(5 * time.Second),
    16  	}
    17  
    18  	settings := &httpSettings{
    19  		headers: make(map[string]string),
    20  		timeout: defaultTimeout,
    21  	}
    22  
    23  	for _, f := range options {
    24  		f(settings)
    25  	}
    26  
    27  	assert.Equal(t, map[string]string{
    28  		"Accept-Language": "zh-CN,zh;q=0.9",
    29  		"Content-Type":    "text/xml; charset=utf-8",
    30  	}, settings.headers)
    31  	assert.True(t, settings.close)
    32  	assert.Equal(t, 5*time.Second, settings.timeout)
    33  }
    34  
    35  func TestUploadOption(t *testing.T) {
    36  	options := []UploadOption{
    37  		WithResourceURL("https://img.test.com/test.jpg"),
    38  		WithExtraField("title", "TITLE"),
    39  		WithExtraField("introduction", "INTRODUCTION"),
    40  	}
    41  
    42  	upload := &httpUpload{extraFields: make(map[string]string)}
    43  
    44  	for _, f := range options {
    45  		f(upload)
    46  	}
    47  
    48  	assert.Equal(t, "https://img.test.com/test.jpg", upload.resourceURL)
    49  	assert.Equal(t, map[string]string{
    50  		"title":        "TITLE",
    51  		"introduction": "INTRODUCTION",
    52  	}, upload.extraFields)
    53  }