-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreg_routes_test.go
89 lines (73 loc) · 2.19 KB
/
reg_routes_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 regginservice_test
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/gin-gonic/gin"
restyv2 "github.com/go-resty/resty/v2"
"github.com/stretchr/testify/require"
"github.com/yyle88/reggin/regginservice"
)
var testServerURL string
func TestMain(m *testing.M) {
engine := gin.Default()
regginservice.AddEndpoints(engine, &mockService1{})
regginservice.SetRouteGroup(engine, "/api", &mockService2{})
regginservice.SetRoutes(engine, "/abc", &mockService3{})
serverUt := httptest.NewServer(engine)
defer serverUt.Close()
testServerURL = serverUt.URL
m.Run()
}
type mockService1 struct{}
func (s *mockService1) RegisterRoutes(router *gin.Engine) {
router.GET("/test/service", func(c *gin.Context) {
c.String(http.StatusOK, "Service endpoint hit")
})
}
type mockService2 struct{}
func (r *mockService2) RegisterRoutes(group *gin.RouterGroup) {
group.GET("/test/route", func(c *gin.Context) {
c.String(http.StatusOK, "Route group endpoint hit")
})
}
type mockService3 struct{}
func (m *mockService3) RegisterRoutes(router gin.IRouter) {
router.GET("/case/route", func(c *gin.Context) {
c.String(http.StatusOK, "Route endpoint hit")
})
}
func TestAddEndpoints(t *testing.T) {
resp, err := restyv2.New().SetRetryCount(3).
SetRetryWaitTime(time.Second * 2).
R().
Get(testServerURL + "/test/service")
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode())
message := string(resp.Body())
t.Log(message)
require.Contains(t, message, "Service endpoint hit")
}
func TestSetRouteGroup(t *testing.T) {
resp, err := restyv2.New().SetRetryCount(3).
SetRetryWaitTime(time.Second * 2).
R().
Get(testServerURL + "/api/test/route")
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode())
message := string(resp.Body())
t.Log(message)
require.Contains(t, message, "Route group endpoint hit")
}
func TestSetRoutes(t *testing.T) {
resp, err := restyv2.New().SetRetryCount(3).
SetRetryWaitTime(time.Second * 2).
R().
Get(testServerURL + "/abc/case/route")
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode())
message := string(resp.Body())
t.Log(message)
require.Contains(t, message, "Route endpoint hit")
}