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