-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocs_test.go
More file actions
91 lines (65 loc) · 2.21 KB
/
docs_test.go
File metadata and controls
91 lines (65 loc) · 2.21 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
package api_test
import (
"context"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/bjaus/api"
)
func TestServeDocs_default_title(t *testing.T) {
t.Parallel()
r := api.New(api.WithTitle("My API"))
r.ServeDocs("/docs")
srv := httptest.NewServer(r)
t.Cleanup(srv.Close)
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, srv.URL+"/docs", nil)
require.NoError(t, err)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer func() { require.NoError(t, resp.Body.Close()) }()
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "text/html; charset=utf-8", resp.Header.Get("Content-Type"))
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
bodyStr := string(body)
assert.Contains(t, bodyStr, "My API")
assert.Contains(t, bodyStr, "elements-api")
assert.Contains(t, bodyStr, "stoplight")
assert.Contains(t, bodyStr, "/openapi.json")
}
func TestServeDocs_custom_title(t *testing.T) {
t.Parallel()
r := api.New(api.WithTitle("Default Title"))
r.ServeDocs("/docs", api.WithDocsTitle("Custom"))
srv := httptest.NewServer(r)
t.Cleanup(srv.Close)
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, srv.URL+"/docs", nil)
require.NoError(t, err)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer func() { require.NoError(t, resp.Body.Close()) }()
assert.Equal(t, http.StatusOK, resp.StatusCode)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
bodyStr := string(body)
assert.Contains(t, bodyStr, "Custom")
assert.Contains(t, bodyStr, "/openapi.json")
}
func TestServeDocs_spec_url_rendered(t *testing.T) {
t.Parallel()
r := api.New()
r.ServeDocs("/api-docs")
srv := httptest.NewServer(r)
t.Cleanup(srv.Close)
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, srv.URL+"/api-docs", nil)
require.NoError(t, err)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer func() { require.NoError(t, resp.Body.Close()) }()
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
assert.Contains(t, string(body), `apiDescriptionUrl="/openapi.json"`)
}