Jamie Hannaford | b2a59c5 | 2015-02-17 14:56:17 +0100 | [diff] [blame] | 1 | package fixture |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 8 | th "github.com/gophercloud/gophercloud/testhelper" |
| 9 | "github.com/gophercloud/gophercloud/testhelper/client" |
Jamie Hannaford | b2a59c5 | 2015-02-17 14:56:17 +0100 | [diff] [blame] | 10 | ) |
| 11 | |
| 12 | func SetupHandler(t *testing.T, url, method, requestBody, responseBody string, status int) { |
| 13 | th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) { |
| 14 | th.TestMethod(t, r, method) |
| 15 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 16 | |
| 17 | if requestBody != "" { |
| 18 | th.TestJSONRequest(t, r, requestBody) |
| 19 | } |
| 20 | |
| 21 | if responseBody != "" { |
| 22 | w.Header().Add("Content-Type", "application/json") |
| 23 | } |
| 24 | |
| 25 | w.WriteHeader(status) |
| 26 | |
| 27 | if responseBody != "" { |
| 28 | fmt.Fprintf(w, responseBody) |
| 29 | } |
| 30 | }) |
| 31 | } |