| 1 |
// Utility functions to help writing tests for a Go-Json-Rest app |
| 2 |
// |
| 3 |
// Go comes with net/http/httptest to help writing test for an http |
| 4 |
// server. When this http server implements a JSON REST API, some basic |
| 5 |
// checks end up to be always the same. This test package tries to save |
| 6 |
// some typing by providing helpers for this particular use case. |
| 7 |
// |
| 8 |
// package main |
| 9 |
// |
| 10 |
// import ( |
| 11 |
// "github.com/ant0ine/go-json-rest/rest" |
| 12 |
// "github.com/ant0ine/go-json-rest/rest/test" |
| 13 |
// "testing" |
| 14 |
// ) |
| 15 |
// |
| 16 |
// func TestSimpleRequest(t *testing.T) { |
| 17 |
// api := rest.NewApi() |
| 18 |
// api.Use(rest.DefaultDevStack...) |
| 19 |
// router, err := rest.MakeRouter( |
| 20 |
// rest.Get("/r", func(w rest.ResponseWriter, r *rest.Request) { |
| 21 |
// w.WriteJson(map[string]string{"Id": "123"}) |
| 22 |
// }), |
| 23 |
// ) |
| 24 |
// if err != nil { |
| 25 |
// log.Fatal(err) |
| 26 |
// } |
| 27 |
// api.SetApp(router) |
| 28 |
// recorded := test.RunRequest(t, api.MakeHandler(), |
| 29 |
// test.MakeSimpleRequest("GET", "http://1.2.3.4/r", nil)) |
| 30 |
// recorded.CodeIs(200) |
| 31 |
// recorded.ContentTypeIsJson() |
| 32 |
// } |
| 33 |
package test |