unit tests for delegated functions; factor out common test http handlers into 'fixtures.go' files
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)
}