github.com/aliyun/aliyun-oss-go-sdk@v3.0.2+incompatible/oss/option_test.go (about)

     1  package oss
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"time"
     7  
     8  	. "gopkg.in/check.v1"
     9  )
    10  
    11  type OssOptionSuite struct{}
    12  
    13  var _ = Suite(&OssOptionSuite{})
    14  
    15  type optionTestCase struct {
    16  	option Option
    17  	key    string
    18  	value  string
    19  }
    20  
    21  var headerTestcases = []optionTestCase{
    22  	{
    23  		option: Meta("User", "baymax"),
    24  		key:    "X-Oss-Meta-User",
    25  		value:  "baymax",
    26  	},
    27  	{
    28  		option: ACL(ACLPrivate),
    29  		key:    "X-Oss-Acl",
    30  		value:  "private",
    31  	},
    32  	{
    33  		option: ContentType("plain/text"),
    34  		key:    "Content-Type",
    35  		value:  "plain/text",
    36  	},
    37  	{
    38  		option: CacheControl("no-cache"),
    39  		key:    "Cache-Control",
    40  		value:  "no-cache",
    41  	},
    42  	{
    43  		option: ContentDisposition("Attachment; filename=example.txt"),
    44  		key:    "Content-Disposition",
    45  		value:  "Attachment; filename=example.txt",
    46  	},
    47  	{
    48  		option: ContentEncoding("gzip"),
    49  		key:    "Content-Encoding",
    50  		value:  "gzip",
    51  	},
    52  	{
    53  		option: Expires(pastDate),
    54  		key:    "Expires",
    55  		value:  pastDate.Format(http.TimeFormat),
    56  	},
    57  	{
    58  		option: Range(0, 9),
    59  		key:    "Range",
    60  		value:  "bytes=0-9",
    61  	},
    62  	{
    63  		option: Origin("localhost"),
    64  		key:    "Origin",
    65  		value:  "localhost",
    66  	},
    67  	{
    68  		option: CopySourceRange(0, 9),
    69  		key:    "X-Oss-Copy-Source-Range",
    70  		value:  "bytes=0-8",
    71  	},
    72  	{
    73  		option: IfModifiedSince(pastDate),
    74  		key:    "If-Modified-Since",
    75  		value:  pastDate.Format(http.TimeFormat),
    76  	},
    77  	{
    78  		option: IfUnmodifiedSince(futureDate),
    79  		key:    "If-Unmodified-Since",
    80  		value:  futureDate.Format(http.TimeFormat),
    81  	},
    82  	{
    83  		option: IfMatch("xyzzy"),
    84  		key:    "If-Match",
    85  		value:  "xyzzy",
    86  	},
    87  	{
    88  		option: IfNoneMatch("xyzzy"),
    89  		key:    "If-None-Match",
    90  		value:  "xyzzy",
    91  	},
    92  	{
    93  		option: CopySource("bucket_name", "object_name"),
    94  		key:    "X-Oss-Copy-Source",
    95  		value:  "/bucket_name/object_name",
    96  	},
    97  	{
    98  		option: CopySourceIfModifiedSince(pastDate),
    99  		key:    "X-Oss-Copy-Source-If-Modified-Since",
   100  		value:  pastDate.Format(http.TimeFormat),
   101  	},
   102  	{
   103  		option: CopySourceIfUnmodifiedSince(futureDate),
   104  		key:    "X-Oss-Copy-Source-If-Unmodified-Since",
   105  		value:  futureDate.Format(http.TimeFormat),
   106  	},
   107  	{
   108  		option: CopySourceIfMatch("xyzzy"),
   109  		key:    "X-Oss-Copy-Source-If-Match",
   110  		value:  "xyzzy",
   111  	},
   112  	{
   113  		option: CopySourceIfNoneMatch("xyzzy"),
   114  		key:    "X-Oss-Copy-Source-If-None-Match",
   115  		value:  "xyzzy",
   116  	},
   117  	{
   118  		option: MetadataDirective(MetaCopy),
   119  		key:    "X-Oss-Metadata-Directive",
   120  		value:  "COPY",
   121  	},
   122  	{
   123  		option: ServerSideEncryption("AES256"),
   124  		key:    "X-Oss-Server-Side-Encryption",
   125  		value:  "AES256",
   126  	},
   127  	{
   128  		option: ObjectACL(ACLPrivate),
   129  		key:    "X-Oss-Object-Acl",
   130  		value:  "private",
   131  	},
   132  	{
   133  		option: ObjectStorageClass(StorageStandard),
   134  		key:    "X-Oss-Storage-Class",
   135  		value:  "Standard",
   136  	},
   137  	{
   138  		option: Callback("JTdCJTIyY2FsbGJhY2tVcmwlMjIlM0ElMjJleGFtcGxlLmNvbS9pbmRleC5odG1sJTIyJTdE"),
   139  		key:    "X-Oss-Callback",
   140  		value:  "JTdCJTIyY2FsbGJhY2tVcmwlMjIlM0ElMjJleGFtcGxlLmNvbS9pbmRleC5odG1sJTIyJTdE",
   141  	},
   142  	{
   143  		option: CallbackVar("JTdCJTIyeCUzQXZhcjElMjIlM0ElMjJ2YWx1ZTElMjIlMkMlMjJ4JTNBdmFyMiUyMiUzQSUyMnZhbHVlMiUyMiU3RA=="),
   144  		key:    "X-Oss-Callback-Var",
   145  		value:  "JTdCJTIyeCUzQXZhcjElMjIlM0ElMjJ2YWx1ZTElMjIlMkMlMjJ4JTNBdmFyMiUyMiUzQSUyMnZhbHVlMiUyMiU3RA==",
   146  	},
   147  	{
   148  		option: ContentLanguage("zh-CN"),
   149  		key:    "Content-Language",
   150  		value:  "zh-CN",
   151  	},
   152  	{
   153  		option: ServerSideEncryptionKeyID("xossekid"),
   154  		key:    "X-Oss-Server-Side-Encryption-Key-Id",
   155  		value:  "xossekid",
   156  	},
   157  }
   158  
   159  func (s *OssOptionSuite) TestHeaderOptions(c *C) {
   160  	for _, testcase := range headerTestcases {
   161  		headers := make(map[string]optionValue)
   162  		err := testcase.option(headers)
   163  		c.Assert(err, IsNil)
   164  
   165  		expected, actual := testcase.value, headers[testcase.key].Value
   166  		c.Assert(expected, Equals, actual)
   167  	}
   168  }
   169  
   170  var paramTestCases = []optionTestCase{
   171  	{
   172  		option: Delimiter("/"),
   173  		key:    "delimiter",
   174  		value:  "/",
   175  	},
   176  	{
   177  		option: Marker("abc"),
   178  		key:    "marker",
   179  		value:  "abc",
   180  	},
   181  	{
   182  		option: MaxKeys(150),
   183  		key:    "max-keys",
   184  		value:  "150",
   185  	},
   186  	{
   187  		option: Prefix("fun"),
   188  		key:    "prefix",
   189  		value:  "fun",
   190  	},
   191  	{
   192  		option: EncodingType("ascii"),
   193  		key:    "encoding-type",
   194  		value:  "ascii",
   195  	},
   196  	{
   197  		option: MaxUploads(100),
   198  		key:    "max-uploads",
   199  		value:  "100",
   200  	},
   201  	{
   202  		option: KeyMarker("abc"),
   203  		key:    "key-marker",
   204  		value:  "abc",
   205  	},
   206  	{
   207  		option: UploadIDMarker("xyz"),
   208  		key:    "upload-id-marker",
   209  		value:  "xyz",
   210  	},
   211  	{
   212  		option: MaxParts(1000),
   213  		key:    "max-parts",
   214  		value:  "1000",
   215  	},
   216  	{
   217  		option: PartNumberMarker(1),
   218  		key:    "part-number-marker",
   219  		value:  "1",
   220  	},
   221  	{
   222  		option: Process("image/format,png"),
   223  		key:    "x-oss-process",
   224  		value:  "image/format,png",
   225  	},
   226  }
   227  
   228  func (s *OssOptionSuite) TestParamOptions(c *C) {
   229  	for _, testcase := range paramTestCases {
   230  		params := make(map[string]optionValue)
   231  		err := testcase.option(params)
   232  		c.Assert(err, IsNil)
   233  
   234  		expected, actual := testcase.value, params[testcase.key].Value
   235  		c.Assert(expected, Equals, actual)
   236  	}
   237  }
   238  
   239  func (s *OssOptionSuite) TestHandleOptions(c *C) {
   240  	headers := make(map[string]string)
   241  	options := []Option{}
   242  
   243  	for _, testcase := range headerTestcases {
   244  		options = append(options, testcase.option)
   245  	}
   246  
   247  	err := handleOptions(headers, options)
   248  	c.Assert(err, IsNil)
   249  
   250  	for _, testcase := range headerTestcases {
   251  		expected, actual := testcase.value, headers[testcase.key]
   252  		c.Assert(expected, Equals, actual)
   253  	}
   254  
   255  	options = []Option{IfMatch(""), nil}
   256  	headers = map[string]string{}
   257  	err = handleOptions(headers, options)
   258  	c.Assert(err, IsNil)
   259  	c.Assert(len(headers), Equals, 1)
   260  }
   261  
   262  func (s *OssOptionSuite) TestHandleParams(c *C) {
   263  	client, err := New(endpoint, accessID, accessKey)
   264  	c.Assert(err, IsNil)
   265  
   266  	options := []Option{}
   267  
   268  	for _, testcase := range paramTestCases {
   269  		options = append(options, testcase.option)
   270  	}
   271  
   272  	params, err := GetRawParams(options)
   273  	c.Assert(err, IsNil)
   274  
   275  	out := client.Conn.getURLParams(params)
   276  	c.Assert(len(out), Equals, 191)
   277  
   278  	options = []Option{KeyMarker(""), nil}
   279  
   280  	params, err = GetRawParams(options)
   281  	c.Assert(err, IsNil)
   282  
   283  	out = client.Conn.getURLParams(params)
   284  	c.Assert(out, Equals, "key-marker")
   285  }
   286  
   287  func (s *OssOptionSuite) TestFindOption(c *C) {
   288  	options := []Option{}
   289  
   290  	for _, testcase := range headerTestcases {
   291  		options = append(options, testcase.option)
   292  	}
   293  
   294  	str, err := FindOption(options, "X-Oss-Acl", "")
   295  	c.Assert(err, IsNil)
   296  	c.Assert(str, Equals, "private")
   297  
   298  	str, err = FindOption(options, "MyProp", "")
   299  	c.Assert(err, IsNil)
   300  	c.Assert(str, Equals, "")
   301  }
   302  
   303  func (s *OssOptionSuite) TestDeleteOption(c *C) {
   304  	options := []Option{VersionId("123"), VersionIdMarker("456"), KeyMarker("789")}
   305  	str, err := FindOption(options, "versionId", "")
   306  	c.Assert(str, Equals, "123")
   307  	c.Assert(err, IsNil)
   308  
   309  	skipOption := DeleteOption(options, "versionId")
   310  	str, err = FindOption(skipOption, "versionId", "")
   311  	c.Assert(str, Equals, "")
   312  
   313  	str, err = FindOption(skipOption, "version-id-marker", "")
   314  	c.Assert(str, Equals, "456")
   315  
   316  	str, err = FindOption(skipOption, "key-marker", "")
   317  	c.Assert(str, Equals, "789")
   318  
   319  }
   320  
   321  func (s *OssOptionSuite) TestWithContext(c *C) {
   322  	ctx := context.Background()
   323  	ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
   324  	defer cancel()
   325  	options := []Option{WithContext(ctx)}
   326  	ctxArg, _ := FindOption(options, contextArg, nil)
   327  
   328  	c.Assert(ctxArg, NotNil)
   329  	c.Assert(ctxArg.(context.Context), Equals, ctx)
   330  
   331  	options = []Option{}
   332  	ctxArg, _ = FindOption(options, contextArg, nil)
   333  	c.Assert(ctxArg, Equals, nil)
   334  	ctxnil, _ := ctxArg.(context.Context)
   335  	c.Assert(ctxnil, Equals, nil)
   336  }