From 2e29efe90b8cf340bfcfc6e3afa11c89ebfbbd61 Mon Sep 17 00:00:00 2001 From: Jannis Mattheis Date: Thu, 15 Mar 2018 21:51:23 +0100 Subject: [PATCH] Add Dev router config --- router/router.go | 8 ++++++++ router/router_test.go | 30 ++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/router/router.go b/router/router.go index aff357b..4e1bf75 100644 --- a/router/router.go +++ b/router/router.go @@ -16,6 +16,7 @@ import ( "github.com/gotify/server/docs" "github.com/gotify/server/model" "github.com/gotify/server/stream" + "github.com/gotify/server/mode" ) // Create creates the gin engine with all routes. @@ -36,8 +37,15 @@ func Create(db *database.GormDatabase, vInfo *model.VersionInfo, conf *config.Co g.Use(func(ctx *gin.Context) { ctx.Header("Content-Type", "application/json") + if mode.IsDev() { + ctx.Header("Access-Control-Allow-Origin", "*") + ctx.Header("Access-Control-Allow-Methods", "GET,POST,DELETE,OPTIONS,PUT") + ctx.Header("Access-Control-Allow-Headers", "X-Gotify-Key,Authorization,Content-Type,Upgrade,Origin,Connection,Accept-Encoding,Accept-Language,Host") + } }) + g.OPTIONS("/*any") + // swagger:operation GET /version version getVersion // // Get version information. diff --git a/router/router_test.go b/router/router_test.go index 2a9b1ff..acb2ea5 100644 --- a/router/router_test.go +++ b/router/router_test.go @@ -9,13 +9,13 @@ import ( "strings" "testing" - "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/json" "github.com/gotify/server/database" "github.com/gotify/server/model" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" "github.com/gotify/server/config" + "github.com/gotify/server/mode" ) var ( @@ -35,7 +35,7 @@ type IntegrationSuite struct { } func (s *IntegrationSuite) BeforeTest(string, string) { - gin.SetMode(gin.TestMode) + mode.Set(mode.TestDev) var err error s.db, err = database.New("sqlite3", "itest.db", "admin", "pw", 5) assert.Nil(s.T(), err) @@ -57,6 +57,32 @@ func (s *IntegrationSuite) TestVersionInfo() { doRequestAndExpect(s.T(), req, 200, `{"version":"1.0.0", "commit":"asdasds", "buildDate":"2018-02-20-17:30:47"}`) } +func (s *IntegrationSuite) TestHeaderInDev() { + mode.Set(mode.TestDev) + req := s.newRequest("GET", "version", "") + + res, err := client.Do(req) + assert.Nil(s.T(), err) + assert.NotEmpty(s.T(), res.Header.Get("Access-Control-Allow-Origin")) +} + +func (s *IntegrationSuite) TestHeaderInProd() { + mode.Set(mode.Prod) + req := s.newRequest("GET", "version", "") + + res, err := client.Do(req) + assert.Nil(s.T(), err) + assert.Empty(s.T(), res.Header.Get("Access-Control-Allow-Origin")) +} + +func (s *IntegrationSuite) TestOptionsRequest() { + req := s.newRequest("OPTIONS", "version", "") + + res, err := client.Do(req) + assert.Nil(s.T(), err) + assert.Equal(s.T(), res.StatusCode, 200) +} + func (s *IntegrationSuite) TestSendMessage() { req := s.newRequest("POST", "application", `{"name": "backup-server"}`) req.SetBasicAuth("admin", "pw")