Add test for serverResult func
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
|||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
"github.com/sosedoff/pgweb/pkg/data"
|
||||||
"github.com/sosedoff/pgweb/pkg/shared"
|
"github.com/sosedoff/pgweb/pkg/shared"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -139,3 +140,22 @@ func assetContentType(name string) string {
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func serveStaticAsset(path string, c *gin.Context) {
|
||||||
|
data, err := data.Asset("static" + path)
|
||||||
|
if err != nil {
|
||||||
|
c.String(400, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Data(200, assetContentType(path), data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func serveResult(result interface{}, err error, c *gin.Context) {
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(400, NewError(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(200, result)
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
"net/url"
|
"net/url"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -36,3 +39,34 @@ func Test_getSessionId(t *testing.T) {
|
|||||||
req.URL, _ = url.Parse("http://foobar/?_session_id=token")
|
req.URL, _ = url.Parse("http://foobar/?_session_id=token")
|
||||||
assert.Equal(t, "token", getSessionId(req))
|
assert.Equal(t, "token", getSessionId(req))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_serveResult(t *testing.T) {
|
||||||
|
server := gin.Default()
|
||||||
|
server.GET("/good", func(c *gin.Context) {
|
||||||
|
serveResult(gin.H{"foo": "bar"}, nil, c)
|
||||||
|
})
|
||||||
|
server.GET("/bad", func(c *gin.Context) {
|
||||||
|
serveResult(nil, errors.New("message"), c)
|
||||||
|
})
|
||||||
|
server.GET("/nodata", func(c *gin.Context) {
|
||||||
|
serveResult(nil, nil, c)
|
||||||
|
})
|
||||||
|
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
req, _ := http.NewRequest("GET", "/good", nil)
|
||||||
|
server.ServeHTTP(w, req)
|
||||||
|
assert.Equal(t, 200, w.Code)
|
||||||
|
assert.Equal(t, `{"foo":"bar"}`, w.Body.String())
|
||||||
|
|
||||||
|
w = httptest.NewRecorder()
|
||||||
|
req, _ = http.NewRequest("GET", "/bad", nil)
|
||||||
|
server.ServeHTTP(w, req)
|
||||||
|
assert.Equal(t, 400, w.Code)
|
||||||
|
assert.Equal(t, `{"error":"message"}`, w.Body.String())
|
||||||
|
|
||||||
|
w = httptest.NewRecorder()
|
||||||
|
req, _ = http.NewRequest("GET", "/nodata", nil)
|
||||||
|
server.ServeHTTP(w, req)
|
||||||
|
assert.Equal(t, 200, w.Code)
|
||||||
|
assert.Equal(t, `null`, w.Body.String())
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
"github.com/sosedoff/pgweb/pkg/command"
|
"github.com/sosedoff/pgweb/pkg/command"
|
||||||
"github.com/sosedoff/pgweb/pkg/data"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Middleware function to check database connection status before running queries
|
// Middleware function to check database connection status before running queries
|
||||||
@@ -58,25 +57,6 @@ func requestInspectMiddleware() gin.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func serveStaticAsset(path string, c *gin.Context) {
|
|
||||||
data, err := data.Asset("static" + path)
|
|
||||||
if err != nil {
|
|
||||||
c.String(400, err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Data(200, assetContentType(path), data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func serveResult(result interface{}, err error, c *gin.Context) {
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(400, NewError(err))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
c.JSON(200, result)
|
|
||||||
}
|
|
||||||
|
|
||||||
func corsMiddleware() gin.HandlerFunc {
|
func corsMiddleware() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
c.Header("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
|
c.Header("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
|
||||||
|
|||||||
Reference in New Issue
Block a user