Skip to content

Commit

Permalink
fix for review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
poornas committed Sep 26, 2017
1 parent 531f7f2 commit ba9737d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 15 deletions.
2 changes: 1 addition & 1 deletion api-put-object-multipart.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (c Client) initiateMultipartUpload(ctx context.Context, bucketName, objectN
urlValues.Set("uploads", "")

// Set ContentType header.
customHeader := opts.getMetadata()
customHeader := opts.Header()

reqMetadata := requestMetadata{
bucketName: bucketName,
Expand Down
2 changes: 1 addition & 1 deletion api-put-object-streaming.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ func (c Client) putObjectDo(ctx context.Context, bucketName, objectName string,
return ObjectInfo{}, err
}
// Set headers.
customHeader := opts.getMetadata()
customHeader := opts.Header()

// Populate request metadata.
reqMetadata := requestMetadata{
Expand Down
28 changes: 15 additions & 13 deletions api-put-object.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,40 +53,42 @@ func (opts PutObjectOptions) getNumThreads() (numThreads int) {
return
}

// getMetaData - constructs the headers from metadata entered by user in
// Header - constructs the headers from metadata entered by user in
// PutObjectOptions struct
func (opts PutObjectOptions) getMetadata() (headers http.Header) {
headers = make(http.Header)
func (opts PutObjectOptions) Header() (header http.Header) {
header = make(http.Header)

if opts.ContentType != "" {
headers["Content-Type"] = []string{opts.ContentType}
header["Content-Type"] = []string{opts.ContentType}
} else {
headers["Content-Type"] = []string{"application/octet-stream"}
header["Content-Type"] = []string{"application/octet-stream"}
}
if opts.ContentEncoding != "" {
headers["Content-Encoding"] = []string{opts.ContentEncoding}
header["Content-Encoding"] = []string{opts.ContentEncoding}
}
if opts.ContentDisposition != "" {
headers["Content-Disposition"] = []string{opts.ContentDisposition}
header["Content-Disposition"] = []string{opts.ContentDisposition}
}
if opts.CacheControl != "" {
headers["Cache-Control"] = []string{opts.CacheControl}
header["Cache-Control"] = []string{opts.CacheControl}
}
if opts.EncryptMaterials != nil {
headers[amzHeaderIV] = []string{opts.EncryptMaterials.GetIV()}
headers[amzHeaderKey] = []string{opts.EncryptMaterials.GetKey()}
headers[amzHeaderMatDesc] = []string{opts.EncryptMaterials.GetDesc()}
header[amzHeaderIV] = []string{opts.EncryptMaterials.GetIV()}
header[amzHeaderKey] = []string{opts.EncryptMaterials.GetKey()}
header[amzHeaderMatDesc] = []string{opts.EncryptMaterials.GetDesc()}
}
for k, v := range opts.UserMetadata {
if !strings.HasPrefix(strings.ToLower(k), "x-amz-meta-") && !isStandardHeader(k) {
headers["X-Amz-Meta-"+k] = []string{v}
header["X-Amz-Meta-"+k] = []string{v}
} else {
headers[k] = []string{v}
header[k] = []string{v}
}
}
return
}

// validate() checks if the UserMetadata map has standard headers or client side
// encryption headers and raises an error if so.
func (opts PutObjectOptions) validate() (err error) {
for k := range opts.UserMetadata {
if isStandardHeader(k) || isCSEHeader(k) {
Expand Down
52 changes: 52 additions & 0 deletions api-put-object_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Minio Go Library for Amazon S3 Compatible Cloud Storage (C) 2017 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package minio

import (
"testing"
)

func TestPutObjectOptionsValidate(t *testing.T) {
testCases := []struct {
metadata map[string]string
shouldPass bool
}{
{map[string]string{"Content-Type": "custom/content-type"}, false},
{map[string]string{"content-type": "custom/content-type"}, false},
{map[string]string{"Content-Encoding": "gzip"}, false},
{map[string]string{"Cache-Control": "blah"}, false},
{map[string]string{"Content-Disposition": "something"}, false},
{map[string]string{"my-custom-header": "blah"}, true},
{map[string]string{"X-Amz-Iv": "blah"}, false},
{map[string]string{"X-Amz-Key": "blah"}, false},
{map[string]string{"X-Amz-Key-prefixed-header": "blah"}, false},
{map[string]string{"custom-X-Amz-Key-middle": "blah"}, true},
{map[string]string{"my-custom-header-X-Amz-Key": "blah"}, true},
{map[string]string{"X-Amz-Matdesc": "blah"}, false},
{map[string]string{"blah-X-Amz-Matdesc": "blah"}, true},
{map[string]string{"X-Amz-MatDesc-suffix": "blah"}, true},
{map[string]string{"x-amz-meta-X-Amz-Iv": "blah"}, false},
{map[string]string{"x-amz-meta-X-Amz-Key": "blah"}, false},
{map[string]string{"x-amz-meta-X-Amz-Matdesc": "blah"}, false},
}
for i, testCase := range testCases {
err := PutObjectOptions{UserMetadata: testCase.metadata}.validate()

if testCase.shouldPass && err != nil {
t.Errorf("Test %d - output did not match with reference results", i+1)
}
}
}

0 comments on commit ba9737d

Please sign in to comment.