blob: 65dc66b9567d581a9d06260d57a1bb930400091a [file] [log] [blame]
Ash Wilsondd580ce2014-08-28 15:37:55 -04001package testhelper
2
3import (
4 "encoding/json"
5 "io/ioutil"
6 "net/http"
Ash Wilson0ab4d612014-08-29 11:10:13 -04007 "net/http/httptest"
Ash Wilsondd580ce2014-08-28 15:37:55 -04008 "net/url"
9 "reflect"
10 "testing"
Jamie Hannaford539e8d92014-10-02 17:27:51 +020011
12 "github.com/rackspace/gophercloud"
Ash Wilsondd580ce2014-08-28 15:37:55 -040013)
14
Ash Wilson0ab4d612014-08-29 11:10:13 -040015var (
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.
24func SetupHTTP() {
25 Mux = http.NewServeMux()
26 Server = httptest.NewServer(Mux)
27}
28
29// TeardownHTTP releases HTTP-related resources.
30func TeardownHTTP() {
31 Server.Close()
32}
33
34// Endpoint returns a fake endpoint that will actually target the Mux.
35func Endpoint() string {
36 return Server.URL + "/"
37}
38
Ash Wilsondd580ce2014-08-28 15:37:55 -040039// TestFormValues ensures that all the URL parameters given to the http.Request are the same as values.
40func 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).
53func 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.
60func 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.
67func 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.
80func 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 Wilson9e3f7152014-08-28 16:02:49 -040099 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 Wilsondd580ce2014-08-28 15:37:55 -0400113 t.Errorf("Response body did not contain the correct JSON.")
114 }
115}
Jamie Hannaford539e8d92014-10-02 17:27:51 +0200116
117// Fake token to use.
118const TokenID = "cbc36478b0bd8e67e89469c7749d4127"
119
120// ServiceClient returns a generic service client for use in tests.
121func ServiceClient() *gophercloud.ServiceClient {
122 return &gophercloud.ServiceClient{
123 Provider: &gophercloud.ProviderClient{TokenID: TokenID},
124 Endpoint: Endpoint(),
125 }
126}