[GH-ISSUE #52] Content type not preserved #2161

Closed
opened 2026-03-15 17:48:58 +03:00 by kerem · 2 comments
Owner

Originally created by @tschaub on GitHub (Jul 30, 2019).
Original GitHub issue: https://github.com/fsouza/fake-gcs-server/issues/52

Thank you for your work on this module - it has made testing with GCS a breeze.

It looks to me like the content type of my objects is not getting preserved. Here is a function that tries to set the content type for a new object and then to get the same from the objects attrs:

func checkContentType() {
	bucketName := "test-bucket"
	server := fakestorage.NewServer(nil)
	server.CreateBucket(bucketName)
	client := server.Client()
	defer client.Close()

	bucket := client.Bucket(bucketName)
	object := bucket.Object("test-object")
	writer := object.NewWriter(context.Background())
	defer writer.Close()

	// trying to set content type
	writer.ContentType = "text/html"
	
	reader := strings.NewReader("<body>content</body>")
	if _, err := io.Copy(writer, reader); err != nil {
		log.Fatalln(err)
	}
	if err := writer.Close(); err != nil {
		log.Fatalln(err)
	}

	attrs, err := object.Attrs(context.Background())
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Printf("content-type: %s\n", attrs.ContentType)
}

I'd expect that to print content-type: text/html. Instead it prints content-type: .

Let me know if this looks like a bug. I'd be happy to take a pass at a patch.

Originally created by @tschaub on GitHub (Jul 30, 2019). Original GitHub issue: https://github.com/fsouza/fake-gcs-server/issues/52 Thank you for your work on this module - it has made testing with GCS a breeze. It looks to me like the content type of my objects is not getting preserved. Here is a function that tries to set the content type for a new object and then to get the same from the objects attrs: ```go func checkContentType() { bucketName := "test-bucket" server := fakestorage.NewServer(nil) server.CreateBucket(bucketName) client := server.Client() defer client.Close() bucket := client.Bucket(bucketName) object := bucket.Object("test-object") writer := object.NewWriter(context.Background()) defer writer.Close() // trying to set content type writer.ContentType = "text/html" reader := strings.NewReader("<body>content</body>") if _, err := io.Copy(writer, reader); err != nil { log.Fatalln(err) } if err := writer.Close(); err != nil { log.Fatalln(err) } attrs, err := object.Attrs(context.Background()) if err != nil { log.Fatalln(err) } fmt.Printf("content-type: %s\n", attrs.ContentType) } ``` I'd expect that to print `content-type: text/html`. Instead it prints `content-type: `. Let me know if this looks like a bug. I'd be happy to take a pass at a patch.
kerem closed this issue 2026-03-15 17:49:03 +03:00
Author
Owner

@fsouza commented on GitHub (Jul 31, 2019):

Hey @tschaub, thanks for reporting this.

Currently fake-gcs-server never sets the content-type. I can look into this before the end of the week, or if you want to take a stab with a PR, the change is unfortunately larger than I'd hope, but we need to add a ContentType field to the fake object, then make sure we set it when creating the object from uploads (there are actually 3 upload methods: simple upload, resumable upload and multipart upload - checkout the insertObject method). Finally, we need to set the Content-Type header on getObject

Again, I can definitely get to this before the end of the week, but I'm also happy to review a PR.

<!-- gh-comment-id:516659039 --> @fsouza commented on GitHub (Jul 31, 2019): Hey @tschaub, thanks for reporting this. Currently fake-gcs-server never sets the content-type. I can look into this before the end of the week, or if you want to take a stab with a PR, the change is unfortunately larger than I'd hope, but we need to add a ContentType field to the [fake object](https://godoc.org/github.com/fsouza/fake-gcs-server/fakestorage#Object), then make sure we set it when creating the object from uploads (there are actually 3 upload methods: simple upload, resumable upload and multipart upload - checkout the [``insertObject``](https://github.com/fsouza/fake-gcs-server/blob/882070f5d5ebb268bf4efda8aba65a9094795be8/fakestorage/upload.go#L37-L56) method). Finally, we need to set the Content-Type header on [``getObject``](https://github.com/fsouza/fake-gcs-server/blob/882070f5d5ebb268bf4efda8aba65a9094795be8/fakestorage/object.go#L148-L160) Again, I can definitely get to this before the end of the week, but I'm also happy to review a PR.
Author
Owner

@tschaub commented on GitHub (Aug 7, 2019):

Thanks for the fix @fsouza! This is working well in the v1.11.1 release.

<!-- gh-comment-id:519295048 --> @tschaub commented on GitHub (Aug 7, 2019): Thanks for the fix @fsouza! This is working well in the v1.11.1 release.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/fake-gcs-server#2161
No description provided.