|
| 1 | +// Copyright 2016 Google Inc. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package gcsx_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "strings" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/googlecloudplatform/gcsfuse/internal/gcsx" |
| 22 | + "github.com/jacobsa/gcloud/gcs" |
| 23 | + "github.com/jacobsa/gcloud/gcs/gcsfake" |
| 24 | + "github.com/jacobsa/timeutil" |
| 25 | + "golang.org/x/net/context" |
| 26 | +) |
| 27 | + |
| 28 | +var contentTypeBucketTestCases = []struct { |
| 29 | + name string |
| 30 | + request string // ContentType in request |
| 31 | + expected string // Expected final type |
| 32 | +}{ |
| 33 | + ///////////////// |
| 34 | + // No extension |
| 35 | + ///////////////// |
| 36 | + |
| 37 | + 0: { |
| 38 | + name: "foo/bar", |
| 39 | + request: "", |
| 40 | + expected: "", |
| 41 | + }, |
| 42 | + |
| 43 | + 1: { |
| 44 | + name: "foo/bar", |
| 45 | + request: "image/jpeg", |
| 46 | + expected: "image/jpeg", |
| 47 | + }, |
| 48 | + |
| 49 | + ////////////////////// |
| 50 | + // Unknown extension |
| 51 | + ////////////////////// |
| 52 | + |
| 53 | + 2: { |
| 54 | + name: "foo/bar.asdf", |
| 55 | + request: "", |
| 56 | + expected: "", |
| 57 | + }, |
| 58 | + |
| 59 | + 3: { |
| 60 | + name: "foo/bar.asdf", |
| 61 | + request: "image/jpeg", |
| 62 | + expected: "image/jpeg", |
| 63 | + }, |
| 64 | + |
| 65 | + ////////////////////// |
| 66 | + // Known extension |
| 67 | + ////////////////////// |
| 68 | + |
| 69 | + 4: { |
| 70 | + name: "foo/bar.jpg", |
| 71 | + request: "", |
| 72 | + expected: "image/jpeg", |
| 73 | + }, |
| 74 | + |
| 75 | + 5: { |
| 76 | + name: "foo/bar.jpg", |
| 77 | + request: "text/plain", |
| 78 | + expected: "text/plain", |
| 79 | + }, |
| 80 | +} |
| 81 | + |
| 82 | +func TestContentTypeBucket_CreateObject(t *testing.T) { |
| 83 | + for i, tc := range contentTypeBucketTestCases { |
| 84 | + // Set up a bucket. |
| 85 | + bucket := gcsx.NewContentTypeBucket( |
| 86 | + gcsfake.NewFakeBucket(timeutil.RealClock(), "")) |
| 87 | + |
| 88 | + // Create the object. |
| 89 | + req := &gcs.CreateObjectRequest{ |
| 90 | + Name: tc.name, |
| 91 | + ContentType: tc.request, |
| 92 | + Contents: strings.NewReader(""), |
| 93 | + } |
| 94 | + |
| 95 | + o, err := bucket.CreateObject(context.Background(), req) |
| 96 | + if err != nil { |
| 97 | + t.Fatalf("Test case %d: CreateObject: %v", i, err) |
| 98 | + } |
| 99 | + |
| 100 | + // Check the content type. |
| 101 | + if got, want := o.ContentType, tc.expected; got != want { |
| 102 | + t.Errorf("Test case %d: o.ContentType is %q, want %q", i, got, want) |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func TestContentTypeBucket_ComposeObjects(t *testing.T) { |
| 108 | + var err error |
| 109 | + ctx := context.Background() |
| 110 | + |
| 111 | + for i, tc := range contentTypeBucketTestCases { |
| 112 | + // Set up a bucket. |
| 113 | + bucket := gcsx.NewContentTypeBucket( |
| 114 | + gcsfake.NewFakeBucket(timeutil.RealClock(), "")) |
| 115 | + |
| 116 | + // Create a source object. |
| 117 | + const srcName = "some_src" |
| 118 | + _, err = bucket.CreateObject(ctx, &gcs.CreateObjectRequest{ |
| 119 | + Name: srcName, |
| 120 | + Contents: strings.NewReader(""), |
| 121 | + }) |
| 122 | + |
| 123 | + if err != nil { |
| 124 | + t.Fatalf("Test case %d: CreateObject: %v", err) |
| 125 | + } |
| 126 | + |
| 127 | + // Compose. |
| 128 | + req := &gcs.ComposeObjectsRequest{ |
| 129 | + DstName: tc.name, |
| 130 | + ContentType: tc.request, |
| 131 | + Sources: []gcs.ComposeSource{{Name: srcName}}, |
| 132 | + } |
| 133 | + |
| 134 | + o, err := bucket.ComposeObjects(ctx, req) |
| 135 | + if err != nil { |
| 136 | + t.Fatalf("Test case %d: ComposeObject: %v", i, err) |
| 137 | + } |
| 138 | + |
| 139 | + // Check the content type. |
| 140 | + if got, want := o.ContentType, tc.expected; got != want { |
| 141 | + t.Errorf("Test case %d: o.ContentType is %q, want %q", i, got, want) |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments