-
Notifications
You must be signed in to change notification settings - Fork 8
/
examples_test.go
118 lines (100 loc) · 3.21 KB
/
examples_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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package gorilla_test
import (
"context"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
swagger "github.com/davidebianchi/gswagger"
"github.com/davidebianchi/gswagger/support/gorilla"
"github.com/stretchr/testify/require"
"github.com/getkin/kin-openapi/openapi3"
"github.com/gorilla/mux"
)
func TestExample(t *testing.T) {
context := context.Background()
muxRouter := mux.NewRouter()
router, _ := swagger.NewRouter(gorilla.NewRouter(muxRouter), swagger.Options{
Context: context,
Openapi: &openapi3.T{
Info: &openapi3.Info{
Title: "my title",
Version: "1.0.0",
},
},
})
okHandler := func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}
type User struct {
Name string `json:"name" jsonschema:"title=The user name,required" jsonschema_extras:"example=Jane"`
PhoneNumber int `json:"phone" jsonschema:"title=mobile number of user"`
Groups []string `json:"groups,omitempty" jsonschema:"title=groups of the user,default=users"`
Address string `json:"address" jsonschema:"title=user address"`
}
type errorResponse struct {
Message string `json:"message"`
}
router.AddRoute(http.MethodPost, "/users", okHandler, swagger.Definitions{
RequestBody: &swagger.ContentValue{
Content: swagger.Content{
"application/json": {Value: User{}},
},
},
Responses: map[int]swagger.ContentValue{
201: {
Content: swagger.Content{
"text/html": {Value: ""},
},
},
401: {
Content: swagger.Content{
"application/json": {Value: &errorResponse{}},
},
Description: "invalid request",
},
},
})
router.AddRoute(http.MethodGet, "/users", okHandler, swagger.Definitions{
Responses: map[int]swagger.ContentValue{
200: {
Content: swagger.Content{
"application/json": {Value: &[]User{}},
},
},
},
})
carSchema := openapi3.NewObjectSchema().WithProperties(map[string]*openapi3.Schema{
"foo": openapi3.NewStringSchema(),
"bar": openapi3.NewIntegerSchema().WithMax(15).WithMin(5),
})
requestBody := openapi3.NewRequestBody().WithJSONSchema(carSchema)
operation := swagger.NewOperation()
operation.AddRequestBody(requestBody)
router.AddRawRoute(http.MethodPost, "/cars", okHandler, operation)
_, err := router.AddRoute(http.MethodGet, "/users/{userId}", okHandler, swagger.Definitions{
Querystring: swagger.ParameterValue{
"query": {
Schema: &swagger.Schema{Value: ""},
},
},
})
require.NoError(t, err)
_, err = router.AddRoute(http.MethodGet, "/cars/{carId}/drivers/{driverId}", okHandler, swagger.Definitions{})
require.NoError(t, err)
router.GenerateAndExposeOpenapi()
t.Run("correctly exposes documentation", func(t *testing.T) {
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, swagger.DefaultJSONDocumentationPath, nil)
muxRouter.ServeHTTP(w, req)
require.Equal(t, http.StatusOK, w.Result().StatusCode)
require.Equal(t, "application/json", w.Result().Header.Get("Content-Type"))
body, err := io.ReadAll(w.Result().Body)
require.NoError(t, err)
expected, err := os.ReadFile("./testdata/examples-users.json")
require.NoError(t, err)
require.JSONEq(t, string(expected), string(body), "actual json data: %s", body)
})
}