Skip to content

Commit

Permalink
[Feature] enable by mode (#13)
Browse files Browse the repository at this point in the history
* [Test] cover release mode by test
  • Loading branch information
JanGalek authored Dec 18, 2024
1 parent 3efdb18 commit db0fb5f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
7 changes: 1 addition & 6 deletions diagoMiddleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,7 @@ func DiagoMiddleware(r *router.Router, d *Diago) gin.HandlerFunc {
}

c.Writer = originalWriter
_, err := c.Writer.Write(responseBuffer.Bytes())
if err != nil {
err = c.Error(err)
c.Status(500)
writer.buffer.WriteString("Error generating Diago panel HTML")
}
c.Writer.Write(responseBuffer.Bytes())

status := c.Writer.Status()
log.Printf("Status: %d", status)
Expand Down
28 changes: 28 additions & 0 deletions tests/diagoMiddleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tests
import (
"github.com/gin-gonic/gin"
"github.com/gouef/diago"
"github.com/gouef/router"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -52,3 +53,30 @@ func TestDiagoMiddleware(t *testing.T) {
assert.Contains(t, w.Body.String(), "<div>Content</div>")
assert.Contains(t, w.Body.String(), "<script>console.log('JS');</script>")
}

func TestDiagoMiddlewareReleaseMode(t *testing.T) {
r := router.NewRouter()
r.EnableRelease()
n := r.GetNativeRouter()
gin.SetMode(gin.ReleaseMode)

d := diago.NewDiago()

d.AddExtension(&MyExtension{})

n.GET("/test", diago.DiagoMiddleware(r, d), func(c *gin.Context) {
c.Header("Content-Type", "text/html; charset=utf-8")
c.String(200, "Hello, world!")
})
w := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/test", nil)
if err != nil {
t.Fatal(err)
}

n.ServeHTTP(w, req)

assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))

assert.Equal(t, w.Body.String(), "Hello, world!")
}

0 comments on commit db0fb5f

Please sign in to comment.