-
Notifications
You must be signed in to change notification settings - Fork 28
/
redoc_test.go
62 lines (50 loc) · 1.4 KB
/
redoc_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
package redoc_test
import (
"embed"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/mvrilo/go-redoc"
"github.com/stretchr/testify/assert"
)
//go:embed testdata/spec.json
var spec embed.FS
func TestRedoc(t *testing.T) {
r := redoc.Redoc{
SpecFile: "testdata/spec.json",
SpecFS: &spec,
SpecPath: "/openapi.json", // "/openapi.yaml"
Title: "Test API",
}
t.Run("Body", func(t *testing.T) {
body, err := r.Body()
assert.NoError(t, err)
assert.Contains(t, string(body), r.Title)
})
t.Run("Handler", func(t *testing.T) {
handler := r.Handler()
t.Run("Spec", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/openapi.json", nil)
w := httptest.NewRecorder()
handler(w, req)
resp := w.Result()
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
body, err := ioutil.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Contains(t, string(body), `"swagger":"2.0"`)
})
t.Run("Docs", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder()
handler(w, req)
resp := w.Result()
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "text/html", resp.Header.Get("Content-Type"))
body, err := ioutil.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Contains(t, string(body), r.Title)
})
})
}