@@ -2,11 +2,13 @@ package server
2
2
3
3
import (
4
4
"context"
5
- "github.com/rameshsunkara/go-rest-api-example/internal/models"
6
5
"net/http"
7
6
"testing"
8
7
"time"
9
8
9
+ "github.com/rameshsunkara/go-rest-api-example/internal/models"
10
+ "github.com/stretchr/testify/assert"
11
+
10
12
"github.com/gin-gonic/gin"
11
13
"go.mongodb.org/mongo-driver/mongo"
12
14
"go.mongodb.org/mongo-driver/mongo/options"
@@ -37,12 +39,20 @@ func (m *MockMongoDataBase) Collection(name string, opts ...*options.CollectionO
37
39
func TestListOfRoutes (t * testing.T ) {
38
40
router := WebRouter (svcInfo , & MockMongoDBClient {}, & MockMongoDataBase {})
39
41
list := router .Routes ()
42
+ mode := gin .Mode ()
43
+
44
+ assert .Equal (t , gin .ReleaseMode , mode )
40
45
41
46
assertRoutePresent (t , list , gin.RouteInfo {
42
47
Method : http .MethodGet ,
43
48
Path : "/status" ,
44
49
})
45
50
51
+ assertRouteNotPresent (t , list , gin.RouteInfo {
52
+ Method : http .MethodPost ,
53
+ Path : "/seedDB" ,
54
+ })
55
+
46
56
assertRoutePresent (t , list , gin.RouteInfo {
47
57
Method : http .MethodGet ,
48
58
Path : "/api/v1/orders" ,
@@ -70,6 +80,20 @@ func TestListOfRoutes(t *testing.T) {
70
80
71
81
}
72
82
83
+ func TestModeSpecificRoutes (t * testing.T ) {
84
+ svcInfo .Environment = "dev"
85
+ router := WebRouter (svcInfo , & MockMongoDBClient {}, & MockMongoDataBase {})
86
+ list := router .Routes ()
87
+ mode := gin .Mode ()
88
+
89
+ assert .Equal (t , gin .DebugMode , mode )
90
+
91
+ assertRoutePresent (t , list , gin.RouteInfo {
92
+ Method : http .MethodPost ,
93
+ Path : "/seedDB" ,
94
+ })
95
+ }
96
+
73
97
func assertRoutePresent (t * testing.T , gotRoutes gin.RoutesInfo , wantRoute gin.RouteInfo ) {
74
98
for _ , gotRoute := range gotRoutes {
75
99
if gotRoute .Path == wantRoute .Path && gotRoute .Method == wantRoute .Method {
@@ -78,3 +102,11 @@ func assertRoutePresent(t *testing.T, gotRoutes gin.RoutesInfo, wantRoute gin.Ro
78
102
}
79
103
t .Errorf ("route not found: %v" , wantRoute )
80
104
}
105
+
106
+ func assertRouteNotPresent (t * testing.T , gotRoutes gin.RoutesInfo , wantRoute gin.RouteInfo ) {
107
+ for _ , gotRoute := range gotRoutes {
108
+ if gotRoute .Path == wantRoute .Path && gotRoute .Method == wantRoute .Method {
109
+ t .Errorf ("route found: %v" , wantRoute )
110
+ }
111
+ }
112
+ }
0 commit comments