Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update documentation and tests on updating Content-Type #150

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions swift.go
Original file line number Diff line number Diff line change
Expand Up @@ -2105,10 +2105,10 @@ func (c *Connection) objectBase(container string, objectName string) (info Objec
// X-Delete-At or X-Delete-After for expiring objects.
//
// You cannot use this to change any of the object's other headers
// such as Content-Type, ETag, etc.
// such as ETag, etc.
//
// Refer to copying an object when you need to update metadata or
// other headers such as Content-Type or CORS headers.
// other headers such as CORS headers.
//
// May return ObjectNotFound.
func (c *Connection) ObjectUpdate(container string, objectName string, h Headers) error {
Expand Down Expand Up @@ -2175,11 +2175,12 @@ func (c *Connection) ObjectMove(srcContainer string, srcObjectName string, dstCo
return c.ObjectDelete(srcContainer, srcObjectName)
}

// ObjectUpdateContentType updates the content type of an object
// ObjectUpdateContentType updates the content type of an object while
// keeping current custom metadata.
//
// This is a convenience method which calls ObjectCopy
//
// All other metadata is preserved.
// This is a convenience method which calls ObjectCopy, which causes data movement.
// Content-Type can also be updated with ObjectUpdate which updates metadata
// in place, without need of data movement in the cluster.
func (c *Connection) ObjectUpdateContentType(container string, objectName string, contentType string) (err error) {
h := Headers{"Content-Type": contentType}
_, err = c.ObjectCopy(container, objectName, container, objectName, h)
Expand Down
48 changes: 48 additions & 0 deletions swift_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var (
srv *swifttest.SwiftServer
m1 = swift.Metadata{"Hello": "1", "potato-Salad": "2"}
m2 = swift.Metadata{"hello": "", "potato-salad": ""}
m3 = swift.Metadata{"sweet-potato-salad": "1"}
skipVersionTests = false
)

Expand Down Expand Up @@ -1304,6 +1305,29 @@ func TestObjectUpdate2(t *testing.T) {
compareMaps(t, headers.ObjectMetadata(), map[string]string{"hello": "", "potato-salad": ""})
}

func TestObjectUpdate3(t *testing.T) {
c, rollback := makeConnectionWithObjectHeaders(t)
defer rollback()

// sanity
_, headers, err := c.Object(CONTAINER, OBJECT)
if err != nil {
t.Fatal(err)
}
compareMaps(t, headers.ObjectMetadata(), map[string]string{"hello": "1", "potato-salad": "1"})

// update with new headers
err = c.ObjectUpdate(CONTAINER, OBJECT, m3.ObjectHeaders())
if err != nil {
t.Fatal(err)
}
_, headers, err = c.Object(CONTAINER, OBJECT)
if err != nil {
t.Fatal(err)
}
compareMaps(t, headers.ObjectMetadata(), map[string]string{"sweet-potato-salad": "1"})
}

func TestContainers(t *testing.T) {
c, rollback := makeConnectionWithObjectHeaders(t)
defer rollback()
Expand Down Expand Up @@ -1612,10 +1636,14 @@ func TestObjectMove(t *testing.T) {
func TestObjectUpdateContentType(t *testing.T) {
c, rollback := makeConnectionWithObjectHeaders(t)
defer rollback()

// to maintain backwards-compatibility update content-type with
// ObjectUpdateContentType, which copies object in place
err := c.ObjectUpdateContentType(CONTAINER, OBJECT, "text/potato")
if err != nil {
t.Fatal(err)
}

// Re-read the metadata to see if it is correct
_, headers, err := c.Object(CONTAINER, OBJECT)
if err != nil {
Expand All @@ -1624,7 +1652,27 @@ func TestObjectUpdateContentType(t *testing.T) {
if headers["Content-Type"] != "text/potato" {
t.Error("Didn't change content type")
}

// ObjectUpdateContentType keeps old metadata because copy was used
compareMaps(t, headers.ObjectMetadata(), map[string]string{"hello": "1", "potato-salad": "2"})

// Now update content-type with ObjectUpdate
// Note, old metadata is lost if not sent again
h := swift.Headers{"Content-Type": "text/sweet-potato", "X-Object-Meta-Sweet-Potato-Salad": "1"}
err = c.ObjectUpdate(CONTAINER, OBJECT, h)
if err != nil {
t.Fatal(err)
}

// Re-read the metadata to see if it is correct
_, headers, err = c.Object(CONTAINER, OBJECT)
if err != nil {
t.Fatal(err)
}
if headers["Content-Type"] != "text/sweet-potato" {
t.Error("Didn't change content type")
}
compareMaps(t, headers.ObjectMetadata(), map[string]string{"sweet-potato-salad": "1"})
}

func TestVersionContainerCreate(t *testing.T) {
Expand Down