Making server action result types more consistent
diff --git a/_site/testhelper/client/fake.go b/_site/testhelper/client/fake.go
new file mode 100644
index 0000000..3eb9e12
--- /dev/null
+++ b/_site/testhelper/client/fake.go
@@ -0,0 +1,17 @@
+package client
+
+import (
+ "github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/testhelper"
+)
+
+// Fake token to use.
+const TokenID = "cbc36478b0bd8e67e89469c7749d4127"
+
+// ServiceClient returns a generic service client for use in tests.
+func ServiceClient() *gophercloud.ServiceClient {
+ return &gophercloud.ServiceClient{
+ Provider: &gophercloud.ProviderClient{TokenID: TokenID},
+ Endpoint: testhelper.Endpoint(),
+ }
+}
diff --git a/_site/testhelper/convenience.go b/_site/testhelper/convenience.go
new file mode 100644
index 0000000..f6cb371
--- /dev/null
+++ b/_site/testhelper/convenience.go
@@ -0,0 +1,75 @@
+package testhelper
+
+import (
+ "fmt"
+ "path/filepath"
+ "reflect"
+ "runtime"
+ "testing"
+)
+
+func prefix() string {
+ _, file, line, _ := runtime.Caller(3)
+ return fmt.Sprintf("Failure in %s, line %d:", filepath.Base(file), line)
+}
+
+func green(str interface{}) string {
+ return fmt.Sprintf("\033[0m\033[1;32m%#v\033[0m\033[1;31m", str)
+}
+
+func yellow(str interface{}) string {
+ return fmt.Sprintf("\033[0m\033[1;33m%#v\033[0m\033[1;31m", str)
+}
+
+func logFatal(t *testing.T, str string) {
+ t.Fatalf("\033[1;31m%s %s\033[0m", prefix(), str)
+}
+
+func logError(t *testing.T, str string) {
+ t.Errorf("\033[1;31m%s %s\033[0m", prefix(), str)
+}
+
+// AssertEquals compares two arbitrary values and performs a comparison. If the
+// comparison fails, a fatal error is raised that will fail the test
+func AssertEquals(t *testing.T, expected, actual interface{}) {
+ if expected != actual {
+ logFatal(t, fmt.Sprintf("expected %s but got %s", green(expected), yellow(actual)))
+ }
+}
+
+// CheckEquals is similar to AssertEquals, except with a non-fatal error
+func CheckEquals(t *testing.T, expected, actual interface{}) {
+ if expected != actual {
+ logError(t, fmt.Sprintf("expected %s but got %s", green(expected), yellow(actual)))
+ }
+}
+
+// AssertDeepEquals - like Equals - performs a comparison - but on more complex
+// structures that requires deeper inspection
+func AssertDeepEquals(t *testing.T, expected, actual interface{}) {
+ if !reflect.DeepEqual(expected, actual) {
+ logFatal(t, fmt.Sprintf("expected %s but got %s", green(expected), yellow(actual)))
+ }
+}
+
+// CheckDeepEquals is similar to AssertDeepEquals, except with a non-fatal error
+func CheckDeepEquals(t *testing.T, expected, actual interface{}) {
+ if !reflect.DeepEqual(expected, actual) {
+ logError(t, fmt.Sprintf("expected %s but got %s", green(expected), yellow(actual)))
+ }
+}
+
+// AssertNoErr is a convenience function for checking whether an error value is
+// an actual error
+func AssertNoErr(t *testing.T, e error) {
+ if e != nil {
+ logFatal(t, fmt.Sprintf("unexpected error %s", yellow(e.Error())))
+ }
+}
+
+// CheckNoErr is similar to AssertNoErr, except with a non-fatal error
+func CheckNoErr(t *testing.T, e error) {
+ if e != nil {
+ logError(t, fmt.Sprintf("unexpected error %s", yellow(e.Error())))
+ }
+}
diff --git a/_site/testhelper/doc.go b/_site/testhelper/doc.go
new file mode 100644
index 0000000..25b4dfe
--- /dev/null
+++ b/_site/testhelper/doc.go
@@ -0,0 +1,4 @@
+/*
+Package testhelper container methods that are useful for writing unit tests.
+*/
+package testhelper
diff --git a/_site/testhelper/http_responses.go b/_site/testhelper/http_responses.go
new file mode 100644
index 0000000..481a833
--- /dev/null
+++ b/_site/testhelper/http_responses.go
@@ -0,0 +1,113 @@
+package testhelper
+
+import (
+ "encoding/json"
+ "io/ioutil"
+ "net/http"
+ "net/http/httptest"
+ "net/url"
+ "reflect"
+ "testing"
+)
+
+var (
+ // Mux is a multiplexer that can be used to register handlers.
+ Mux *http.ServeMux
+
+ // Server is an in-memory HTTP server for testing.
+ Server *httptest.Server
+)
+
+// SetupHTTP prepares the Mux and Server.
+func SetupHTTP() {
+ Mux = http.NewServeMux()
+ Server = httptest.NewServer(Mux)
+}
+
+// TeardownHTTP releases HTTP-related resources.
+func TeardownHTTP() {
+ Server.Close()
+}
+
+// Endpoint returns a fake endpoint that will actually target the Mux.
+func Endpoint() string {
+ return Server.URL + "/"
+}
+
+// TestFormValues ensures that all the URL parameters given to the http.Request are the same as values.
+func TestFormValues(t *testing.T, r *http.Request, values map[string]string) {
+ want := url.Values{}
+ for k, v := range values {
+ want.Add(k, v)
+ }
+
+ r.ParseForm()
+ if !reflect.DeepEqual(want, r.Form) {
+ t.Errorf("Request parameters = %v, want %v", r.Form, want)
+ }
+}
+
+// TestMethod checks that the Request has the expected method (e.g. GET, POST).
+func TestMethod(t *testing.T, r *http.Request, expected string) {
+ if expected != r.Method {
+ t.Errorf("Request method = %v, expected %v", r.Method, expected)
+ }
+}
+
+// TestHeader checks that the header on the http.Request matches the expected value.
+func TestHeader(t *testing.T, r *http.Request, header string, expected string) {
+ if actual := r.Header.Get(header); expected != actual {
+ t.Errorf("Header %s = %s, expected %s", header, actual, expected)
+ }
+}
+
+// TestBody verifies that the request body matches an expected body.
+func TestBody(t *testing.T, r *http.Request, expected string) {
+ b, err := ioutil.ReadAll(r.Body)
+ if err != nil {
+ t.Errorf("Unable to read body: %v", err)
+ }
+ str := string(b)
+ if expected != str {
+ t.Errorf("Body = %s, expected %s", str, expected)
+ }
+}
+
+// TestJSONRequest verifies that the JSON payload of a request matches an expected structure, without asserting things about
+// whitespace or ordering.
+func TestJSONRequest(t *testing.T, r *http.Request, expected string) {
+ b, err := ioutil.ReadAll(r.Body)
+ if err != nil {
+ t.Errorf("Unable to read request body: %v", err)
+ }
+
+ var expectedJSON interface{}
+ err = json.Unmarshal([]byte(expected), &expectedJSON)
+ if err != nil {
+ t.Errorf("Unable to parse expected value as JSON: %v", err)
+ }
+
+ var actualJSON interface{}
+ err = json.Unmarshal(b, &actualJSON)
+ if err != nil {
+ t.Errorf("Unable to parse request body as JSON: %v", err)
+ }
+
+ if !reflect.DeepEqual(expectedJSON, actualJSON) {
+ prettyExpected, err := json.MarshalIndent(expectedJSON, "", " ")
+ if err != nil {
+ t.Logf("Unable to pretty-print expected JSON: %v\n%s", err, expected)
+ } else {
+ t.Logf("Expected JSON:\n%s", prettyExpected)
+ }
+
+ prettyActual, err := json.MarshalIndent(actualJSON, "", " ")
+ if err != nil {
+ t.Logf("Unable to pretty-print actual JSON: %v\n%s", err, b)
+ } else {
+ t.Logf("Actual JSON:\n%s", prettyActual)
+ }
+
+ t.Errorf("Response body did not contain the correct JSON.")
+ }
+}