-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
openapi_handler_test.go
78 lines (59 loc) · 1.94 KB
/
openapi_handler_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package fuego
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
)
// dummyMiddleware sets the X-Test header on the request and the X-Test-Response header on the response.
func dummyMiddleware(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Header.Set("X-Test", "test")
w.Header().Set("X-Test-Response", "response")
handler.ServeHTTP(w, r)
})
}
func TestUIHandler(t *testing.T) {
t.Run("works with DefaultOpenAPIHandler", func(t *testing.T) {
s := NewServer()
s.OutputOpenAPISpec()
require.NotNil(t, s.OpenAPIConfig.UIHandler)
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/swagger/index.html", nil)
s.Mux.ServeHTTP(w, r)
require.Equal(t, 200, w.Code)
require.Contains(t, w.Body.String(), "OpenAPI specification")
require.Equal(t, "", w.Header().Get("X-Test-Response"))
})
t.Run("wrap DefaultOpenAPIHandler behind a middleware", func(t *testing.T) {
s := NewServer(
WithOpenAPIConfig(OpenAPIConfig{
UIHandler: func(specURL string) http.Handler {
return dummyMiddleware(DefaultOpenAPIHandler(specURL))
},
}),
)
s.OutputOpenAPISpec()
require.NotNil(t, s.OpenAPIConfig.UIHandler)
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/swagger/index.html", nil)
s.Mux.ServeHTTP(w, r)
require.Equal(t, 200, w.Code)
require.Contains(t, w.Body.String(), "OpenAPI specification")
require.Equal(t, "response", w.Header().Get("X-Test-Response"))
})
t.Run("disabling UI", func(t *testing.T) {
s := NewServer(
WithOpenAPIConfig(OpenAPIConfig{
DisableSwaggerUI: true,
}),
)
s.OutputOpenAPISpec()
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/swagger/index.html", nil)
s.Mux.ServeHTTP(w, r)
require.Equal(t, 404, w.Code)
require.Contains(t, w.Body.String(), "404 page not found")
require.Equal(t, "", w.Header().Get("X-Test-Response"))
})
}