2016-02-19 06:17:31 +03:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2018-11-06 02:07:32 +03:00
|
|
|
"errors"
|
2016-02-26 19:48:55 +03:00
|
|
|
"net/http"
|
2018-11-06 02:07:32 +03:00
|
|
|
"net/http/httptest"
|
2016-02-26 19:48:55 +03:00
|
|
|
"net/url"
|
2016-02-19 06:17:31 +03:00
|
|
|
"testing"
|
2016-02-26 19:48:55 +03:00
|
|
|
|
2018-11-06 02:07:32 +03:00
|
|
|
"github.com/gin-gonic/gin"
|
2016-02-26 19:48:55 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-02-19 06:17:31 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_desanitize64(t *testing.T) {
|
|
|
|
examples := map[string]string{
|
|
|
|
"test": "test",
|
|
|
|
"test+test+": "test-test-",
|
|
|
|
"test/test/": "test_test_",
|
|
|
|
"test=test==": "test.test..",
|
|
|
|
}
|
|
|
|
|
|
|
|
for expected, example := range examples {
|
|
|
|
assert.Equal(t, expected, desanitize64(example))
|
|
|
|
}
|
|
|
|
}
|
2016-02-19 07:18:07 +03:00
|
|
|
|
|
|
|
func Test_cleanQuery(t *testing.T) {
|
|
|
|
assert.Equal(t, "a\nb\nc", cleanQuery("a\nb\nc"))
|
|
|
|
assert.Equal(t, "", cleanQuery("--something"))
|
|
|
|
assert.Equal(t, "test", cleanQuery("--test\ntest\n -- test\n"))
|
|
|
|
}
|
2016-02-26 19:48:55 +03:00
|
|
|
|
|
|
|
func Test_getSessionId(t *testing.T) {
|
|
|
|
req := &http.Request{Header: http.Header{}}
|
|
|
|
req.Header.Add("x-session-id", "token")
|
|
|
|
assert.Equal(t, "token", getSessionId(req))
|
|
|
|
|
|
|
|
req = &http.Request{}
|
|
|
|
req.URL, _ = url.Parse("http://foobar/?_session_id=token")
|
|
|
|
assert.Equal(t, "token", getSessionId(req))
|
|
|
|
}
|
2018-11-06 02:07:32 +03:00
|
|
|
|
|
|
|
func Test_serveResult(t *testing.T) {
|
|
|
|
server := gin.Default()
|
|
|
|
server.GET("/good", func(c *gin.Context) {
|
2018-12-01 06:40:28 +03:00
|
|
|
serveResult(c, gin.H{"foo": "bar"}, nil)
|
2018-11-06 02:07:32 +03:00
|
|
|
})
|
|
|
|
server.GET("/bad", func(c *gin.Context) {
|
2018-12-01 06:40:28 +03:00
|
|
|
serveResult(c, nil, errors.New("message"))
|
2018-11-06 02:07:32 +03:00
|
|
|
})
|
|
|
|
server.GET("/nodata", func(c *gin.Context) {
|
2018-12-01 06:40:28 +03:00
|
|
|
serveResult(c, nil, nil)
|
2018-11-06 02:07:32 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
2018-12-01 06:40:28 +03:00
|
|
|
assert.Equal(t, `{"error":"message","status":400}`, w.Body.String())
|
2018-11-06 02:07:32 +03:00
|
|
|
|
|
|
|
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())
|
|
|
|
}
|