github.com/aavshr/aws-sdk-go@v1.41.3/service/s3/customizations_test.go (about)

     1  package s3_test
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/aavshr/aws-sdk-go/aws"
    10  	"github.com/aavshr/aws-sdk-go/awstesting/unit"
    11  	"github.com/aavshr/aws-sdk-go/service/s3"
    12  )
    13  
    14  const (
    15  	metaKeyPrefix = `X-Amz-Meta-`
    16  	utf8KeySuffix = `My-Info`
    17  	utf8Value     = "hello-世界\u0444"
    18  )
    19  
    20  func TestPutObjectMetadataWithUnicode(t *testing.T) {
    21  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    22  		if e, a := utf8Value, r.Header.Get(metaKeyPrefix+utf8KeySuffix); e != a {
    23  			t.Errorf("expected %s, but received %s", e, a)
    24  		}
    25  	}))
    26  	defer server.Close()
    27  
    28  	svc := s3.New(unit.Session, &aws.Config{
    29  		Endpoint:   aws.String(server.URL),
    30  		DisableSSL: aws.Bool(true),
    31  	})
    32  
    33  	_, err := svc.PutObject(&s3.PutObjectInput{
    34  		Bucket: aws.String("my_bucket"),
    35  		Key:    aws.String("my_key"),
    36  		Body:   strings.NewReader(""),
    37  		Metadata: func() map[string]*string {
    38  			v := map[string]*string{}
    39  			v[utf8KeySuffix] = aws.String(utf8Value)
    40  			return v
    41  		}(),
    42  	})
    43  
    44  	if err != nil {
    45  		t.Errorf("expected no error, but received %v", err)
    46  	}
    47  }
    48  
    49  func TestGetObjectMetadataWithUnicode(t *testing.T) {
    50  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    51  		w.Header().Set(metaKeyPrefix+utf8KeySuffix, utf8Value)
    52  	}))
    53  	defer server.Close()
    54  
    55  	svc := s3.New(unit.Session, &aws.Config{
    56  		Endpoint:   aws.String(server.URL),
    57  		DisableSSL: aws.Bool(true),
    58  	})
    59  
    60  	resp, err := svc.GetObject(&s3.GetObjectInput{
    61  		Bucket: aws.String("my_bucket"),
    62  		Key:    aws.String("my_key"),
    63  	})
    64  
    65  	if err != nil {
    66  		t.Errorf("expected no error, but received %v", err)
    67  	}
    68  	resp.Body.Close()
    69  
    70  	if e, a := utf8Value, *resp.Metadata[utf8KeySuffix]; e != a {
    71  		t.Errorf("expected %s, but received %s", e, a)
    72  	}
    73  
    74  }