forked from emicklei/go-restful-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_path_test.go
164 lines (143 loc) · 5.31 KB
/
build_path_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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package restfulspec
import (
"testing"
restful "github.com/emicklei/go-restful"
"github.com/go-openapi/spec"
)
func TestRouteToPath(t *testing.T) {
description := "get the <strong>a</strong> <em>b</em> test\nthis is the test description"
notes := "notes\nblah blah"
ws := new(restful.WebService)
ws.Path("/tests/{v}")
ws.Param(ws.PathParameter("v", "value of v").DefaultValue("default-v"))
ws.Consumes(restful.MIME_JSON)
ws.Produces(restful.MIME_XML)
ws.Route(ws.GET("/a/{b}").To(dummy).
Doc(description).
Notes(notes).
Param(ws.PathParameter("b", "value of b").DefaultValue("default-b")).
Param(ws.QueryParameter("q", "value of q").DefaultValue("default-q")).
Returns(200, "list of a b tests", []Sample{}).
Writes([]Sample{}))
ws.Route(ws.GET("/a/{b}/{c:[a-z]+}/{d:[1-9]+}/e").To(dummy).
Param(ws.PathParameter("b", "value of b").DefaultValue("default-b")).
Param(ws.PathParameter("c", "with regex").DefaultValue("abc")).
Param(ws.PathParameter("d", "with regex").DefaultValue("abcef")).
Param(ws.QueryParameter("q", "value of q").DefaultValue("default-q")).
Returns(200, "list of a b tests", []Sample{}).
Writes([]Sample{}))
p := buildPaths(ws, Config{})
t.Log(asJSON(p))
if p.Paths["/tests/{v}/a/{b}"].Get.Parameters[0].Type != "string" {
t.Error("Parameter type is not set.")
}
if _, exists := p.Paths["/tests/{v}/a/{b}/{c}/{d}/e"]; !exists {
t.Error("Expected path to exist after it was sanitized.")
}
if p.Paths["/tests/{v}/a/{b}"].Get.Description != notes {
t.Errorf("GET description incorrect")
}
if p.Paths["/tests/{v}/a/{b}"].Get.Summary != "get the a b test\nthis is the test description" {
t.Errorf("GET summary incorrect")
}
response := p.Paths["/tests/{v}/a/{b}"].Get.Responses.StatusCodeResponses[200]
if response.Schema.Type[0] != "array" {
t.Errorf("response type incorrect")
}
if response.Schema.Items.Schema.Ref.String() != "#/definitions/restfulspec.Sample" {
t.Errorf("response element type incorrect")
}
// Test for patterns
path := p.Paths["/tests/{v}/a/{b}/{c}/{d}/e"]
checkPattern(t, path, "c", "[a-z]+")
checkPattern(t, path, "d", "[1-9]+")
checkPattern(t, path, "v", "")
}
func getParameter(path spec.PathItem, name string) (*spec.Parameter, bool) {
for _, param := range path.Get.Parameters {
if param.Name == name {
return ¶m, true
}
}
return nil, false
}
func checkPattern(t *testing.T, path spec.PathItem, paramName string, pattern string) {
param, exists := getParameter(path, paramName)
if !exists {
t.Error("Expected Parameter %s to exist", paramName)
}
if param.Pattern != pattern {
t.Error("Expected pattern %s to equal %s", param.Pattern, pattern)
}
}
func TestMultipleMethodsRouteToPath(t *testing.T) {
ws := new(restful.WebService)
ws.Path("/tests/a")
ws.Consumes(restful.MIME_JSON)
ws.Produces(restful.MIME_XML)
ws.Route(ws.GET("/a/b").To(dummy).
Doc("get a b test").
Returns(200, "list of a b tests", []Sample{}).
Writes([]Sample{}))
ws.Route(ws.POST("/a/b").To(dummy).
Doc("post a b test").
Returns(200, "list of a b tests", []Sample{}).
Returns(500, "internal server error", []Sample{}).
Reads(Sample{}).
Writes([]Sample{}))
p := buildPaths(ws, Config{})
t.Log(asJSON(p))
if p.Paths["/tests/a/a/b"].Get.Summary != "get a b test" {
t.Errorf("GET summary incorrect")
}
if p.Paths["/tests/a/a/b"].Post.Summary != "post a b test" {
t.Errorf("POST summary incorrect")
}
if _, exists := p.Paths["/tests/a/a/b"].Post.Responses.StatusCodeResponses[500]; !exists {
t.Errorf("Response code 500 not added to spec.")
}
expectedRef := spec.MustCreateRef("#/definitions/restfulspec.Sample")
postBodyparam := p.Paths["/tests/a/a/b"].Post.Parameters[0]
postBodyRef := postBodyparam.Schema.Ref
if postBodyRef.String() != expectedRef.String() {
t.Errorf("Expected: %s, Got: %s", expectedRef.String(), postBodyRef.String())
}
if postBodyparam.Format != "" || postBodyparam.Type != "" || postBodyparam.Default != nil {
t.Errorf("Invalid parameter property is set on body property")
}
}
func TestReadArrayObjectInBody(t *testing.T) {
ws := new(restful.WebService)
ws.Path("/tests/a")
ws.Consumes(restful.MIME_JSON)
ws.Produces(restful.MIME_XML)
ws.Route(ws.POST("/a/b").To(dummy).
Doc("post a b test with array in body").
Returns(200, "list of a b tests", []Sample{}).
Returns(500, "internal server error", []Sample{}).
Reads([]Sample{}).
Writes([]Sample{}))
p := buildPaths(ws, Config{})
t.Log(asJSON(p))
postInfo := p.Paths["/tests/a/a/b"].Post
if postInfo.Summary != "post a b test with array in body" {
t.Errorf("POST description incorrect")
}
if _, exists := postInfo.Responses.StatusCodeResponses[500]; !exists {
t.Errorf("Response code 500 not added to spec.")
}
// indentify element model type in body array
expectedItemRef := spec.MustCreateRef("#/definitions/restfulspec.Sample")
postBody := postInfo.Parameters[0]
if postBody.Schema.Ref.String() != "" {
t.Errorf("you shouldn't have body Ref setting when using array in body!")
}
// check body array dy item ref
postBodyitems := postBody.Schema.Items.Schema.Ref
if postBodyitems.String() != expectedItemRef.String() {
t.Errorf("Expected: %s, Got: %s", expectedItemRef.String(), expectedItemRef.String())
}
if postBody.Format != "" || postBody.Type != "" || postBody.Default != nil {
t.Errorf("Invalid parameter property is set on body property")
}
}