Skip to content

Commit

Permalink
Set ContentType when loading objects from disk
Browse files Browse the repository at this point in the history
Closes #531.
  • Loading branch information
fsouza committed Jul 13, 2021
1 parent cd0b800 commit 3c965af
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 29 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ENV CGO_ENABLED=0
RUN go build -o fake-gcs-server

FROM alpine:3.14.0
RUN apk add --no-cache mailcap
COPY --from=builder /code/fake-gcs-server /bin/fake-gcs-server
RUN /bin/fake-gcs-server -h
EXPOSE 4443
Expand Down
1 change: 1 addition & 0 deletions ci/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM alpine:3.14.0

RUN apk add --no-cache mailcap
ADD fake-gcs-server /bin/fake-gcs-server
ENTRYPOINT ["/bin/fake-gcs-server", "-data", "/data"]
12 changes: 7 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io/ioutil"
"log"
"mime"
"os"
"os/signal"
"path/filepath"
Expand Down Expand Up @@ -91,11 +92,12 @@ func objectsFromBucket(localBucketPath, bucketName string) ([]fakestorage.Object
return fmt.Errorf("could not read file %q: %w", path, err)
}
objects = append(objects, fakestorage.Object{
BucketName: bucketName,
Name: objectKey,
Content: fileContent,
Crc32c: checksum.EncodedCrc32cChecksum(fileContent),
Md5Hash: checksum.EncodedMd5Hash(fileContent),
BucketName: bucketName,
Name: objectKey,
ContentType: mime.TypeByExtension(filepath.Ext(path)),
Content: fileContent,
Crc32c: checksum.EncodedCrc32cChecksum(fileContent),
Md5Hash: checksum.EncodedMd5Hash(fileContent),
})
}
return nil
Expand Down
60 changes: 36 additions & 24 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io"
"io/ioutil"
"mime"
"os"
"testing"

Expand All @@ -17,7 +18,10 @@ import (
"github.com/sirupsen/logrus"
)

const testContentType = "text/plain; charset=utf-8"

func TestMain(m *testing.M) {
mime.AddExtensionType(".txt", testContentType)
const emptyBucketDir = "testdata/basic/empty-bucket"
err := ensureEmptyDir(emptyBucketDir)
if err != nil {
Expand All @@ -44,9 +48,10 @@ func TestGenerateObjectsFromFiles(t *testing.T) {
folder: "testdata/basic",
expectedObjects: []fakestorage.Object{
{
BucketName: "sample-bucket",
Name: "some_file.txt",
Content: []byte("Some amazing content to be loaded"),
BucketName: "sample-bucket",
Name: "some_file.txt",
Content: []byte("Some amazing content to be loaded"),
ContentType: testContentType,
},
},
expectedEmptyBuckets: []string{"empty-bucket"},
Expand All @@ -56,19 +61,22 @@ func TestGenerateObjectsFromFiles(t *testing.T) {
folder: "testdata/multi-level",
expectedObjects: []fakestorage.Object{
{
BucketName: "some-bucket",
Name: "a/b/c/d/e/f/object1.txt",
Content: []byte("this is object 1\n"),
BucketName: "some-bucket",
Name: "a/b/c/d/e/f/object1.txt",
Content: []byte("this is object 1\n"),
ContentType: testContentType,
},
{
BucketName: "some-bucket",
Name: "a/b/c/d/e/f/object2.txt",
Content: []byte("this is object 2\n"),
BucketName: "some-bucket",
Name: "a/b/c/d/e/f/object2.txt",
Content: []byte("this is object 2\n"),
ContentType: testContentType,
},
{
BucketName: "some-bucket",
Name: "root-object.txt",
Content: []byte("r00t\n"),
BucketName: "some-bucket",
Name: "root-object.txt",
Content: []byte("r00t\n"),
ContentType: testContentType,
},
},
},
Expand All @@ -85,24 +93,28 @@ func TestGenerateObjectsFromFiles(t *testing.T) {
folder: "testdata/chaos",
expectedObjects: []fakestorage.Object{
{
BucketName: "bucket1",
Name: "object1.txt",
Content: []byte("object 1\n"),
BucketName: "bucket1",
Name: "object1.txt",
Content: []byte("object 1\n"),
ContentType: testContentType,
},
{
BucketName: "bucket1",
Name: "object2.txt",
Content: []byte("object 2\n"),
BucketName: "bucket1",
Name: "object2.txt",
Content: []byte("object 2\n"),
ContentType: testContentType,
},
{
BucketName: "bucket2",
Name: "object1.txt",
Content: []byte("object 1\n"),
BucketName: "bucket2",
Name: "object1.txt",
Content: []byte("object 1\n"),
ContentType: testContentType,
},
{
BucketName: "bucket2",
Name: "object2.txt",
Content: []byte("object 2\n"),
BucketName: "bucket2",
Name: "object2.txt",
Content: []byte("object 2\n"),
ContentType: testContentType,
},
},
},
Expand Down

0 comments on commit 3c965af

Please sign in to comment.