Skip to content

Commit

Permalink
added test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
s0s01qp committed Jul 21, 2021
1 parent ead78e1 commit 84ca455
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 12 deletions.
1 change: 0 additions & 1 deletion commons/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const (
// Endpoints
const (
EnvEndpoint = "/env"
HealthEndpoint = "/health"
HTTPTraceEndpoint = "/httpTrace"
InfoEndpoint = "/info"
MetricsEndpoint = "/metrics"
Expand Down
12 changes: 9 additions & 3 deletions controllers/gin/commons_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@ package gin_test

import (
"github.com/gin-gonic/gin"
ginControllers "github.com/sinhashubham95/go-actuator/controllers/gin"
"github.com/sinhashubham95/go-actuator/core"
"github.com/sinhashubham95/go-actuator/models"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)

func setupRouterAndGetResponse(t *testing.T, endpoint string, handler gin.HandlerFunc) *httptest.ResponseRecorder {
func setupRouterAndGetResponse(t *testing.T, endpoint int, path string) *httptest.ResponseRecorder {
router := gin.Default()
router.GET(endpoint, handler)
router.Use(core.GINTracer())
ginControllers.ConfigureHandlers(&models.Config{
Endpoints: []int{endpoint},
}, router)

request, err := http.NewRequest(http.MethodGet, endpoint, nil)
request, err := http.NewRequest(http.MethodGet, path, nil)
assert.NoError(t, err)

w := httptest.NewRecorder()
Expand Down
10 changes: 8 additions & 2 deletions controllers/gin/env_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package gin_test

import (
"encoding/json"
"github.com/sinhashubham95/go-actuator/models"
"github.com/stretchr/testify/assert"
"net/http"
"testing"

"github.com/sinhashubham95/go-actuator/commons"
ginControllers "github.com/sinhashubham95/go-actuator/controllers/gin"
)

func TestHandleEnv(t *testing.T) {
w := setupRouterAndGetResponse(t, commons.EnvEndpoint, ginControllers.HandleEnv)
w := setupRouterAndGetResponse(t, models.Env, commons.EnvEndpoint)
assert.Equal(t, http.StatusOK, w.Code)

var env map[string]interface{}
err := json.NewDecoder(w.Body).Decode(&env)
assert.NoError(t, err)
assert.NotEmpty(t, env)
}
21 changes: 21 additions & 0 deletions controllers/gin/httptrace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package gin_test

import (
"encoding/json"
"github.com/sinhashubham95/go-actuator/models"
"github.com/stretchr/testify/assert"
"net/http"
"testing"

"github.com/sinhashubham95/go-actuator/commons"
)

func TestHandleHTTPTrace(t *testing.T) {
w := setupRouterAndGetResponse(t, models.HTTPTrace, commons.HTTPTraceEndpoint)
assert.Equal(t, http.StatusOK, w.Code)

var traces []*models.HTTPTraceResult
err := json.NewDecoder(w.Body).Decode(&traces)
assert.NoError(t, err)
assert.NotEmpty(t, traces)
}
20 changes: 20 additions & 0 deletions controllers/gin/info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package gin_test

import (
"encoding/json"
"github.com/sinhashubham95/go-actuator/commons"
"github.com/sinhashubham95/go-actuator/models"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)

func TestHandleInfo(t *testing.T) {
w := setupRouterAndGetResponse(t, models.Info, commons.InfoEndpoint)
assert.Equal(t, http.StatusOK, w.Code)

var info map[string]interface{}
err := json.NewDecoder(w.Body).Decode(&info)
assert.NoError(t, err)
assert.NotEmpty(t, info)
}
20 changes: 20 additions & 0 deletions controllers/gin/metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package gin_test

import (
"encoding/json"
"github.com/sinhashubham95/go-actuator/commons"
"github.com/sinhashubham95/go-actuator/models"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)

func TestHandleMetrics(t *testing.T) {
w := setupRouterAndGetResponse(t, models.Metrics, commons.MetricsEndpoint)
assert.Equal(t, http.StatusOK, w.Code)

var metrics *models.MemStats
err := json.NewDecoder(w.Body).Decode(&metrics)
assert.NoError(t, err)
assert.NotNil(t, metrics)
}
14 changes: 14 additions & 0 deletions controllers/gin/ping_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package gin_test

import (
"github.com/sinhashubham95/go-actuator/commons"
"github.com/sinhashubham95/go-actuator/models"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)

func TestHandlePing(t *testing.T) {
w := setupRouterAndGetResponse(t, models.Ping, commons.PingEndpoint)
assert.Equal(t, http.StatusOK, w.Code)
}
11 changes: 5 additions & 6 deletions controllers/gin/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

func ConfigureHandlers(config *models.Config, router *gin.Engine) {
actuator := router.Group(config.Prefix)
gin.Logger()
for _, e := range config.Endpoints {
// now one by one add the handler of each endpoint
switch e {
Expand All @@ -18,15 +17,15 @@ func ConfigureHandlers(config *models.Config, router *gin.Engine) {
case models.HTTPTrace:
actuator.GET(commons.HTTPTraceEndpoint, HandleHTTPTrace)
case models.Info:
actuator.GET(commons.HealthEndpoint, HandleInfo)
actuator.GET(commons.InfoEndpoint, HandleInfo)
case models.Metrics:
actuator.GET(commons.HealthEndpoint, HandleMetrics)
actuator.GET(commons.MetricsEndpoint, HandleMetrics)
case models.Ping:
actuator.GET(commons.HealthEndpoint, HandlePing)
actuator.GET(commons.PingEndpoint, HandlePing)
case models.Shutdown:
actuator.GET(commons.HealthEndpoint, HandleShutdown)
actuator.GET(commons.ShutdownEndpoint, HandleShutdown)
case models.ThreadDump:
actuator.GET(commons.HealthEndpoint, HandleThreadDump)
actuator.GET(commons.ThreadDumpEndpoint, HandleThreadDump)
}
}
}
14 changes: 14 additions & 0 deletions controllers/gin/shutdown_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package gin_test

import (
"github.com/sinhashubham95/go-actuator/commons"
"github.com/sinhashubham95/go-actuator/models"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)

func TestHandleShutdown(t *testing.T) {
w := setupRouterAndGetResponse(t, models.Shutdown, commons.ShutdownEndpoint)
assert.Equal(t, http.StatusInternalServerError, w.Code)
}
15 changes: 15 additions & 0 deletions controllers/gin/threaddump_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package gin_test

import (
"github.com/sinhashubham95/go-actuator/commons"
"github.com/sinhashubham95/go-actuator/models"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)

func TestHandleThreadDump(t *testing.T) {
w := setupRouterAndGetResponse(t, models.ThreadDump, commons.ThreadDumpEndpoint)
assert.Equal(t, http.StatusOK, w.Code)
assert.NotEmpty(t, w.Body.String())
}

0 comments on commit 84ca455

Please sign in to comment.