Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

testing index page

goenning264ebb13e22e
diff --git a/handler/index.go b/handler/index.go
index b9568fb5e..80d5ed6a9 100644
--- a/handler/index.go
+++ b/handler/index.go
@@ -1,8 +1,6 @@
package handler
import (
- "net/http"
-
"github.com/WeCanHearYou/wchy/context"
"github.com/gin-gonic/gin"
)
@@ -18,8 +16,8 @@ func Index(ctx *context.WchyContext) gin.HandlerFunc {
func (h indexHandlder) get() gin.HandlerFunc {
return func(c *gin.Context) {
- c.HTML(http.StatusOK, "index.html", gin.H{
- "Title": c.MustGet("Tenant"),
+ c.HTML(200, "index.html", gin.H{
+ "Tenant": c.MustGet("Tenant"),
})
}
}
diff --git a/handler/index_test.go b/handler/index_test.go
new file mode 100644
index 000000000..0f14b477b
--- /dev/null
+++ b/handler/index_test.go
@@ -0,0 +1,24 @@
+package handler_test
+
+import (
+ "testing"
+
+ "github.com/WeCanHearYou/wchy/context"
+ "github.com/WeCanHearYou/wchy/handler"
+ "github.com/WeCanHearYou/wchy/mock"
+ "github.com/WeCanHearYou/wchy/model"
+ . "github.com/onsi/gomega"
+)
+
+func TestIndexHandler(t *testing.T) {
+ RegisterTestingT(t)
+
+ ctx := &context.WchyContext{}
+
+ server := mock.NewServer()
+ server.Set("Tenant", &model.Tenant{Name: "Any Tenant"})
+ server.Register(handler.Index(ctx))
+ status, _ := server.Request()
+
+ Expect(status).To(Equal(200))
+}
diff --git a/main.go b/main.go
index 1a13fe5aa..8b988b1a0 100644
--- a/main.go
+++ b/main.go
@@ -6,21 +6,21 @@ import (
_ "github.com/lib/pq"
+ "fmt"
+
"github.com/WeCanHearYou/wchy/context"
"github.com/WeCanHearYou/wchy/env"
"github.com/WeCanHearYou/wchy/router"
"github.com/WeCanHearYou/wchy/service"
- logging "github.com/op/go-logging"
)
-var log = logging.MustGetLogger("main")
var ctx *context.WchyContext
var db *sql.DB
var buildtime string
func init() {
- log.Info("Application is starting...")
- log.Infof("GO_ENV: %s", env.Current())
+ fmt.Printf("Application is starting...\n")
+ fmt.Printf("GO_ENV: %s\n", env.Current())
db, _ = sql.Open("postgres", os.Getenv("DATABASE_URL"))
diff --git a/mock/server.go b/mock/server.go
index f1d8af37b..824e91476 100644
--- a/mock/server.go
+++ b/mock/server.go
@@ -4,8 +4,10 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
+ "os"
+ "path/filepath"
- "fmt"
+ "strings"
"github.com/gin-gonic/gin"
"github.com/jmoiron/jsonq"
@@ -19,6 +21,8 @@ type Server struct {
// NewServer creates a new test server
func NewServer() *Server {
engine := gin.New()
+ engine.LoadHTMLGlob(filepath.Join(os.Getenv("GOPATH"), "src/github.com/WeCanHearYou/wchy/views/*"))
+
return &Server{
engine: engine,
}
@@ -36,6 +40,13 @@ func (s *Server) Param(key, value string) {
})
}
+// Set can be used to set context values
+func (s *Server) Set(key string, value interface{}) {
+ s.Use(func(c *gin.Context) {
+ c.Set(key, value)
+ })
+}
+
// Register given handler
func (s *Server) Register(handler gin.HandlerFunc) {
s.engine.Handle("GET", "/", handler)
@@ -48,9 +59,8 @@ func (s *Server) Request() (int, *jsonq.JsonQuery) {
s.engine.ServeHTTP(response, request)
status := response.Result().StatusCode
- fmt.Println(status)
- if status == 200 && response.Body.Len() > 0 {
+ if status == 200 && hasJSON(response) {
var data interface{}
decoder := json.NewDecoder(response.Body)
decoder.Decode(&data)
@@ -60,3 +70,13 @@ func (s *Server) Request() (int, *jsonq.JsonQuery) {
return status, nil
}
+
+func hasJSON(r *httptest.ResponseRecorder) bool {
+ isJSONContentType := strings.Contains(r.Result().Header.Get("Content-Type"), gin.MIMEJSON)
+
+ if r.Body.Len() > 0 && isJSONContentType {
+ return true
+ }
+
+ return false
+}
diff --git a/router/multitenant.go b/router/multitenant.go
index ba2a2d97a..47c8118f4 100644
--- a/router/multitenant.go
+++ b/router/multitenant.go
@@ -13,7 +13,7 @@ func MultiTenant(ctx *context.WchyContext) gin.HandlerFunc {
hostname := stripPort(c.Request.Host)
tenant, err := ctx.Tenant.GetByDomain(hostname)
if err == nil {
- c.Set("Tenant", tenant.Name)
+ c.Set("Tenant", tenant)
c.Next()
} else {
c.AbortWithStatus(404)
diff --git a/router/multitenant_test.go b/router/multitenant_test.go
new file mode 100644
index 000000000..0b8fd3824
--- /dev/null
+++ b/router/multitenant_test.go
@@ -0,0 +1,93 @@
+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 TestMultiTenant(t *testing.T) {
+ RegisterTestingT(t)
+
+ 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))
+ }
+ }
+
+}
+
+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))
+}
diff --git a/router/router.go b/router/router.go
index ecbe80a24..beb5efebb 100644
--- a/router/router.go
+++ b/router/router.go
@@ -16,19 +16,13 @@ func GetMainEngine(ctx *context.WchyContext) *gin.Engine {
router := gin.New()
if env.IsDevelopment() {
- router.Use(gin.Logger())
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
}
router.Use(MultiTenant(ctx))
-
- if env.IsTest() {
- router.LoadHTMLGlob(filepath.Join(os.Getenv("GOPATH"), "src/github.com/WeCanHearYou/wchy/views/*"))
- } else {
- router.LoadHTMLGlob("views/*")
- }
+ router.LoadHTMLGlob(filepath.Join(os.Getenv("GOPATH"), "src/github.com/WeCanHearYou/wchy/views/*"))
router.GET("/", handler.Index(ctx))
diff --git a/router/router_test.go b/router/router_test.go
index 83b8080a5..584f623b9 100644
--- a/router/router_test.go
+++ b/router/router_test.go
@@ -3,98 +3,13 @@ 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 := router.GetMainEngine(nil)
Expect(r).ToNot(BeNil())
}
-
-func TestMultiTenant(t *testing.T) {
- RegisterTestingT(t)
-
- 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))
-}
diff --git a/views/index.html b/views/index.html
index 472706e93..c6106ccc6 100644
--- a/views/index.html
+++ b/views/index.html
@@ -1,5 +1,5 @@
<html>
<h1>
- Hello World: {{ .Title }}
+ Hello World: {{ .Tenant.Name }}
</h1>
</html>
\ No newline at end of file