From db0fb5fea6a58fc9e97178d5dcef21e1b26831cb Mon Sep 17 00:00:00 2001 From: Jan Galek Date: Wed, 18 Dec 2024 03:03:20 +0100 Subject: [PATCH] [Feature] enable by mode (#13) * [Test] cover release mode by test --- diagoMiddleware.go | 7 +------ tests/diagoMiddleware_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/diagoMiddleware.go b/diagoMiddleware.go index 072d0bb..605d4ce 100644 --- a/diagoMiddleware.go +++ b/diagoMiddleware.go @@ -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) diff --git a/tests/diagoMiddleware_test.go b/tests/diagoMiddleware_test.go index e6585e5..f0376b2 100644 --- a/tests/diagoMiddleware_test.go +++ b/tests/diagoMiddleware_test.go @@ -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" @@ -52,3 +53,30 @@ func TestDiagoMiddleware(t *testing.T) { assert.Contains(t, w.Body.String(), "
Content
") assert.Contains(t, w.Body.String(), "") } + +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!") +}