11package controllers
22
33import (
4+ "app/base/core"
45 "encoding/json"
6+ "io"
7+ "net/http"
8+ "net/http/httptest"
9+ "regexp"
510 "testing"
611
12+ "github.com/gin-gonic/gin"
713 "github.com/stretchr/testify/assert"
814)
915
@@ -14,3 +20,63 @@ func ParseResponseBody(t *testing.T, bytes []byte, out interface{}) {
1420 err := json .Unmarshal (bytes , & out )
1521 assert .Nil (t , err , string (bytes ))
1622}
23+
24+ // Add content type to header
25+ func acceptContentType (req * http.Request , ct string ) {
26+ if ct != "" {
27+ req .Header .Add ("Accept" , ct )
28+ }
29+ }
30+
31+ // Fill params and append query string to routerPath to get url
32+ func getURLFromRouterPath (routerPath , param , queryString string ) string {
33+ pattern := regexp .MustCompile (":[a-zA-Z0-9_]+" )
34+ return pattern .ReplaceAllString (routerPath , param ) + queryString
35+ }
36+
37+ // Init request
38+ func prepareRequest (method string , url string , body io.Reader , ct string ) (w * httptest.ResponseRecorder ,
39+ req * http.Request ) {
40+ req , _ = http .NewRequest (method , url , body )
41+ acceptContentType (req , ct )
42+ return httptest .NewRecorder (), req
43+ }
44+
45+ // Create simple request
46+ func CreateRequest (method string , url string , body io.Reader , contentType string , handler gin.HandlerFunc ,
47+ contextKVs ... core.ContextKV ) (
48+ w * httptest.ResponseRecorder ) {
49+ w , req := prepareRequest (method , url , body , contentType )
50+ core .InitRouter (handler , contextKVs ... ).ServeHTTP (w , req )
51+ return w
52+ }
53+
54+ // Create request and initialize router with params
55+ func CreateRequestRouterWithParams (method , routerPath , param , queryString string , body io.Reader , contentType string ,
56+ handler gin.HandlerFunc , routerAccount int , contextKVs ... core.ContextKV ) (w * httptest.ResponseRecorder ) {
57+ w , req := prepareRequest (method , getURLFromRouterPath (routerPath , param , queryString ), body , contentType )
58+ core .InitRouterWithParams (handler , routerAccount , method , routerPath , contextKVs ... ).ServeHTTP (w , req )
59+ return w
60+ }
61+
62+ // Create request and initialize router with path
63+ func CreateRequestRouterWithPath (method , routerPath , param , queryString string , body io.Reader , contentType string ,
64+ handler gin.HandlerFunc , contextKVs ... core.ContextKV ) (w * httptest.ResponseRecorder ) {
65+ w , req := prepareRequest (method , getURLFromRouterPath (routerPath , param , queryString ), body , contentType )
66+ core .InitRouterWithPath (handler , routerPath , contextKVs ... ).ServeHTTP (w , req )
67+ return w
68+ }
69+
70+ // Create request and initialize router with account
71+ func CreateRequestRouterWithAccount (method , routerPath , param , queryString string , body io.Reader , contentType string ,
72+ handler gin.HandlerFunc , routerAccount int , contextKVs ... core.ContextKV ) (w * httptest.ResponseRecorder ) {
73+ w , req := prepareRequest (method , getURLFromRouterPath (routerPath , param , queryString ), body , contentType )
74+ core .InitRouterWithAccount (handler , routerPath , routerAccount , contextKVs ... ).ServeHTTP (w , req )
75+ return w
76+ }
77+
78+ // Check status and parse response body
79+ func CheckResponse (t * testing.T , w * httptest.ResponseRecorder , expectedStatus int , output interface {}) {
80+ assert .Equal (t , expectedStatus , w .Code )
81+ ParseResponseBody (t , w .Body .Bytes (), & output )
82+ }
0 commit comments