Merge pull request #340 from sundowndev/refactor/tests

Improve api unit tests
This commit is contained in:
Raphaël authored and GitHub committed 2020-05-11 19:38:46 +02:00
commit e7ab046576
4 files changed
+38 -17

No files matched your search

+1 -1
View File
@@ -46,5 +46,5 @@ jobs:
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
run: |
go test -race -coverprofile=coverage.txt -covermode=atomic -v ./...
go test -race -coverprofile=coverage.txt -covermode=atomic -v -bench=. ./...
bash <(curl -s https://codecov.io/bash)
+4 -5
View File
@@ -38,6 +38,10 @@ func registerClientRoute(router *gin.Engine) {
path := strings.ReplaceAll(name, clientDistPath, staticPath)
data := file.Data
if path == staticPath+"index.html" {
path = staticPath
}
router.GET(path, func(c *gin.Context) {
c.Header("Content-Type", detectContentType(path, data))
c.Writer.WriteHeader(http.StatusOK)
@@ -45,11 +49,6 @@ func registerClientRoute(router *gin.Engine) {
c.Abort()
})
}
router.GET("/", func(c *gin.Context) {
c.Redirect(302, staticPath+"index.html")
c.Abort()
})
}
// Serve launches the web client
+33 -9
View File
@@ -9,9 +9,12 @@ import (
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
gock "gopkg.in/h2non/gock.v1"
"gopkg.in/sundowndev/phoneinfoga.v2/pkg/config"
"gopkg.in/sundowndev/phoneinfoga.v2/pkg/scanners"
)
var r *gin.Engine
func performRequest(r http.Handler, method, path string) (*httptest.ResponseRecorder, error) {
req, err := http.NewRequest(method, path, nil)
w := httptest.NewRecorder()
@@ -19,10 +22,23 @@ func performRequest(r http.Handler, method, path string) (*httptest.ResponseReco
return w, err
}
func BenchmarkAPI(b *testing.B) {
assert := assert.New(b)
r = gin.Default()
r = Serve(r, true)
b.Run("localScan - /api/numbers/:number/scan/local", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := performRequest(r, "GET", "/api/numbers/3312345253/scan/local")
assert.Equal(nil, err)
}
})
}
func TestApi(t *testing.T) {
assert := assert.New(t)
r := gin.Default()
r = Serve(r, true)
r = gin.Default()
r = Serve(r, false)
t.Run("detectContentType", func(t *testing.T) {
contentType := detectContentType("/file.hash.css", []byte{})
@@ -189,15 +205,15 @@ func TestApi(t *testing.T) {
})
})
// t.Run("healthHandler - /api/", func(t *testing.T) {
// res, err := performRequest(r, "GET", "/api")
t.Run("healthHandler - /api/", func(t *testing.T) {
res, err := performRequest(r, "GET", "/api/")
// body, _ := ioutil.ReadAll(res.Body)
body, _ := ioutil.ReadAll(res.Body)
// assert.Equal(err, nil, "should be equal")
// assert.Equal(res.Result().StatusCode, 200, "should be equal")
// assert.Equal(string(body), "{\"success\":true,\"version\":\""+config.Version+"\"}", "should be equal")
// })
assert.Equal(nil, err, "should be equal")
assert.Equal(200, res.Result().StatusCode, "should be equal")
assert.Equal("{\"success\":true,\"version\":\""+config.Version+"\"}", string(body), "should be equal")
})
t.Run("404 error - /api/notfound", func(t *testing.T) {
res, err := performRequest(r, "GET", "/api/notfound")
@@ -208,5 +224,13 @@ func TestApi(t *testing.T) {
assert.Equal(res.Result().StatusCode, 404, "should be equal")
assert.Equal(string(body), "{\"success\":false,\"error\":\"Resource not found\"}", "should be equal")
})
t.Run("Client - /", func(t *testing.T) {
res, err := performRequest(r, "GET", "/")
assert.Equal(nil, err, "should be equal")
assert.Equal(200, res.Result().StatusCode, "should be equal")
assert.Equal(http.Header{"Content-Type":[]string{"text/html; charset=utf-8"}}, res.Header(), "should be equal")
})
})
}
-2
View File
@@ -29,8 +29,6 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.6.2 h1:88crIK23zO6TqlQBt+f9FrPJNKm9ZEr7qjp9vl/d5TM=
github.com/gin-gonic/gin v1.6.2/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=