@@ -2,13 +2,17 @@ package server
22
33import (
44 "bytes"
5+ "context"
56 "io"
67 "net/http"
78 "net/http/httptest"
89 "strings"
910 "testing"
1011
12+ "github.com/gin-gonic/gin"
1113 "github.com/lukaszbudnik/migrator/config"
14+ "github.com/lukaszbudnik/migrator/coordinator"
15+ "github.com/lukaszbudnik/migrator/types"
1216 "github.com/stretchr/testify/assert"
1317)
1418
@@ -22,6 +26,11 @@ func newTestRequest(method, url string, body io.Reader) (*http.Request, error) {
2226 return http .NewRequest (method , versionURL , body )
2327}
2428
29+ func testSetupRouter (config * config.Config , newCoordinator func (ctx context.Context , config * config.Config ) coordinator.Coordinator ) * gin.Engine {
30+ versionInfo := & types.VersionInfo {Release : "GitBranch" , CommitSha : "GitCommitSha" , CommitDate : "2020-01-08T09:56:41+01:00" , APIVersions : []string {"v1" }}
31+ return SetupRouter (versionInfo , config , newCoordinator )
32+ }
33+
2534func TestGetDefaultPort (t * testing.T ) {
2635 config , err := config .FromFile (configFile )
2736 assert .Nil (t , err )
@@ -34,13 +43,30 @@ func TestGetDefaultPortOverrides(t *testing.T) {
3443 assert .Equal (t , "8811" , GetPort (config ))
3544}
3645
46+ // section /
47+
48+ func TestRoot (t * testing.T ) {
49+ config , err := config .FromFile (configFile )
50+ assert .Nil (t , err )
51+
52+ router := testSetupRouter (config , nil )
53+
54+ w := httptest .NewRecorder ()
55+ req , _ := http .NewRequest ("GET" , "/" , nil )
56+ router .ServeHTTP (w , req )
57+
58+ assert .Equal (t , http .StatusOK , w .Code )
59+ assert .Equal (t , "application/json; charset=utf-8" , w .HeaderMap ["Content-Type" ][0 ])
60+ assert .Equal (t , `{"release":"GitBranch","commitSha":"GitCommitSha","commitDate":"2020-01-08T09:56:41+01:00","apiVersions":["v1"]}` , strings .TrimSpace (w .Body .String ()))
61+ }
62+
3763// section /config
3864
3965func TestConfigRoute (t * testing.T ) {
4066 config , err := config .FromFile (configFile )
4167 assert .Nil (t , err )
4268
43- router := SetupRouter (config , nil )
69+ router := testSetupRouter (config , nil )
4470
4571 w := httptest .NewRecorder ()
4672 req , _ := newTestRequest ("GET" , "/config" , nil )
@@ -57,7 +83,7 @@ func TestDiskMigrationsRoute(t *testing.T) {
5783 config , err := config .FromFile (configFile )
5884 assert .Nil (t , err )
5985
60- router := SetupRouter (config , newMockedCoordinator )
86+ router := testSetupRouter (config , newMockedCoordinator )
6187
6288 w := httptest .NewRecorder ()
6389 req , _ := newTestRequest ("GET" , "/migrations/source" , nil )
@@ -74,7 +100,7 @@ func TestAppliedMigrationsRoute(t *testing.T) {
74100 config , err := config .FromFile (configFile )
75101 assert .Nil (t , err )
76102
77- router := SetupRouter (config , newMockedCoordinator )
103+ router := testSetupRouter (config , newMockedCoordinator )
78104
79105 req , _ := newTestRequest (http .MethodGet , "/migrations/applied" , nil )
80106
@@ -92,7 +118,7 @@ func TestMigrationsPostRoute(t *testing.T) {
92118 config , err := config .FromFile (configFile )
93119 assert .Nil (t , err )
94120
95- router := SetupRouter (config , newMockedCoordinator )
121+ router := testSetupRouter (config , newMockedCoordinator )
96122
97123 json := []byte (`{"mode": "apply", "response": "full"}` )
98124 req , _ := newTestRequest (http .MethodPost , "/migrations" , bytes .NewBuffer (json ))
@@ -109,7 +135,7 @@ func TestMigrationsPostRouteSummaryResponse(t *testing.T) {
109135 config , err := config .FromFile (configFile )
110136 assert .Nil (t , err )
111137
112- router := SetupRouter (config , newMockedCoordinator )
138+ router := testSetupRouter (config , newMockedCoordinator )
113139
114140 json := []byte (`{"mode": "apply", "response": "summary"}` )
115141 req , _ := newTestRequest (http .MethodPost , "/migrations" , bytes .NewBuffer (json ))
@@ -127,7 +153,7 @@ func TestMigrationsPostRouteBadRequest(t *testing.T) {
127153 config , err := config .FromFile (configFile )
128154 assert .Nil (t , err )
129155
130- router := SetupRouter (config , newMockedCoordinator )
156+ router := testSetupRouter (config , newMockedCoordinator )
131157
132158 // response is invalid
133159 json := []byte (`{"mode": "apply", "response": "abc"}` )
@@ -145,7 +171,7 @@ func TestMigrationsPostRouteCheckSumError(t *testing.T) {
145171 config , err := config .FromFile (configFile )
146172 assert .Nil (t , err )
147173
148- router := SetupRouter (config , newMockedErrorCoordinator (0 ))
174+ router := testSetupRouter (config , newMockedErrorCoordinator (0 ))
149175
150176 json := []byte (`{"mode": "apply", "response": "full"}` )
151177 req , _ := newTestRequest (http .MethodPost , "/migrations" , bytes .NewBuffer (json ))
@@ -164,7 +190,7 @@ func TestTenantsGetRoute(t *testing.T) {
164190 config , err := config .FromFile (configFile )
165191 assert .Nil (t , err )
166192
167- router := SetupRouter (config , newMockedCoordinator )
193+ router := testSetupRouter (config , newMockedCoordinator )
168194
169195 w := httptest .NewRecorder ()
170196 req , _ := newTestRequest ("GET" , "/tenants" , nil )
@@ -179,7 +205,7 @@ func TestTenantsPostRoute(t *testing.T) {
179205 config , err := config .FromFile (configFile )
180206 assert .Nil (t , err )
181207
182- router := SetupRouter (config , newMockedCoordinator )
208+ router := testSetupRouter (config , newMockedCoordinator )
183209
184210 json := []byte (`{"name": "new_tenant", "response": "full", "mode":"dry-run"}` )
185211 req , _ := newTestRequest (http .MethodPost , "/tenants" , bytes .NewBuffer (json ))
@@ -196,7 +222,7 @@ func TestTenantsPostRouteSummaryResponse(t *testing.T) {
196222 config , err := config .FromFile (configFile )
197223 assert .Nil (t , err )
198224
199- router := SetupRouter (config , newMockedCoordinator )
225+ router := testSetupRouter (config , newMockedCoordinator )
200226
201227 json := []byte (`{"name": "new_tenant", "response": "summary", "mode":"dry-run"}` )
202228 req , _ := newTestRequest (http .MethodPost , "/tenants" , bytes .NewBuffer (json ))
@@ -214,7 +240,7 @@ func TestTenantsPostRouteBadRequestError(t *testing.T) {
214240 config , err := config .FromFile (configFile )
215241 assert .Nil (t , err )
216242
217- router := SetupRouter (config , newMockedCoordinator )
243+ router := testSetupRouter (config , newMockedCoordinator )
218244
219245 json := []byte (`{"a": "new_tenant"}` )
220246 req , _ := newTestRequest (http .MethodPost , "/tenants" , bytes .NewBuffer (json ))
@@ -231,7 +257,7 @@ func TestTenantsPostRouteCheckSumError(t *testing.T) {
231257 config , err := config .FromFile (configFile )
232258 assert .Nil (t , err )
233259
234- router := SetupRouter (config , newMockedErrorCoordinator (0 ))
260+ router := testSetupRouter (config , newMockedErrorCoordinator (0 ))
235261
236262 json := []byte (`{"name": "new_tenant", "response": "full", "mode":"dry-run"}` )
237263 req , _ := newTestRequest (http .MethodPost , "/tenants" , bytes .NewBuffer (json ))
@@ -248,7 +274,7 @@ func TestRouteError(t *testing.T) {
248274 config , err := config .FromFile (configFile )
249275 assert .Nil (t , err )
250276
251- router := SetupRouter (config , newMockedErrorCoordinator (0 ))
277+ router := testSetupRouter (config , newMockedErrorCoordinator (0 ))
252278
253279 w := httptest .NewRecorder ()
254280 req , _ := newTestRequest ("GET" , "/migrations/source" , nil )
0 commit comments