mirror of
https://github.com/sosedoff/pgweb.git
synced 2024-12-15 03:36:33 +03:00
Add test for serverResult func
This commit is contained in:
parent
9ca959c31c
commit
abb143601b
@ -10,6 +10,7 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/sosedoff/pgweb/pkg/data"
|
||||
"github.com/sosedoff/pgweb/pkg/shared"
|
||||
)
|
||||
|
||||
@ -139,3 +140,22 @@ func assetContentType(name string) string {
|
||||
|
||||
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
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@ -36,3 +39,34 @@ func Test_getSessionId(t *testing.T) {
|
||||
req.URL, _ = url.Parse("http://foobar/?_session_id=token")
|
||||
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/sosedoff/pgweb/pkg/command"
|
||||
"github.com/sosedoff/pgweb/pkg/data"
|
||||
)
|
||||
|
||||
// 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 {
|
||||
return func(c *gin.Context) {
|
||||
c.Header("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
|
||||
|
Loading…
Reference in New Issue
Block a user