aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Majewsky <majewsky@gmx.net>2018-02-04 22:42:31 +0100
committerStefan Majewsky <majewsky@gmx.net>2018-02-04 22:42:31 +0100
commit698cd6aaf4e4fd235d5af904376c89f8faf177f7 (patch)
tree2323352b54762966f5794cc47c5fd540a755a4ee
parent137f2df4bc383058d59cc0b283c41ef11a1eaebd (diff)
downloadgo-schwift-698cd6aaf4e4fd235d5af904376c89f8faf177f7.tar.gz
add tests for metadata
-rw-r--r--account_test.go44
-rw-r--r--headers.go7
-rw-r--r--metadata.go17
-rw-r--r--metadata_test.go91
4 files changed, 152 insertions, 7 deletions
diff --git a/account_test.go b/account_test.go
index da49a8c..76185d4 100644
--- a/account_test.go
+++ b/account_test.go
@@ -38,8 +38,12 @@ func TestAccountBasic(t *testing.T) {
func TestAccountMetadata(t *testing.T) {
testWithAccount(t, func(a *Account) {
+ //test creating some metadata
err := a.Post(AccountHeaders{
- Metadata: NewMetadata("schwift-test", "first"),
+ Metadata: NewMetadata(
+ "schwift-test1", "first",
+ "schwift-test2", "second",
+ ),
}, nil)
if !expectError(t, err, nil) {
t.FailNow()
@@ -49,6 +53,42 @@ func TestAccountMetadata(t *testing.T) {
if !expectError(t, err, nil) {
t.FailNow()
}
- expectString(t, hdr.Metadata.Get("schwift-test"), "first")
+ expectString(t, hdr.Metadata.Get("schwift-test1"), "first")
+ expectString(t, hdr.Metadata.Get("schwift-test2"), "second")
+
+ //test deleting some metadata
+ m := make(Metadata)
+ m.Clear("schwift-test1")
+ err = a.Post(AccountHeaders{
+ Metadata: m,
+ }, nil)
+ if !expectError(t, err, nil) {
+ t.FailNow()
+ }
+
+ hdr, err = a.Headers()
+ if !expectError(t, err, nil) {
+ t.FailNow()
+ }
+ expectString(t, hdr.Metadata.Get("schwift-test1"), "")
+ expectString(t, hdr.Metadata.Get("schwift-test2"), "second")
+
+ //test updating some metadata
+ m = make(Metadata)
+ m.Set("schwift-test2", "changed")
+ err = a.Post(AccountHeaders{
+ Metadata: m,
+ }, nil)
+ if !expectError(t, err, nil) {
+ t.FailNow()
+ }
+
+ hdr, err = a.Headers()
+ if !expectError(t, err, nil) {
+ t.FailNow()
+ }
+ expectString(t, hdr.Metadata.Get("schwift-test1"), "")
+ expectString(t, hdr.Metadata.Get("schwift-test2"), "changed")
+
})
}
diff --git a/headers.go b/headers.go
index eb65488..3473ac0 100644
--- a/headers.go
+++ b/headers.go
@@ -95,7 +95,7 @@ func (f StringField) Set(value string) {
//Clear removes this key from the original AccountHeaders, ContainerHeaders or
//ObjectHeaders instance.
func (f StringField) Clear() {
- f.metadata.Set(f.key, "")
+ f.metadata.Clear(f.key)
}
//UnsignedIntField is a helper type used in the interface of AccountHeaders,
@@ -142,7 +142,7 @@ func (f UnsignedIntField) Set(value uint64) {
//Clear removes this key from the original AccountHeaders, ContainerHeaders or
//ObjectHeaders instance.
func (f UnsignedIntField) Clear() {
- f.metadata.Set(f.key, "")
+ f.metadata.Clear(f.key)
}
////////////////////////////////////////////////////////////////////////////////
@@ -214,9 +214,6 @@ func compileHeaders(headers interface{}, opts *RequestOptions) RequestOptions {
//...for container and account metadata, a key is removed by
//setting its value to the empty string
hdr.Set(info.HeaderName+key, "")
- } else {
- //for object metadata, you just leave out the metadata fields that
- //you want to clear, so we do nothing
}
} else {
//NOTE: The spec says that `value` needs to be percent-encoded, but
diff --git a/metadata.go b/metadata.go
index 3e01c02..5f5f63f 100644
--- a/metadata.go
+++ b/metadata.go
@@ -50,7 +50,24 @@ func NewMetadata(args ...string) Metadata {
return m
}
+//Clear sets the value to this key to the empty string, such that a Post() with
+//this Metadata will remove the existing value from this metadata key on the server.
+func (m Metadata) Clear(key string) {
+ m.Set(key, "")
+}
+
//Del works just like http.Header.Del().
+//
+//Del deletes a key from the Metadata instance. When the Metadata instance
+//is then sent to the server with Post(), Del() has different effects depending
+//on context because of Swift's inconsistent API:
+//
+//For account or container metadata, a key which has been deleted with Del() will
+//remain unchanged on the server. To remove the key on the server, use Clear()
+//instead.
+//
+//For object metadata, deleting a key will cause that key to be deleted on the
+//server. Del() is identical to Clear() in this case.
func (m Metadata) Del(key string) {
k := textproto.CanonicalMIMEHeaderKey(key)
delete(m, k)
diff --git a/metadata_test.go b/metadata_test.go
new file mode 100644
index 0000000..8977747
--- /dev/null
+++ b/metadata_test.go
@@ -0,0 +1,91 @@
+/******************************************************************************
+*
+* Copyright 2018 Stefan Majewsky <majewsky@gmx.net>
+*
+* 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 schwift
+
+import "testing"
+
+func TestMetadata(t *testing.T) {
+ m := NewMetadata(
+ "first", "value1",
+ "second-thing", "value2",
+ )
+
+ expectMetadata(t, m, map[string]string{
+ "First": "value1",
+ "Second-Thing": "value2",
+ })
+
+ expectString(t, m.Get("first"), "value1")
+ expectString(t, m.Get("First"), "value1")
+ expectString(t, m.Get("FIRST"), "value1")
+
+ m.Set("first", "changed")
+ m.Set("third", "")
+
+ expectMetadata(t, m, map[string]string{
+ "First": "changed",
+ "Second-Thing": "value2",
+ "Third": "",
+ })
+
+ m.Clear("second-thing")
+ m.Clear("fourth-thing")
+
+ expectMetadata(t, m, map[string]string{
+ "First": "changed",
+ "Second-Thing": "",
+ "Third": "",
+ "Fourth-Thing": "",
+ })
+
+ m.Del("FIRST")
+ m.Del("second-Thing")
+
+ expectMetadata(t, m, map[string]string{
+ "Third": "",
+ "Fourth-Thing": "",
+ })
+
+}
+
+func expectMetadata(t *testing.T, actual Metadata, expected map[string]string) {
+ t.Helper()
+ reported := make(map[string]bool)
+
+ for k, av := range actual {
+ ev, exists := expected[k]
+ if !exists {
+ ev = "<not set>"
+ }
+ if av != ev {
+ t.Errorf(`expected "%s: %s", got "%s: %s" instead`, k, ev, k, av)
+ reported[k] = true
+ }
+ }
+
+ for k, ev := range expected {
+ av, exists := actual[k]
+ if !exists {
+ av = "<not set>"
+ }
+ if av != ev && !reported[k] {
+ t.Errorf(`expected "%s: %s", got "%s: %s" instead`, k, ev, k, av)
+ }
+ }
+}