unit tests for delegated functions; factor out common test http handlers into 'fixtures.go' files
diff --git a/openstack/objectstorage/v1/accounts/fixtures.go b/openstack/objectstorage/v1/accounts/fixtures.go
new file mode 100644
index 0000000..3dad0c5
--- /dev/null
+++ b/openstack/objectstorage/v1/accounts/fixtures.go
@@ -0,0 +1,38 @@
+// +build fixtures
+
+package accounts
+
+import (
+ "net/http"
+ "testing"
+
+ th "github.com/rackspace/gophercloud/testhelper"
+ fake "github.com/rackspace/gophercloud/testhelper/client"
+)
+
+// HandleGetAccountSuccessfully creates an HTTP handler at `/` on the test handler mux that
+// responds with a `Get` response.
+func HandleGetAccountSuccessfully(t *testing.T) {
+ th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "POST")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+ th.TestHeader(t, r, "X-Account-Meta-Gophercloud-Test", "accounts")
+
+ w.Header().Set("X-Account-Container-Count", "2")
+ w.Header().Set("X-Account-Bytes-Used", "14")
+ w.Header().Set("X-Account-Meta-Subject", "books")
+
+ w.WriteHeader(http.StatusNoContent)
+ })
+}
+
+// HandleUpdateAccountSuccessfully creates an HTTP handler at `/` on the test handler mux that
+// responds with a `Update` response.
+func HandleUpdateAccountSuccessfully(t *testing.T) {
+ th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "HEAD")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+ w.Header().Set("X-Account-Meta-Foo", "bar")
+ w.WriteHeader(http.StatusNoContent)
+ })
+}
diff --git a/openstack/objectstorage/v1/accounts/requests_test.go b/openstack/objectstorage/v1/accounts/requests_test.go
index d109214..0ad8d33 100644
--- a/openstack/objectstorage/v1/accounts/requests_test.go
+++ b/openstack/objectstorage/v1/accounts/requests_test.go
@@ -1,7 +1,6 @@
package accounts
import (
- "net/http"
"testing"
th "github.com/rackspace/gophercloud/testhelper"
@@ -13,18 +12,7 @@
func TestUpdateAccount(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "POST")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- th.TestHeader(t, r, "X-Account-Meta-Gophercloud-Test", "accounts")
-
- w.Header().Set("X-Account-Container-Count", "2")
- w.Header().Set("X-Account-Bytes-Used", "14")
- w.Header().Set("X-Account-Meta-Subject", "books")
-
- w.WriteHeader(http.StatusNoContent)
- })
+ HandleGetAccountSuccessfully(t)
options := &UpdateOpts{Metadata: map[string]string{"gophercloud-test": "accounts"}}
_, err := Update(fake.ServiceClient(), options).Extract()
@@ -34,13 +22,7 @@
func TestGetAccount(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "HEAD")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- w.Header().Set("X-Account-Meta-Foo", "bar")
- w.WriteHeader(http.StatusNoContent)
- })
+ HandleUpdateAccountSuccessfully(t)
expected := map[string]string{"Foo": "bar"}
actual, err := Get(fake.ServiceClient(), &GetOpts{}).ExtractMetadata()
diff --git a/openstack/objectstorage/v1/containers/fixtures.go b/openstack/objectstorage/v1/containers/fixtures.go
new file mode 100644
index 0000000..1c0a915
--- /dev/null
+++ b/openstack/objectstorage/v1/containers/fixtures.go
@@ -0,0 +1,132 @@
+// +build fixtures
+
+package containers
+
+import (
+ "fmt"
+ "net/http"
+ "testing"
+
+ th "github.com/rackspace/gophercloud/testhelper"
+ fake "github.com/rackspace/gophercloud/testhelper/client"
+)
+
+// ExpectedListInfo is the result expected from a call to `List` when full
+// info is requested.
+var ExpectedListInfo = []Container{
+ Container{
+ Count: 0,
+ Bytes: 0,
+ Name: "janeausten",
+ },
+ Container{
+ Count: 1,
+ Bytes: 14,
+ Name: "marktwain",
+ },
+}
+
+// ExpectedListNames is the result expected from a call to `List` when just
+// container names are requested.
+var ExpectedListNames = []string{"janeausten", "marktwain"}
+
+// HandleListContainerInfoSuccessfully creates an HTTP handler at `/` on the test handler mux that
+// responds with a `List` response when full info is requested.
+func HandleListContainerInfoSuccessfully(t *testing.T) {
+ th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "GET")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+ th.TestHeader(t, r, "Accept", "application/json")
+
+ w.Header().Set("Content-Type", "application/json")
+ r.ParseForm()
+ marker := r.Form.Get("marker")
+ switch marker {
+ case "":
+ fmt.Fprintf(w, `[
+ {
+ "count": 0,
+ "bytes": 0,
+ "name": "janeausten"
+ },
+ {
+ "count": 1,
+ "bytes": 14,
+ "name": "marktwain"
+ }
+ ]`)
+ case "marktwain":
+ fmt.Fprintf(w, `[]`)
+ default:
+ t.Fatalf("Unexpected marker: [%s]", marker)
+ }
+ })
+}
+
+// HandleListContainerNamesSuccessfully creates an HTTP handler at `/` on the test handler mux that
+// responds with a `ListNames` response when only container names are requested.
+func HandleListContainerNamesSuccessfully(t *testing.T) {
+ th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "GET")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+ th.TestHeader(t, r, "Accept", "text/plain")
+
+ w.Header().Set("Content-Type", "text/plain")
+ r.ParseForm()
+ marker := r.Form.Get("marker")
+ switch marker {
+ case "":
+ fmt.Fprintf(w, "janeausten\nmarktwain\n")
+ case "marktwain":
+ fmt.Fprintf(w, ``)
+ default:
+ t.Fatalf("Unexpected marker: [%s]", marker)
+ }
+ })
+}
+
+// HandleCreateContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that
+// responds with a `Create` response.
+func HandleCreateContainerSuccessfully(t *testing.T) {
+ th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "PUT")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+ th.TestHeader(t, r, "Accept", "application/json")
+
+ w.Header().Add("X-Container-Meta-Foo", "bar")
+ w.WriteHeader(http.StatusNoContent)
+ })
+}
+
+// HandleDeleteContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that
+// responds with a `Delete` response.
+func HandleDeleteContainerSuccessfully(t *testing.T) {
+ th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "DELETE")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+ th.TestHeader(t, r, "Accept", "application/json")
+ w.WriteHeader(http.StatusNoContent)
+ })
+}
+
+// HandleUpdateContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that
+// responds with a `Update` response.
+func HandleUpdateContainerSuccessfully(t *testing.T) {
+ th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "POST")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+ th.TestHeader(t, r, "Accept", "application/json")
+ w.WriteHeader(http.StatusNoContent)
+ })
+}
+
+// HandleGetContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that
+// responds with a `Get` response.
+func HandleGetContainerSuccessfully(t *testing.T) {
+ th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "HEAD")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+ th.TestHeader(t, r, "Accept", "application/json")
+ w.WriteHeader(http.StatusNoContent)
+ })
+}
diff --git a/openstack/objectstorage/v1/containers/requests_test.go b/openstack/objectstorage/v1/containers/requests_test.go
index a134272..4420aa0 100644
--- a/openstack/objectstorage/v1/containers/requests_test.go
+++ b/openstack/objectstorage/v1/containers/requests_test.go
@@ -1,8 +1,6 @@
package containers
import (
- "fmt"
- "net/http"
"testing"
"github.com/rackspace/gophercloud/pagination"
@@ -15,94 +13,29 @@
func TestListContainerInfo(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "GET")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- th.TestHeader(t, r, "Accept", "application/json")
-
- w.Header().Set("Content-Type", "application/json")
- r.ParseForm()
- marker := r.Form.Get("marker")
- switch marker {
- case "":
- fmt.Fprintf(w, `[
- {
- "count": 0,
- "bytes": 0,
- "name": "janeausten"
- },
- {
- "count": 1,
- "bytes": 14,
- "name": "marktwain"
- }
- ]`)
- case "marktwain":
- fmt.Fprintf(w, `[]`)
- default:
- t.Fatalf("Unexpected marker: [%s]", marker)
- }
- })
+ HandleListContainerInfoSuccessfully(t)
count := 0
-
- List(fake.ServiceClient(), &ListOpts{Full: true}).EachPage(func(page pagination.Page) (bool, error) {
+ err := List(fake.ServiceClient(), &ListOpts{Full: true}).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractInfo(page)
- if err != nil {
- t.Errorf("Failed to extract container info: %v", err)
- return false, err
- }
+ th.AssertNoErr(t, err)
- expected := []Container{
- Container{
- Count: 0,
- Bytes: 0,
- Name: "janeausten",
- },
- Container{
- Count: 1,
- Bytes: 14,
- Name: "marktwain",
- },
- }
-
- th.CheckDeepEquals(t, expected, actual)
+ th.CheckDeepEquals(t, ExpectedListInfo, actual)
return true, nil
})
-
- if count != 1 {
- t.Errorf("Expected 1 page, got %d", count)
- }
+ th.AssertNoErr(t, err)
+ th.CheckEquals(t, count, 1)
}
func TestListContainerNames(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "GET")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- th.TestHeader(t, r, "Accept", "text/plain")
-
- w.Header().Set("Content-Type", "text/plain")
- r.ParseForm()
- marker := r.Form.Get("marker")
- switch marker {
- case "":
- fmt.Fprintf(w, "janeausten\nmarktwain\n")
- case "marktwain":
- fmt.Fprintf(w, ``)
- default:
- t.Fatalf("Unexpected marker: [%s]", marker)
- }
- })
+ HandleListContainerNamesSuccessfully(t)
count := 0
-
- List(fake.ServiceClient(), &ListOpts{Full: false}).EachPage(func(page pagination.Page) (bool, error) {
+ err := List(fake.ServiceClient(), &ListOpts{Full: false}).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractNames(page)
if err != nil {
@@ -110,87 +43,49 @@
return false, err
}
- expected := []string{"janeausten", "marktwain"}
-
- th.CheckDeepEquals(t, expected, actual)
+ th.CheckDeepEquals(t, ExpectedListNames, actual)
return true, nil
})
-
- if count != 1 {
- t.Errorf("Expected 1 page, got %d", count)
- }
+ th.AssertNoErr(t, err)
+ th.CheckEquals(t, count, 1)
}
func TestCreateContainer(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "PUT")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- th.TestHeader(t, r, "Accept", "application/json")
-
- w.Header().Add("X-Container-Meta-Foo", "bar")
- w.WriteHeader(http.StatusNoContent)
- })
+ HandleCreateContainerSuccessfully(t)
options := CreateOpts{ContentType: "application/json", Metadata: map[string]string{"foo": "bar"}}
- headers, err := Create(fake.ServiceClient(), "testContainer", options).Extract()
- if err != nil {
- t.Fatalf("Unexpected error creating container: %v", err)
- }
+ headers, err := Create(fake.ServiceClient(), "testContainer", options).ExtractHeaders()
+ th.CheckNoErr(t, err)
th.CheckEquals(t, "bar", headers["X-Container-Meta-Foo"][0])
}
func TestDeleteContainer(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
+ HandleDeleteContainerSuccessfully(t)
- th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "DELETE")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- th.TestHeader(t, r, "Accept", "application/json")
- w.WriteHeader(http.StatusNoContent)
- })
-
- _, err := Delete(fake.ServiceClient(), "testContainer").Extract()
- if err != nil {
- t.Fatalf("Unexpected error deleting container: %v", err)
- }
+ _, err := Delete(fake.ServiceClient(), "testContainer").ExtractHeaders()
+ th.CheckNoErr(t, err)
}
func TestUpateContainer(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "POST")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- th.TestHeader(t, r, "Accept", "application/json")
- w.WriteHeader(http.StatusNoContent)
- })
+ HandleUpdateContainerSuccessfully(t)
options := &UpdateOpts{Metadata: map[string]string{"foo": "bar"}}
- _, err := Update(fake.ServiceClient(), "testContainer", options).Extract()
- if err != nil {
- t.Fatalf("Unexpected error updating container metadata: %v", err)
- }
+ _, err := Update(fake.ServiceClient(), "testContainer", options).ExtractHeaders()
+ th.CheckNoErr(t, err)
}
func TestGetContainer(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "HEAD")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- th.TestHeader(t, r, "Accept", "application/json")
- w.WriteHeader(http.StatusNoContent)
- })
+ HandleGetContainerSuccessfully(t)
_, err := Get(fake.ServiceClient(), "testContainer").ExtractMetadata()
- if err != nil {
- t.Fatalf("Unexpected error getting container metadata: %v", err)
- }
+ th.CheckNoErr(t, err)
}
diff --git a/openstack/objectstorage/v1/objects/fixtures.go b/openstack/objectstorage/v1/objects/fixtures.go
new file mode 100644
index 0000000..d951160
--- /dev/null
+++ b/openstack/objectstorage/v1/objects/fixtures.go
@@ -0,0 +1,164 @@
+// +build fixtures
+
+package objects
+
+import (
+ "fmt"
+ "net/http"
+ "testing"
+
+ th "github.com/rackspace/gophercloud/testhelper"
+ fake "github.com/rackspace/gophercloud/testhelper/client"
+)
+
+// HandleDownloadObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that
+// responds with a `Download` response.
+func HandleDownloadObjectSuccessfully(t *testing.T) {
+ th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "GET")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+ th.TestHeader(t, r, "Accept", "application/json")
+ w.WriteHeader(http.StatusOK)
+ fmt.Fprintf(w, "Successful download with Gophercloud")
+ })
+}
+
+// ExpectedListInfo is the result expected from a call to `List` when full
+// info is requested.
+var ExpectedListInfo = []Object{
+ Object{
+ Hash: "451e372e48e0f6b1114fa0724aa79fa1",
+ LastModified: "2009-11-10 23:00:00 +0000 UTC",
+ Bytes: 14,
+ Name: "goodbye",
+ ContentType: "application/octet-stream",
+ },
+ Object{
+ Hash: "451e372e48e0f6b1114fa0724aa79fa1",
+ LastModified: "2009-11-10 23:00:00 +0000 UTC",
+ Bytes: 14,
+ Name: "hello",
+ ContentType: "application/octet-stream",
+ },
+}
+
+// ExpectedListNames is the result expected from a call to `List` when just
+// object names are requested.
+var ExpectedListNames = []string{"hello", "goodbye"}
+
+// HandleListObjectsInfoSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that
+// responds with a `List` response when full info is requested.
+func HandleListObjectsInfoSuccessfully(t *testing.T) {
+ th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "GET")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+ th.TestHeader(t, r, "Accept", "application/json")
+
+ w.Header().Set("Content-Type", "application/json")
+ r.ParseForm()
+ marker := r.Form.Get("marker")
+ switch marker {
+ case "":
+ fmt.Fprintf(w, `[
+ {
+ "hash": "451e372e48e0f6b1114fa0724aa79fa1",
+ "last_modified": "2009-11-10 23:00:00 +0000 UTC",
+ "bytes": 14,
+ "name": "goodbye",
+ "content_type": "application/octet-stream"
+ },
+ {
+ "hash": "451e372e48e0f6b1114fa0724aa79fa1",
+ "last_modified": "2009-11-10 23:00:00 +0000 UTC",
+ "bytes": 14,
+ "name": "hello",
+ "content_type": "application/octet-stream"
+ }
+ ]`)
+ case "hello":
+ fmt.Fprintf(w, `[]`)
+ default:
+ t.Fatalf("Unexpected marker: [%s]", marker)
+ }
+ })
+}
+
+// HandleListObjectNamesSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that
+// responds with a `List` response when only object names are requested.
+func HandleListObjectNamesSuccessfully(t *testing.T) {
+ th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "GET")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+ th.TestHeader(t, r, "Accept", "text/plain")
+
+ w.Header().Set("Content-Type", "text/plain")
+ r.ParseForm()
+ marker := r.Form.Get("marker")
+ switch marker {
+ case "":
+ fmt.Fprintf(w, "hello\ngoodbye\n")
+ case "goodbye":
+ fmt.Fprintf(w, "")
+ default:
+ t.Fatalf("Unexpected marker: [%s]", marker)
+ }
+ })
+}
+
+// HandleCreateObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that
+// responds with a `Create` response.
+func HandleCreateObjectSuccessfully(t *testing.T) {
+ th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "PUT")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+ th.TestHeader(t, r, "Accept", "application/json")
+ w.WriteHeader(http.StatusCreated)
+ })
+}
+
+// HandleCopyObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that
+// responds with a `Copy` response.
+func HandleCopyObjectSuccessfully(t *testing.T) {
+ th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "COPY")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+ th.TestHeader(t, r, "Accept", "application/json")
+ th.TestHeader(t, r, "Destination", "/newTestContainer/newTestObject")
+ w.WriteHeader(http.StatusCreated)
+ })
+}
+
+// HandleDeleteObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that
+// responds with a `Delete` response.
+func HandleDeleteObjectSuccessfully(t *testing.T) {
+ th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "DELETE")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+ th.TestHeader(t, r, "Accept", "application/json")
+ w.WriteHeader(http.StatusNoContent)
+ })
+}
+
+// HandleUpdateObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that
+// responds with a `Update` response.
+func HandleUpdateObjectSuccessfully(t *testing.T) {
+ th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "POST")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+ th.TestHeader(t, r, "Accept", "application/json")
+ th.TestHeader(t, r, "X-Object-Meta-Gophercloud-Test", "objects")
+ w.WriteHeader(http.StatusAccepted)
+ })
+}
+
+// HandleGetObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that
+// responds with a `Get` response.
+func HandleGetObjectSuccessfully(t *testing.T) {
+ th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "HEAD")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+ th.TestHeader(t, r, "Accept", "application/json")
+ w.Header().Add("X-Object-Meta-Gophercloud-Test", "objects")
+ w.WriteHeader(http.StatusNoContent)
+ })
+}
diff --git a/openstack/objectstorage/v1/objects/requests_test.go b/openstack/objectstorage/v1/objects/requests_test.go
index 7147782..232e626 100644
--- a/openstack/objectstorage/v1/objects/requests_test.go
+++ b/openstack/objectstorage/v1/objects/requests_test.go
@@ -2,247 +2,113 @@
import (
"bytes"
- "fmt"
- "net/http"
"testing"
"github.com/rackspace/gophercloud/pagination"
- "github.com/rackspace/gophercloud/testhelper"
+ th "github.com/rackspace/gophercloud/testhelper"
fake "github.com/rackspace/gophercloud/testhelper/client"
)
-var metadata = map[string]string{"Gophercloud-Test": "objects"}
-
func TestDownloadObject(t *testing.T) {
- testhelper.SetupHTTP()
- defer testhelper.TeardownHTTP()
-
- testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
- testhelper.TestMethod(t, r, "GET")
- testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- testhelper.TestHeader(t, r, "Accept", "application/json")
- w.WriteHeader(http.StatusOK)
- fmt.Fprintf(w, "Successful download with Gophercloud")
- })
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ HandleDownloadObjectSuccessfully(t)
content, err := Download(fake.ServiceClient(), "testContainer", "testObject", nil).ExtractContent()
- if err != nil {
- t.Fatalf("Unexpected error downloading object: %v", err)
- }
- if string(content) != "Successful download with Gophercloud" {
- t.Errorf("Expected %s, got %s", "Successful download with Gophercloud", content)
- }
+ th.AssertNoErr(t, err)
+ th.CheckEquals(t, string(content), "Successful download with Gophercloud")
}
func TestListObjectInfo(t *testing.T) {
- testhelper.SetupHTTP()
- defer testhelper.TeardownHTTP()
-
- testhelper.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
- testhelper.TestMethod(t, r, "GET")
- testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- testhelper.TestHeader(t, r, "Accept", "application/json")
-
- w.Header().Set("Content-Type", "application/json")
- r.ParseForm()
- marker := r.Form.Get("marker")
- switch marker {
- case "":
- fmt.Fprintf(w, `[
- {
- "hash": "451e372e48e0f6b1114fa0724aa79fa1",
- "last_modified": "2009-11-10 23:00:00 +0000 UTC",
- "bytes": 14,
- "name": "goodbye",
- "content_type": "application/octet-stream"
- },
- {
- "hash": "451e372e48e0f6b1114fa0724aa79fa1",
- "last_modified": "2009-11-10 23:00:00 +0000 UTC",
- "bytes": 14,
- "name": "hello",
- "content_type": "application/octet-stream"
- }
- ]`)
- case "hello":
- fmt.Fprintf(w, `[]`)
- default:
- t.Fatalf("Unexpected marker: [%s]", marker)
- }
- })
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ HandleListObjectsInfoSuccessfully(t)
count := 0
-
- err := List(fake.ServiceClient(), "testContainer", &ListOpts{Full: true}).EachPage(func(page pagination.Page) (bool, error) {
+ options := &ListOpts{Full: true}
+ err := List(fake.ServiceClient(), "testContainer", options).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractInfo(page)
- if err != nil {
- t.Errorf("Failed to extract object info: %v", err)
- return false, err
- }
+ th.AssertNoErr(t, err)
- expected := []Object{
- Object{
- Hash: "451e372e48e0f6b1114fa0724aa79fa1",
- LastModified: "2009-11-10 23:00:00 +0000 UTC",
- Bytes: 14,
- Name: "goodbye",
- ContentType: "application/octet-stream",
- },
- Object{
- Hash: "451e372e48e0f6b1114fa0724aa79fa1",
- LastModified: "2009-11-10 23:00:00 +0000 UTC",
- Bytes: 14,
- Name: "hello",
- ContentType: "application/octet-stream",
- },
- }
-
- testhelper.CheckDeepEquals(t, actual, expected)
+ th.CheckDeepEquals(t, ExpectedListInfo, actual)
return true, nil
})
- if err != nil {
- t.Error(err)
- }
-
- if count != 1 {
- t.Errorf("Expected 1 page, got %d", count)
- }
+ th.AssertNoErr(t, err)
+ th.CheckEquals(t, count, 1)
}
func TestListObjectNames(t *testing.T) {
- testhelper.SetupHTTP()
- defer testhelper.TeardownHTTP()
-
- testhelper.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
- testhelper.TestMethod(t, r, "GET")
- testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- testhelper.TestHeader(t, r, "Accept", "text/plain")
-
- w.Header().Set("Content-Type", "text/plain")
- r.ParseForm()
- marker := r.Form.Get("marker")
- switch marker {
- case "":
- fmt.Fprintf(w, "hello\ngoodbye\n")
- case "goodbye":
- fmt.Fprintf(w, "")
- default:
- t.Fatalf("Unexpected marker: [%s]", marker)
- }
- })
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ HandleListObjectNamesSuccessfully(t)
count := 0
- List(fake.ServiceClient(), "testContainer", &ListOpts{Full: false}).EachPage(func(page pagination.Page) (bool, error) {
+ options := &ListOpts{Full: false}
+ err := List(fake.ServiceClient(), "testContainer", options).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractNames(page)
if err != nil {
- t.Errorf("Failed to extract object names: %v", err)
+ t.Errorf("Failed to extract container names: %v", err)
return false, err
}
- expected := []string{"hello", "goodbye"}
-
- testhelper.CheckDeepEquals(t, expected, actual)
+ th.CheckDeepEquals(t, ExpectedListNames, actual)
return true, nil
})
-
- if count != 1 {
- t.Errorf("Expected 1 page, got %d", count)
- }
+ th.AssertNoErr(t, err)
+ th.CheckEquals(t, count, 1)
}
func TestCreateObject(t *testing.T) {
- testhelper.SetupHTTP()
- defer testhelper.TeardownHTTP()
-
- testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
- testhelper.TestMethod(t, r, "PUT")
- testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- testhelper.TestHeader(t, r, "Accept", "application/json")
- w.WriteHeader(http.StatusCreated)
- })
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ HandleCreateObjectSuccessfully(t)
content := bytes.NewBufferString("Did gyre and gimble in the wabe")
options := &CreateOpts{ContentType: "application/json"}
- _, err := Create(fake.ServiceClient(), "testContainer", "testObject", content, options).Extract()
- if err != nil {
- t.Fatalf("Unexpected error creating object: %v", err)
- }
+ _, err := Create(fake.ServiceClient(), "testContainer", "testObject", content, options).ExtractHeaders()
+ th.AssertNoErr(t, err)
}
func TestCopyObject(t *testing.T) {
- testhelper.SetupHTTP()
- defer testhelper.TeardownHTTP()
-
- testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
- testhelper.TestMethod(t, r, "COPY")
- testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- testhelper.TestHeader(t, r, "Accept", "application/json")
- testhelper.TestHeader(t, r, "Destination", "/newTestContainer/newTestObject")
- w.WriteHeader(http.StatusCreated)
- })
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ HandleCopyObjectSuccessfully(t)
options := &CopyOpts{Destination: "/newTestContainer/newTestObject"}
- _, err := Copy(fake.ServiceClient(), "testContainer", "testObject", options).Extract()
- if err != nil {
- t.Fatalf("Unexpected error copying object: %v", err)
- }
+ _, err := Copy(fake.ServiceClient(), "testContainer", "testObject", options).ExtractHeaders()
+ th.AssertNoErr(t, err)
}
func TestDeleteObject(t *testing.T) {
- testhelper.SetupHTTP()
- defer testhelper.TeardownHTTP()
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ HandleDeleteObjectSuccessfully(t)
- testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
- testhelper.TestMethod(t, r, "DELETE")
- testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- testhelper.TestHeader(t, r, "Accept", "application/json")
- w.WriteHeader(http.StatusNoContent)
- })
-
- _, err := Delete(fake.ServiceClient(), "testContainer", "testObject", nil).Extract()
- if err != nil {
- t.Fatalf("Unexpected error deleting object: %v", err)
- }
+ _, err := Delete(fake.ServiceClient(), "testContainer", "testObject", nil).ExtractHeaders()
+ th.AssertNoErr(t, err)
}
func TestUpateObjectMetadata(t *testing.T) {
- testhelper.SetupHTTP()
- defer testhelper.TeardownHTTP()
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ HandleUpdateObjectSuccessfully(t)
- testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
- testhelper.TestMethod(t, r, "POST")
- testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- testhelper.TestHeader(t, r, "Accept", "application/json")
- testhelper.TestHeader(t, r, "X-Object-Meta-Gophercloud-Test", "objects")
- w.WriteHeader(http.StatusAccepted)
- })
-
- _, err := Update(fake.ServiceClient(), "testContainer", "testObject", &UpdateOpts{Metadata: metadata}).Extract()
- if err != nil {
- t.Fatalf("Unexpected error updating object metadata: %v", err)
- }
+ options := &UpdateOpts{Metadata: map[string]string{"Gophercloud-Test": "objects"}}
+ _, err := Update(fake.ServiceClient(), "testContainer", "testObject", options).ExtractHeaders()
+ th.AssertNoErr(t, err)
}
func TestGetObject(t *testing.T) {
- testhelper.SetupHTTP()
- defer testhelper.TeardownHTTP()
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ HandleGetObjectSuccessfully(t)
- testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
- testhelper.TestMethod(t, r, "HEAD")
- testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- testhelper.TestHeader(t, r, "Accept", "application/json")
- w.Header().Add("X-Object-Meta-Gophercloud-Test", "objects")
- w.WriteHeader(http.StatusNoContent)
- })
-
- expected := metadata
+ expected := map[string]string{"Gophercloud-Test": "objects"}
actual, err := Get(fake.ServiceClient(), "testContainer", "testObject", nil).ExtractMetadata()
- if err != nil {
- t.Fatalf("Unexpected error getting object metadata: %v", err)
- }
- testhelper.CheckDeepEquals(t, expected, actual)
+ th.AssertNoErr(t, err)
+ th.CheckDeepEquals(t, expected, actual)
}
diff --git a/rackspace/objectstorage/v1/accounts/delegate_test.go b/rackspace/objectstorage/v1/accounts/delegate_test.go
index f9c870a..15a21ae 100644
--- a/rackspace/objectstorage/v1/accounts/delegate_test.go
+++ b/rackspace/objectstorage/v1/accounts/delegate_test.go
@@ -2,14 +2,33 @@
import (
"testing"
- //th "github.com/rackspace/gophercloud/testhelper"
- //fake "github.com/rackspace/gophercloud/testhelper/client"
+
+ os "github.com/rackspace/gophercloud/openstack/objectstorage/v1/accounts"
+ th "github.com/rackspace/gophercloud/testhelper"
+ fake "github.com/rackspace/gophercloud/testhelper/client"
)
func TestGetAccounts(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ os.HandleGetAccountSuccessfully(t)
+ options := &UpdateOpts{Metadata: map[string]string{"gophercloud-test": "accounts"}}
+ _, err := Update(fake.ServiceClient(), options).ExtractHeaders()
+ if err != nil {
+ t.Fatalf("Unable to update account: %v", err)
+ }
}
func TestUpdateAccounts(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ os.HandleUpdateAccountSuccessfully(t)
+ expected := map[string]string{"Foo": "bar"}
+ actual, err := Get(fake.ServiceClient()).ExtractMetadata()
+ if err != nil {
+ t.Fatalf("Unable to get account metadata: %v", err)
+ }
+ th.CheckDeepEquals(t, expected, actual)
}
diff --git a/rackspace/objectstorage/v1/containers/delegate.go b/rackspace/objectstorage/v1/containers/delegate.go
index 67c2c41..293cbf1 100644
--- a/rackspace/objectstorage/v1/containers/delegate.go
+++ b/rackspace/objectstorage/v1/containers/delegate.go
@@ -6,6 +6,18 @@
"github.com/rackspace/gophercloud/pagination"
)
+// ExtractInfo interprets a page of List results when full container info
+// is requested.
+func ExtractInfo(page pagination.Page) ([]os.Container, error) {
+ return os.ExtractInfo(page)
+}
+
+// ExtractNames interprets a page of List results when just the container
+// names are requested.
+func ExtractNames(page pagination.Page) ([]string, error) {
+ return os.ExtractNames(page)
+}
+
// List is a function that retrieves containers associated with the account as
// well as account metadata. It returns a pager which can be iterated with the
// EachPage function.
diff --git a/rackspace/objectstorage/v1/containers/delegate_test.go b/rackspace/objectstorage/v1/containers/delegate_test.go
index 0c09d3e..86868ae 100644
--- a/rackspace/objectstorage/v1/containers/delegate_test.go
+++ b/rackspace/objectstorage/v1/containers/delegate_test.go
@@ -1 +1,91 @@
package containers
+
+import (
+ "testing"
+
+ os "github.com/rackspace/gophercloud/openstack/objectstorage/v1/containers"
+ "github.com/rackspace/gophercloud/pagination"
+ th "github.com/rackspace/gophercloud/testhelper"
+ fake "github.com/rackspace/gophercloud/testhelper/client"
+)
+
+func TestListContainerInfo(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ os.HandleListContainerInfoSuccessfully(t)
+
+ count := 0
+ err := List(fake.ServiceClient(), &os.ListOpts{Full: true}).EachPage(func(page pagination.Page) (bool, error) {
+ count++
+ actual, err := ExtractInfo(page)
+ th.AssertNoErr(t, err)
+
+ th.CheckDeepEquals(t, os.ExpectedListInfo, actual)
+
+ return true, nil
+ })
+ th.AssertNoErr(t, err)
+ th.CheckEquals(t, count, 1)
+}
+
+func TestListContainerNames(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ os.HandleListContainerNamesSuccessfully(t)
+
+ count := 0
+ err := List(fake.ServiceClient(), &os.ListOpts{Full: false}).EachPage(func(page pagination.Page) (bool, error) {
+ count++
+ actual, err := ExtractNames(page)
+ if err != nil {
+ t.Errorf("Failed to extract container names: %v", err)
+ return false, err
+ }
+
+ th.CheckDeepEquals(t, os.ExpectedListNames, actual)
+
+ return true, nil
+ })
+ th.AssertNoErr(t, err)
+ th.CheckEquals(t, count, 1)
+}
+
+func TestCreateContainers(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ os.HandleCreateContainerSuccessfully(t)
+
+ options := os.CreateOpts{ContentType: "application/json", Metadata: map[string]string{"foo": "bar"}}
+ headers, err := Create(fake.ServiceClient(), "testContainer", options).ExtractHeaders()
+ th.CheckNoErr(t, err)
+ th.CheckEquals(t, "bar", headers["X-Container-Meta-Foo"][0])
+
+}
+
+func TestDeleteContainers(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ os.HandleDeleteContainerSuccessfully(t)
+
+ _, err := Delete(fake.ServiceClient(), "testContainer").ExtractHeaders()
+ th.CheckNoErr(t, err)
+}
+
+func TestUpdateContainers(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ os.HandleUpdateContainerSuccessfully(t)
+
+ options := &os.UpdateOpts{Metadata: map[string]string{"foo": "bar"}}
+ _, err := Update(fake.ServiceClient(), "testContainer", options).ExtractHeaders()
+ th.CheckNoErr(t, err)
+}
+
+func TestGetContainers(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ os.HandleGetContainerSuccessfully(t)
+
+ _, err := Get(fake.ServiceClient(), "testContainer").ExtractMetadata()
+ th.CheckNoErr(t, err)
+}
diff --git a/rackspace/objectstorage/v1/objects/delegate_test.go b/rackspace/objectstorage/v1/objects/delegate_test.go
index 004cbf8..808f940 100644
--- a/rackspace/objectstorage/v1/objects/delegate_test.go
+++ b/rackspace/objectstorage/v1/objects/delegate_test.go
@@ -1 +1,115 @@
package objects
+
+import (
+ "bytes"
+ "testing"
+
+ os "github.com/rackspace/gophercloud/openstack/objectstorage/v1/objects"
+ "github.com/rackspace/gophercloud/pagination"
+ th "github.com/rackspace/gophercloud/testhelper"
+ fake "github.com/rackspace/gophercloud/testhelper/client"
+)
+
+func TestDownloadObject(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ os.HandleDownloadObjectSuccessfully(t)
+
+ content, err := Download(fake.ServiceClient(), "testContainer", "testObject", nil).ExtractContent()
+ th.AssertNoErr(t, err)
+ th.CheckEquals(t, string(content), "Successful download with Gophercloud")
+}
+
+func TestListObjectsInfo(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ os.HandleListObjectsInfoSuccessfully(t)
+
+ count := 0
+ options := &os.ListOpts{Full: true}
+ err := List(fake.ServiceClient(), "testContainer", options).EachPage(func(page pagination.Page) (bool, error) {
+ count++
+ actual, err := ExtractInfo(page)
+ th.AssertNoErr(t, err)
+
+ th.CheckDeepEquals(t, os.ExpectedListInfo, actual)
+
+ return true, nil
+ })
+ th.AssertNoErr(t, err)
+ th.CheckEquals(t, count, 1)
+}
+
+func TestListObjectNames(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ os.HandleListObjectNamesSuccessfully(t)
+
+ count := 0
+ options := &os.ListOpts{Full: false}
+ err := List(fake.ServiceClient(), "testContainer", options).EachPage(func(page pagination.Page) (bool, error) {
+ count++
+ actual, err := ExtractNames(page)
+ if err != nil {
+ t.Errorf("Failed to extract container names: %v", err)
+ return false, err
+ }
+
+ th.CheckDeepEquals(t, os.ExpectedListNames, actual)
+
+ return true, nil
+ })
+ th.AssertNoErr(t, err)
+ th.CheckEquals(t, count, 1)
+}
+
+func TestCreateObject(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ os.HandleCreateObjectSuccessfully(t)
+
+ content := bytes.NewBufferString("Did gyre and gimble in the wabe")
+ options := &os.CreateOpts{ContentType: "application/json"}
+ _, err := Create(fake.ServiceClient(), "testContainer", "testObject", content, options).ExtractHeaders()
+ th.AssertNoErr(t, err)
+}
+
+func TestCopyObject(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ os.HandleCopyObjectSuccessfully(t)
+
+ options := &CopyOpts{Destination: "/newTestContainer/newTestObject"}
+ _, err := Copy(fake.ServiceClient(), "testContainer", "testObject", options).ExtractHeaders()
+ th.AssertNoErr(t, err)
+}
+
+func TestDeleteObject(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ os.HandleDeleteObjectSuccessfully(t)
+
+ _, err := Delete(fake.ServiceClient(), "testContainer", "testObject", nil).ExtractHeaders()
+ th.AssertNoErr(t, err)
+}
+
+func TestUpdateObject(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ os.HandleUpdateObjectSuccessfully(t)
+
+ options := &os.UpdateOpts{Metadata: map[string]string{"Gophercloud-Test": "objects"}}
+ _, err := Update(fake.ServiceClient(), "testContainer", "testObject", options).ExtractHeaders()
+ th.AssertNoErr(t, err)
+}
+
+func TestGetObject(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ os.HandleGetObjectSuccessfully(t)
+
+ expected := map[string]string{"Gophercloud-Test": "objects"}
+ actual, err := Get(fake.ServiceClient(), "testContainer", "testObject", nil).ExtractMetadata()
+ th.AssertNoErr(t, err)
+ th.CheckDeepEquals(t, expected, actual)
+}