Skip to content

Commit 2c359ae

Browse files
authored
add content-type response headers (#57)
1 parent 0c8bf3f commit 2c359ae

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

swagger.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package echoSwagger
33
import (
44
"html/template"
55
"net/http"
6+
"path/filepath"
67
"regexp"
78
"sync"
89

@@ -82,6 +83,18 @@ func EchoWrapHandler(configFns ...func(c *Config)) echo.HandlerFunc {
8283
h.Prefix = matches[1]
8384
})
8485

86+
87+
switch filepath.Ext(path) {
88+
case ".html":
89+
c.Response().Header().Set("Content-Type", "text/html; charset=utf-8")
90+
case ".css":
91+
c.Response().Header().Set("Content-Type", "text/css; charset=utf-8")
92+
case ".js":
93+
c.Response().Header().Set("Content-Type", "application/javascript")
94+
case ".json":
95+
c.Response().Header().Set("Content-Type", "application/json; charset=utf-8")
96+
}
97+
8598
defer c.Response().Flush()
8699
switch path {
87100
case "":
@@ -96,7 +109,6 @@ func EchoWrapHandler(configFns ...func(c *Config)) echo.HandlerFunc {
96109
return nil
97110
}
98111

99-
c.Response().Writer.Header().Set("Content-Type", "application/json; charset=utf-8")
100112
_, _ = c.Response().Writer.Write([]byte(doc))
101113
default:
102114
h.ServeHTTP(c.Response().Writer, c.Request())

swagger_test.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,13 @@ func (s *mockedSwag) ReadDoc() string {
232232
}
233233

234234
func TestWrapHandler(t *testing.T) {
235-
236235
router := echo.New()
237236

238-
//RegisterEchoHandler(router.GET, "/", nil)
239237
router.GET("/*", EchoWrapHandler(DocExpansion("none"), DomID("#swagger-ui")))
240238

241239
w1 := performRequest("GET", "/index.html", router)
242240
assert.Equal(t, 200, w1.Code)
241+
assert.Equal(t, w1.Header()["Content-Type"][0], "text/html; charset=utf-8")
243242

244243
w2 := performRequest("GET", "/doc.json", router)
245244
assert.Equal(t, 500, w2.Code)
@@ -251,11 +250,19 @@ func TestWrapHandler(t *testing.T) {
251250
w3 := performRequest("GET", "/favicon-16x16.png", router)
252251
assert.Equal(t, 200, w3.Code)
253252

254-
w4 := performRequest("GET", "/notfound", router)
255-
assert.Equal(t, 404, w4.Code)
253+
w4 := performRequest("GET", "/swagger-ui.css", router)
254+
assert.Equal(t, 200, w4.Code)
255+
assert.Equal(t, w4.Header()["Content-Type"][0], "text/css; charset=utf-8")
256+
257+
w5 := performRequest("GET", "/swagger-ui-bundle.js", router)
258+
assert.Equal(t, 200, w5.Code)
259+
assert.Equal(t, w5.Header()["Content-Type"][0], "application/javascript")
260+
261+
w6 := performRequest("GET", "/notfound", router)
262+
assert.Equal(t, 404, w6.Code)
256263

257-
w5 := performRequest("GET", "/", router)
258-
assert.Equal(t, 301, w5.Code)
264+
w7 := performRequest("GET", "/", router)
265+
assert.Equal(t, 301, w7.Code)
259266
}
260267

261268
func performRequest(method, target string, e *echo.Echo) *httptest.ResponseRecorder {

0 commit comments

Comments
 (0)