Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

increased coverage

goenningac62f9e16545
diff --git a/handler/status_test.go b/handler/status_test.go
index 29c50e18f..e50a62e25 100644
--- a/handler/status_test.go
+++ b/handler/status_test.go
@@ -5,29 +5,29 @@ import (
"github.com/WeCanHearYou/wchy/context"
"github.com/WeCanHearYou/wchy/handler"
+ "github.com/WeCanHearYou/wchy/mock"
. "github.com/onsi/gomega"
)
-type inMemoryHealthCheckService struct {
- status bool
-}
+type falsyHealthCheckService struct{}
-func (svc inMemoryHealthCheckService) IsDatabaseOnline() bool {
- return svc.status
+func (svc falsyHealthCheckService) IsDatabaseOnline() bool {
+ return false
}
func TestStatusHandler(t *testing.T) {
RegisterTestingT(t)
+
ctx := &context.WchyContext{
- Health: &inMemoryHealthCheckService{status: false},
+ Health: &falsyHealthCheckService{},
Settings: context.WchySettings{
BuildTime: "today",
},
}
- server := NewTestServer()
- server.register(handler.Status(ctx))
- status, query := server.request()
+ server := mock.NewServer()
+ server.Register(handler.Status(ctx))
+ status, query := server.Request()
Expect(query.String("build")).To(Equal("today"))
Expect(query.Bool("healthy", "database")).To(Equal(false))
diff --git a/handler/tenant_test.go b/handler/tenant_test.go
index b27d36106..bce112c90 100644
--- a/handler/tenant_test.go
+++ b/handler/tenant_test.go
@@ -5,38 +5,32 @@ import (
"github.com/WeCanHearYou/wchy/context"
"github.com/WeCanHearYou/wchy/handler"
+ "github.com/WeCanHearYou/wchy/mock"
"github.com/WeCanHearYou/wchy/model"
"github.com/WeCanHearYou/wchy/service"
. "github.com/onsi/gomega"
)
-type inMemoryTenantService struct {
- tenants []*model.Tenant
-}
+type mockTenantService struct{}
-func (svc inMemoryTenantService) GetByDomain(domain string) (*model.Tenant, error) {
- for _, tenant := range svc.tenants {
- if tenant.Domain == domain {
- return tenant, nil
- }
+func (svc mockTenantService) GetByDomain(domain string) (*model.Tenant, error) {
+ if domain == "trishop" {
+ return &model.Tenant{ID: 2, Name: "The Triathlon Shop", Domain: "trishop"}, nil
}
return nil, service.ErrNotFound
}
var ctx *context.WchyContext = &context.WchyContext{
- Tenant: &inMemoryTenantService{tenants: []*model.Tenant{
- &model.Tenant{ID: 1, Name: "Orange Inc.", Domain: "orange"},
- &model.Tenant{ID: 2, Name: "The Triathlon Shop", Domain: "trishop"},
- }},
+ Tenant: &mockTenantService{},
}
func TestTenantHandler_404(t *testing.T) {
RegisterTestingT(t)
- server := NewTestServer()
- server.param("domain", "unknown")
- server.register(handler.TenantByDomain(ctx))
- status, _ := server.request()
+ server := mock.NewServer()
+ server.Param("domain", "unknown")
+ server.Register(handler.TenantByDomain(ctx))
+ status, _ := server.Request()
Expect(status).To(Equal(404))
}
@@ -44,10 +38,10 @@ func TestTenantHandler_404(t *testing.T) {
func TestTenantHandler_200(t *testing.T) {
RegisterTestingT(t)
- server := NewTestServer()
- server.param("domain", "trishop")
- server.register(handler.TenantByDomain(ctx))
- status, query := server.request()
+ server := mock.NewServer()
+ server.Param("domain", "trishop")
+ server.Register(handler.TenantByDomain(ctx))
+ status, query := server.Request()
Expect(query.Int("id")).To(Equal(2))
Expect(query.String("name")).To(Equal("The Triathlon Shop"))
diff --git a/handler/handler_helper_test.go b/mock/server.go
similarity index 51%
rename from handler/handler_helper_test.go
rename to mock/server.go
index c0a6a5970..f1d8af37b 100644
--- a/handler/handler_helper_test.go
+++ b/mock/server.go
@@ -1,47 +1,56 @@
-package handler_test
+package mock
import (
"encoding/json"
"net/http"
"net/http/httptest"
+ "fmt"
+
"github.com/gin-gonic/gin"
"github.com/jmoiron/jsonq"
)
-type testServer struct {
+// Server is a HTTP server wrapper for testing purpose
+type Server struct {
engine *gin.Engine
}
-func NewTestServer() *testServer {
+// NewServer creates a new test server
+func NewServer() *Server {
engine := gin.New()
- return &testServer{
+ return &Server{
engine: engine,
}
}
-func (s *testServer) do(f gin.HandlerFunc) {
+// Use to register new middlewares
+func (s *Server) Use(f gin.HandlerFunc) {
s.engine.Use(f)
}
-func (s *testServer) param(key, value string) {
- s.do(func(c *gin.Context) {
+// Param can be used to set request parameters
+func (s *Server) Param(key, value string) {
+ s.Use(func(c *gin.Context) {
c.Params = append(c.Params, gin.Param{Key: key, Value: value})
})
}
-func (s *testServer) register(handler gin.HandlerFunc) {
+// Register given handler
+func (s *Server) Register(handler gin.HandlerFunc) {
s.engine.Handle("GET", "/", handler)
}
-func (s *testServer) request() (int, *jsonq.JsonQuery) {
+// Request registered handler
+func (s *Server) Request() (int, *jsonq.JsonQuery) {
request, _ := http.NewRequest("GET", "/", nil)
response := httptest.NewRecorder()
s.engine.ServeHTTP(response, request)
status := response.Result().StatusCode
+ fmt.Println(status)
- if status == 200 {
+ if status == 200 && response.Body.Len() > 0 {
var data interface{}
decoder := json.NewDecoder(response.Body)
decoder.Decode(&data)
diff --git a/router/multitenant.go b/router/multitenant.go
new file mode 100644
index 000000000..ba2a2d97a
--- /dev/null
+++ b/router/multitenant.go
@@ -0,0 +1,30 @@
+package router
+
+import (
+ "strings"
+
+ "github.com/WeCanHearYou/wchy/context"
+ "github.com/gin-gonic/gin"
+)
+
+// MultiTenant extract tenant information from hostname and inject it into current context
+func MultiTenant(ctx *context.WchyContext) gin.HandlerFunc {
+ return func(c *gin.Context) {
+ hostname := stripPort(c.Request.Host)
+ tenant, err := ctx.Tenant.GetByDomain(hostname)
+ if err == nil {
+ c.Set("Tenant", tenant.Name)
+ c.Next()
+ } else {
+ c.AbortWithStatus(404)
+ }
+ }
+}
+
+func stripPort(hostport string) string {
+ colon := strings.IndexByte(hostport, ':')
+ if colon == -1 {
+ return hostport
+ }
+ return hostport[:colon]
+}
diff --git a/router/router.go b/router/router.go
index 06a1e8509..77c5e88a3 100644
--- a/router/router.go
+++ b/router/router.go
@@ -4,7 +4,6 @@ import (
"net/http"
"os"
"path/filepath"
- "strings"
"github.com/WeCanHearYou/wchy/context"
"github.com/WeCanHearYou/wchy/handler"
@@ -13,30 +12,6 @@ import (
"github.com/gin-gonic/gin"
)
-func multiTenant(ctx *context.WchyContext) gin.HandlerFunc {
- return func(c *gin.Context) {
- hostname := stripPort(c.Request.Host)
- tenant, err := ctx.Tenant.GetByDomain(hostname)
- if err == nil {
- c.Set("Tenant", tenant.Name)
- c.Next()
- } else {
- c.Status(404)
- }
- }
-}
-
-func stripPort(hostport string) string {
- colon := strings.IndexByte(hostport, ':')
- if colon == -1 {
- return hostport
- }
- if i := strings.IndexByte(hostport, ']'); i != -1 {
- return strings.TrimPrefix(hostport[:i], "[")
- }
- return hostport[:colon]
-}
-
// GetMainEngine returns main HTTP engine
func GetMainEngine(ctx *context.WchyContext) *gin.Engine {
router := gin.New()
@@ -48,7 +23,7 @@ func GetMainEngine(ctx *context.WchyContext) *gin.Engine {
gin.SetMode(gin.ReleaseMode)
}
- router.Use(multiTenant(ctx))
+ router.Use(MultiTenant(ctx))
if env.IsTest() {
router.LoadHTMLGlob(filepath.Join(os.Getenv("GOPATH"), "src/github.com/WeCanHearYou/wchy/views/*"))
diff --git a/router/router_test.go b/router/router_test.go
index a1cc2e4e4..83b8080a5 100644
--- a/router/router_test.go
+++ b/router/router_test.go
@@ -1,23 +1,100 @@
-package router
+package router_test
import (
"testing"
+ "github.com/WeCanHearYou/wchy/context"
+ "github.com/WeCanHearYou/wchy/mock"
+ "github.com/WeCanHearYou/wchy/model"
+ "github.com/WeCanHearYou/wchy/router"
+ "github.com/WeCanHearYou/wchy/service"
+ "github.com/gin-gonic/gin"
. "github.com/onsi/gomega"
)
+var testCases = []struct {
+ domain string
+ tenant *model.Tenant
+ hosts []string
+}{
+ {
+ "orange.test.canhearyou.com",
+ &model.Tenant{Name: "The Orange Inc."},
+ []string{
+ "orange.test.canhearyou.com",
+ "orange.test.canhearyou.com:3000",
+ },
+ },
+ {
+ "trishop.test.canhearyou.com",
+ &model.Tenant{Name: "The Triathlon Shop"},
+ []string{
+ "trishop.test.canhearyou.com",
+ "trishop.test.canhearyou.com:1231",
+ "trishop.test.canhearyou.com:80",
+ },
+ },
+}
+
+type mockTenantService struct{}
+
+func (svc mockTenantService) GetByDomain(domain string) (*model.Tenant, error) {
+ for _, testCase := range testCases {
+ if testCase.domain == domain {
+ return testCase.tenant, nil
+ }
+ }
+ return nil, service.ErrNotFound
+}
+
+var ctx *context.WchyContext = &context.WchyContext{
+ Tenant: &mockTenantService{},
+}
+
func TestRouter(t *testing.T) {
RegisterTestingT(t)
- r := GetMainEngine(nil)
+ r := router.GetMainEngine(nil)
Expect(r).ToNot(BeNil())
}
-func TestStripPort(t *testing.T) {
+func TestMultiTenant(t *testing.T) {
RegisterTestingT(t)
- Expect(stripPort("localhost:3000")).To(Equal("localhost"))
- Expect(stripPort("mypage.com:3000")).To(Equal("mypage.com"))
- Expect(stripPort("sub.mypage.com:3000")).To(Equal("sub.mypage.com"))
- Expect(stripPort("mypage.com")).To(Equal("mypage.com"))
+ for _, testCase := range testCases {
+ for _, host := range testCase.hosts {
+ var name interface{}
+
+ server := mock.NewServer()
+ server.Use(func(c *gin.Context) {
+ c.Request.Host = host
+ })
+ server.Use(router.MultiTenant(ctx))
+ server.Register(func(c *gin.Context) {
+ c.Status(200)
+ name = c.MustGet("Tenant")
+ })
+ status, _ := server.Request()
+
+ Expect(status).To(Equal(200))
+ Expect(name).To(Equal(testCase.tenant.Name))
+ }
+ }
+
+}
+
+func TestMultiTenant_UnknownDomain(t *testing.T) {
+ RegisterTestingT(t)
+
+ server := mock.NewServer()
+ server.Use(func(c *gin.Context) {
+ c.Request.Host = "somedomain.com"
+ })
+ server.Use(router.MultiTenant(ctx))
+ server.Register(func(c *gin.Context) {
+ c.Status(200)
+ })
+ status, _ := server.Request()
+
+ Expect(status).To(Equal(404))
}