From d655e99bdbf52d29f57419e3fdf250ffd38e9084 Mon Sep 17 00:00:00 2001 From: Bogdan Ungureanu Date: Tue, 12 Oct 2021 22:01:45 +0300 Subject: [PATCH 1/4] add content-type response headers --- swagger.go | 13 ++++++++++++- swagger_test.go | 13 +++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/swagger.go b/swagger.go index 7fb1000..e3a96ed 100644 --- a/swagger.go +++ b/swagger.go @@ -3,6 +3,7 @@ package echoSwagger import ( "html/template" "net/http" + "path/filepath" "regexp" "sync" @@ -84,6 +85,17 @@ func EchoWrapHandler(configFns ...func(c *Config)) echo.HandlerFunc { handler.Prefix = matches[1] }) + switch filepath.Ext(path) { + case ".html": + c.Response().Header().Set("Content-Type", "text/html; charset=utf-8") + case ".css": + c.Response().Header().Set("Content-Type", "text/css; charset=utf-8") + case ".js": + c.Response().Header().Set("Content-Type", "application/javascript") + case ".json": + c.Response().Header().Set("Content-Type", "application/json; charset=utf-8") + } + defer c.Response().Flush() switch path { @@ -97,7 +109,6 @@ func EchoWrapHandler(configFns ...func(c *Config)) echo.HandlerFunc { return nil } - c.Response().Writer.Header().Set("Content-Type", "application/json; charset=utf-8") _, _ = c.Response().Writer.Write([]byte(doc)) default: handler.ServeHTTP(c.Response().Writer, c.Request()) diff --git a/swagger_test.go b/swagger_test.go index 5eda06c..2150421 100644 --- a/swagger_test.go +++ b/swagger_test.go @@ -239,6 +239,7 @@ func TestWrapHandler(t *testing.T) { w1 := performRequest("GET", "/index.html", router) assert.Equal(t, 200, w1.Code) + assert.Equal(t, w1.Header()["Content-Type"][0], "text/html; charset=utf-8") w2 := performRequest("GET", "/doc.json", router) assert.Equal(t, 500, w2.Code) @@ -250,8 +251,16 @@ func TestWrapHandler(t *testing.T) { w3 := performRequest("GET", "/favicon-16x16.png", router) assert.Equal(t, 200, w3.Code) - w4 := performRequest("GET", "/notfound", router) - assert.Equal(t, 404, w4.Code) + w4 := performRequest("GET", "/swagger-ui.css", router) + assert.Equal(t, 200, w4.Code) + assert.Equal(t, w4.Header()["Content-Type"][0], "text/css; charset=utf-8") + + w5 := performRequest("GET", "/swagger-ui-bundle.js", router) + assert.Equal(t, 200, w5.Code) + assert.Equal(t, w5.Header()["Content-Type"][0], "application/javascript") + + w6 := performRequest("GET", "/notfound", router) + assert.Equal(t, 404, w6.Code) } From 117f60cbc571ef749491c320904e8435ce687ea2 Mon Sep 17 00:00:00 2001 From: Bogdan Ungureanu Date: Tue, 12 Oct 2021 22:05:22 +0300 Subject: [PATCH 2/4] lint --- swagger_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/swagger_test.go b/swagger_test.go index 2150421..4b2b65c 100644 --- a/swagger_test.go +++ b/swagger_test.go @@ -232,7 +232,6 @@ func (s *mockedSwag) ReadDoc() string { } func TestWrapHandler(t *testing.T) { - router := echo.New() router.GET("/*", EchoWrapHandler(DocExpansion("none"), DomID("#swagger-ui"))) @@ -261,7 +260,6 @@ func TestWrapHandler(t *testing.T) { w6 := performRequest("GET", "/notfound", router) assert.Equal(t, 404, w6.Code) - } func performRequest(method, target string, e *echo.Echo) *httptest.ResponseRecorder { From e58288d8d896a3ffbd330bc86d29b232f48ec7fc Mon Sep 17 00:00:00 2001 From: Bogdan Ungureanu Date: Tue, 12 Oct 2021 22:01:45 +0300 Subject: [PATCH 3/4] add content-type response headers --- swagger.go | 13 ++++++++++++- swagger_test.go | 13 +++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/swagger.go b/swagger.go index cf778c1..d0a5db0 100644 --- a/swagger.go +++ b/swagger.go @@ -3,6 +3,7 @@ package echoSwagger import ( "html/template" "net/http" + "path/filepath" "regexp" "sync" @@ -82,6 +83,17 @@ func EchoWrapHandler(configFns ...func(c *Config)) echo.HandlerFunc { h.Prefix = matches[1] }) + switch filepath.Ext(path) { + case ".html": + c.Response().Header().Set("Content-Type", "text/html; charset=utf-8") + case ".css": + c.Response().Header().Set("Content-Type", "text/css; charset=utf-8") + case ".js": + c.Response().Header().Set("Content-Type", "application/javascript") + case ".json": + c.Response().Header().Set("Content-Type", "application/json; charset=utf-8") + } + defer c.Response().Flush() switch path { case "": @@ -96,7 +108,6 @@ func EchoWrapHandler(configFns ...func(c *Config)) echo.HandlerFunc { return nil } - c.Response().Writer.Header().Set("Content-Type", "application/json; charset=utf-8") _, _ = c.Response().Writer.Write([]byte(doc)) default: h.ServeHTTP(c.Response().Writer, c.Request()) diff --git a/swagger_test.go b/swagger_test.go index 3d4da53..c6626be 100644 --- a/swagger_test.go +++ b/swagger_test.go @@ -240,6 +240,7 @@ func TestWrapHandler(t *testing.T) { w1 := performRequest("GET", "/index.html", router) assert.Equal(t, 200, w1.Code) + assert.Equal(t, w1.Header()["Content-Type"][0], "text/html; charset=utf-8") w2 := performRequest("GET", "/doc.json", router) assert.Equal(t, 500, w2.Code) @@ -251,8 +252,16 @@ func TestWrapHandler(t *testing.T) { w3 := performRequest("GET", "/favicon-16x16.png", router) assert.Equal(t, 200, w3.Code) - w4 := performRequest("GET", "/notfound", router) - assert.Equal(t, 404, w4.Code) + w4 := performRequest("GET", "/swagger-ui.css", router) + assert.Equal(t, 200, w4.Code) + assert.Equal(t, w4.Header()["Content-Type"][0], "text/css; charset=utf-8") + + w5 := performRequest("GET", "/swagger-ui-bundle.js", router) + assert.Equal(t, 200, w5.Code) + assert.Equal(t, w5.Header()["Content-Type"][0], "application/javascript") + + w6 := performRequest("GET", "/notfound", router) + assert.Equal(t, 404, w6.Code) w5 := performRequest("GET", "/", router) assert.Equal(t, 301, w5.Code) From 7e3818618d4711d82f3c5a5e369655c2319143d3 Mon Sep 17 00:00:00 2001 From: Bogdan Ungureanu Date: Tue, 12 Oct 2021 22:05:22 +0300 Subject: [PATCH 4/4] lint --- swagger_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/swagger_test.go b/swagger_test.go index c6626be..d3f1ed4 100644 --- a/swagger_test.go +++ b/swagger_test.go @@ -235,7 +235,6 @@ func TestWrapHandler(t *testing.T) { router := echo.New() - //RegisterEchoHandler(router.GET, "/", nil) router.GET("/*", EchoWrapHandler(DocExpansion("none"), DomID("#swagger-ui"))) w1 := performRequest("GET", "/index.html", router) @@ -263,8 +262,8 @@ func TestWrapHandler(t *testing.T) { w6 := performRequest("GET", "/notfound", router) assert.Equal(t, 404, w6.Code) - w5 := performRequest("GET", "/", router) - assert.Equal(t, 301, w5.Code) + w7 := performRequest("GET", "/", router) + assert.Equal(t, 301, w7.Code) } func performRequest(method, target string, e *echo.Echo) *httptest.ResponseRecorder {