Ash Wilson | dd580ce | 2014-08-28 15:37:55 -0400 | [diff] [blame] | 1 | package testhelper |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "io/ioutil" |
| 6 | "net/http" |
Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 7 | "net/http/httptest" |
Ash Wilson | dd580ce | 2014-08-28 15:37:55 -0400 | [diff] [blame] | 8 | "net/url" |
| 9 | "reflect" |
| 10 | "testing" |
Jamie Hannaford | 539e8d9 | 2014-10-02 17:27:51 +0200 | [diff] [blame] | 11 | |
| 12 | "github.com/rackspace/gophercloud" |
Ash Wilson | dd580ce | 2014-08-28 15:37:55 -0400 | [diff] [blame] | 13 | ) |
| 14 | |
Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 15 | var ( |
| 16 | // Mux is a multiplexer that can be used to register handlers. |
| 17 | Mux *http.ServeMux |
| 18 | |
| 19 | // Server is an in-memory HTTP server for testing. |
| 20 | Server *httptest.Server |
| 21 | ) |
| 22 | |
| 23 | // SetupHTTP prepares the Mux and Server. |
| 24 | func SetupHTTP() { |
| 25 | Mux = http.NewServeMux() |
| 26 | Server = httptest.NewServer(Mux) |
| 27 | } |
| 28 | |
| 29 | // TeardownHTTP releases HTTP-related resources. |
| 30 | func TeardownHTTP() { |
| 31 | Server.Close() |
| 32 | } |
| 33 | |
| 34 | // Endpoint returns a fake endpoint that will actually target the Mux. |
| 35 | func Endpoint() string { |
| 36 | return Server.URL + "/" |
| 37 | } |
| 38 | |
Ash Wilson | dd580ce | 2014-08-28 15:37:55 -0400 | [diff] [blame] | 39 | // TestFormValues ensures that all the URL parameters given to the http.Request are the same as values. |
| 40 | func TestFormValues(t *testing.T, r *http.Request, values map[string]string) { |
| 41 | want := url.Values{} |
| 42 | for k, v := range values { |
| 43 | want.Add(k, v) |
| 44 | } |
| 45 | |
| 46 | r.ParseForm() |
| 47 | if !reflect.DeepEqual(want, r.Form) { |
| 48 | t.Errorf("Request parameters = %v, want %v", r.Form, want) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // TestMethod checks that the Request has the expected method (e.g. GET, POST). |
| 53 | func TestMethod(t *testing.T, r *http.Request, expected string) { |
| 54 | if expected != r.Method { |
| 55 | t.Errorf("Request method = %v, expected %v", r.Method, expected) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // TestHeader checks that the header on the http.Request matches the expected value. |
| 60 | func TestHeader(t *testing.T, r *http.Request, header string, expected string) { |
| 61 | if actual := r.Header.Get(header); expected != actual { |
| 62 | t.Errorf("Header %s = %s, expected %s", header, actual, expected) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // TestBody verifies that the request body matches an expected body. |
| 67 | func TestBody(t *testing.T, r *http.Request, expected string) { |
| 68 | b, err := ioutil.ReadAll(r.Body) |
| 69 | if err != nil { |
| 70 | t.Errorf("Unable to read body: %v", err) |
| 71 | } |
| 72 | str := string(b) |
| 73 | if expected != str { |
| 74 | t.Errorf("Body = %s, expected %s", str, expected) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // TestJSONRequest verifies that the JSON payload of a request matches an expected structure, without asserting things about |
| 79 | // whitespace or ordering. |
| 80 | func TestJSONRequest(t *testing.T, r *http.Request, expected string) { |
| 81 | b, err := ioutil.ReadAll(r.Body) |
| 82 | if err != nil { |
| 83 | t.Errorf("Unable to read request body: %v", err) |
| 84 | } |
| 85 | |
| 86 | var expectedJSON interface{} |
| 87 | err = json.Unmarshal([]byte(expected), &expectedJSON) |
| 88 | if err != nil { |
| 89 | t.Errorf("Unable to parse expected value as JSON: %v", err) |
| 90 | } |
| 91 | |
| 92 | var actualJSON interface{} |
| 93 | err = json.Unmarshal(b, &actualJSON) |
| 94 | if err != nil { |
| 95 | t.Errorf("Unable to parse request body as JSON: %v", err) |
| 96 | } |
| 97 | |
| 98 | if !reflect.DeepEqual(expectedJSON, actualJSON) { |
Ash Wilson | 9e3f715 | 2014-08-28 16:02:49 -0400 | [diff] [blame] | 99 | prettyExpected, err := json.MarshalIndent(expectedJSON, "", " ") |
| 100 | if err != nil { |
| 101 | t.Logf("Unable to pretty-print expected JSON: %v\n%s", err, expected) |
| 102 | } else { |
| 103 | t.Logf("Expected JSON:\n%s", prettyExpected) |
| 104 | } |
| 105 | |
| 106 | prettyActual, err := json.MarshalIndent(actualJSON, "", " ") |
| 107 | if err != nil { |
| 108 | t.Logf("Unable to pretty-print actual JSON: %v\n%s", err, b) |
| 109 | } else { |
| 110 | t.Logf("Actual JSON:\n%s", prettyActual) |
| 111 | } |
| 112 | |
Ash Wilson | dd580ce | 2014-08-28 15:37:55 -0400 | [diff] [blame] | 113 | t.Errorf("Response body did not contain the correct JSON.") |
| 114 | } |
| 115 | } |
Jamie Hannaford | 539e8d9 | 2014-10-02 17:27:51 +0200 | [diff] [blame] | 116 | |
| 117 | // Fake token to use. |
| 118 | const TokenID = "cbc36478b0bd8e67e89469c7749d4127" |
| 119 | |
| 120 | // ServiceClient returns a generic service client for use in tests. |
| 121 | func ServiceClient() *gophercloud.ServiceClient { |
| 122 | return &gophercloud.ServiceClient{ |
| 123 | Provider: &gophercloud.ProviderClient{TokenID: TokenID}, |
| 124 | Endpoint: Endpoint(), |
| 125 | } |
| 126 | } |