Skip to content

Commit e09f2a7

Browse files
committed
Merge pull request #70 from GlassyMedia/feature/test-doc
as requested, reverting MakeRequestWitHeader and adding more godoc to test package
2 parents 453162e + 3b2da48 commit e09f2a7

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

Diff for: rest/test/doc.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
// Example:
9+
//
10+
// package main
11+
//
12+
// import (
13+
// "github.com/ant0ine/go-json-rest/rest/test"
14+
// "testing"
15+
// )
16+
//
17+
// func TestSimpleRequest(t *testing.T) {
18+
// handler := ResourceHandler{}
19+
// handler.SetRoutes(
20+
// &Route{"GET", "/r",
21+
// func(w ResponseWriter, r *Request) {
22+
// w.WriteJson(map[string]string{"Id": "123"})
23+
// },
24+
// },
25+
// )
26+
// recorded := test.RunRequest(t, &handler,
27+
// test.MakeSessionRequest("POST", "http://1.2.3.4/r", nil))
28+
// recorded.CodeIs(200)
29+
// recorded.ContentTypeIsJson()
30+
// }
31+
package test

Diff for: rest/test/util.go

+7-15
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
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.
71
package test
82

93
import (
@@ -16,8 +10,10 @@ import (
1610
"testing"
1711
)
1812

19-
func MakeRequestWithHeader(method, urlStr string, header http.Header, payload interface{}) *http.Request {
20-
s := ""
13+
// MakeSimpleRequest returns a http.Request. The returned request object can be
14+
// further prepared by adding headers and query string parmaters, for instance.
15+
func MakeSimpleRequest(method string, urlStr string, payload interface{}) *http.Request {
16+
var s string
2117

2218
if payload != nil {
2319
b, err := json.Marshal(payload)
@@ -31,27 +27,22 @@ func MakeRequestWithHeader(method, urlStr string, header http.Header, payload in
3127
if err != nil {
3228
panic(err)
3329
}
34-
r.Header = header
3530
r.Header.Set("Accept-Encoding", "gzip")
36-
3731
if payload != nil {
3832
r.Header.Set("Content-Type", "application/json")
3933
}
4034

4135
return r
4236
}
4337

44-
func MakeSimpleRequest(method string, urlStr string, payload interface{}) *http.Request {
45-
return MakeRequestWithHeader(method, urlStr, http.Header{}, payload)
46-
}
47-
38+
// CodeIs compares the rescorded status code
4839
func CodeIs(t *testing.T, r *httptest.ResponseRecorder, expectedCode int) {
4940
if r.Code != expectedCode {
5041
t.Errorf("Code %d expected, got: %d", expectedCode, r.Code)
5142
}
5243
}
5344

54-
// Test the first value for the given headerKey
45+
// HeaderIs tests the first value for the given headerKey
5546
func HeaderIs(t *testing.T, r *httptest.ResponseRecorder, headerKey, expectedValue string) {
5647
value := r.HeaderMap.Get(headerKey)
5748
if value != expectedValue {
@@ -96,6 +87,7 @@ type Recorded struct {
9687
Recorder *httptest.ResponseRecorder
9788
}
9889

90+
// RunRequest runs a HTTP request through the given handler
9991
func RunRequest(t *testing.T, handler http.Handler, request *http.Request) *Recorded {
10092
recorder := httptest.NewRecorder()
10193
handler.ServeHTTP(recorder, request)

0 commit comments

Comments
 (0)