/
TDAPITest_test.go
89 lines (73 loc) · 1.95 KB
/
TDAPITest_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package TDApiTest_test
import (
"encoding/json"
"github.com/jeanlebrument/TDApiTest"
"github.com/jeanlebrument/TDApiTest/Godeps/_workspace/src/github.com/gorilla/mux"
"github.com/jeanlebrument/TDApiTest/Godeps/_workspace/src/github.com/stretchr/testify/assert"
"github.com/jeanlebrument/TDApiTest/Godeps/_workspace/src/github.com/unrolled/render"
"net/http"
"net/url"
"testing"
)
const (
GET = "GET"
PathHello = "/hello"
)
var headerValueArr = []string{"Content-Type", "application/x-www-form-urlencoded; param=value"}
var headerValueMap = map[string]string{"Content-Type": "application/x-www-form-urlencoded; param=value"}
var urlParams = url.Values{"key": {"Happy", "Coding"}}
type UT struct {
t *testing.T
}
func newUT(t *testing.T) *UT {
return &UT{t: t}
}
func (ut *UT) Hello(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
assert.Nil(ut.t, err)
render.New().JSON(w, http.StatusOK, r.Form)
}
func newRouter(t *testing.T) *mux.Router {
router := mux.NewRouter().StrictSlash(true)
router.Methods(GET).
Path(PathHello).
Name("Hello").
Headers(headerValueArr...).
Handler(http.HandlerFunc(newUT(t).Hello))
return router
}
func checkHelloRoute(t *testing.T, result string) {
expected, err := json.Marshal(urlParams)
assert.Nil(t, err)
assert.Equal(t, result, string(expected))
}
func TestGetRoutes(t *testing.T) {
td := TDApiTest.NewTDApiTest(newRouter(t))
assert.NotNil(t, td)
td.TestContainers = TDApiTest.TestContainers{
{
Method: GET,
Path: PathHello,
TestsToRun: TDApiTest.TestsToRun{
{
Desc: "Hello route",
Status: http.StatusOK,
Header: headerValueMap,
Params: urlParams,
TestFunc: checkHelloRoute,
},
{
Desc: "Hello route without test func",
Status: http.StatusOK,
Params: urlParams,
Header: headerValueMap,
},
{
Desc: "Hello route without header",
Status: http.StatusNotFound,
},
},
},
}
td.RunTests(t)
}