Add Dev router config

This commit is contained in:
Jannis Mattheis 2018-03-15 21:51:23 +01:00 committed by Jannis Mattheis
parent ee37eafd36
commit 2e29efe90b
2 changed files with 36 additions and 2 deletions

View File

@ -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.

View File

@ -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")