1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package middleware
16
17 import (
18 "context"
19 "net/http"
20 "net/http/httptest"
21 "testing"
22
23 "github.com/stretchr/testify/assert"
24 "github.com/stretchr/testify/require"
25
26 "github.com/go-openapi/runtime"
27 "github.com/go-openapi/runtime/internal/testing/petstore"
28 )
29
30 func TestServeSpecMiddleware(t *testing.T) {
31 spec, api := petstore.NewAPI(t)
32 ctx := NewContext(spec, api, nil)
33
34 t.Run("Spec handler", func(t *testing.T) {
35 handler := Spec("", ctx.spec.Raw(), nil)
36
37 t.Run("serves spec", func(t *testing.T) {
38 request, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "/swagger.json", nil)
39 require.NoError(t, err)
40 request.Header.Add(runtime.HeaderContentType, runtime.JSONMime)
41 recorder := httptest.NewRecorder()
42
43 handler.ServeHTTP(recorder, request)
44 assert.Equal(t, http.StatusOK, recorder.Code)
45
46 responseHeaders := recorder.Result().Header
47 responseContentType := responseHeaders.Get("Content-Type")
48 assert.Equal(t, applicationJSON, responseContentType)
49
50 responseBody := recorder.Body
51 require.NotNil(t, responseBody)
52 require.JSONEq(t, string(spec.Raw()), responseBody.String())
53 })
54
55 t.Run("returns 404 when no next handler", func(t *testing.T) {
56 request, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "/api/pets", nil)
57 require.NoError(t, err)
58 request.Header.Add(runtime.HeaderContentType, runtime.JSONMime)
59 recorder := httptest.NewRecorder()
60
61 handler.ServeHTTP(recorder, request)
62 assert.Equal(t, http.StatusNotFound, recorder.Code)
63 })
64
65 t.Run("forwards to next handler for other url", func(t *testing.T) {
66 handler = Spec("", ctx.spec.Raw(), http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
67 rw.WriteHeader(http.StatusOK)
68 }))
69 request, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "/api/pets", nil)
70 require.NoError(t, err)
71 request.Header.Add(runtime.HeaderContentType, runtime.JSONMime)
72 recorder := httptest.NewRecorder()
73
74 handler.ServeHTTP(recorder, request)
75 assert.Equal(t, http.StatusOK, recorder.Code)
76 })
77 })
78
79 t.Run("Spec handler with options", func(t *testing.T) {
80 handler := Spec("/swagger", ctx.spec.Raw(), nil,
81 WithSpecPath("spec"),
82 WithSpecDocument("myapi-swagger.json"),
83 )
84
85 t.Run("serves spec", func(t *testing.T) {
86 request, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "/swagger/spec/myapi-swagger.json", nil)
87 require.NoError(t, err)
88 request.Header.Add(runtime.HeaderContentType, runtime.JSONMime)
89 recorder := httptest.NewRecorder()
90
91 handler.ServeHTTP(recorder, request)
92 assert.Equal(t, http.StatusOK, recorder.Code)
93 })
94
95 t.Run("should not find spec there", func(t *testing.T) {
96 request, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "/swagger.json", nil)
97 require.NoError(t, err)
98 request.Header.Add(runtime.HeaderContentType, runtime.JSONMime)
99 recorder := httptest.NewRecorder()
100
101 handler.ServeHTTP(recorder, request)
102 assert.Equal(t, http.StatusNotFound, recorder.Code)
103 })
104 })
105 }
106
View as plain text