-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse_test.go
107 lines (98 loc) · 2.49 KB
/
response_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
package openapi_test
import (
"testing"
"github.com/MarkRosemaker/openapi"
)
func TestResponse_JSON(t *testing.T) {
t.Parallel()
// Response of an array of a complex type:
testJSON(t, []byte(`{
"description": "A complex object array response",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/VeryComplexType"
}
}
}
}
}`), &openapi.Response{})
// Response with a string type:
testJSON(t, []byte(`{
"description": "A simple string response",
"content": {
"text/plain": {
"schema": {
"type": "string"
}
}
}
}`), &openapi.Response{})
// Plain text response with headers:
testJSON(t, []byte(`{
"description": "A simple string response",
"headers": {
"X-Rate-Limit-Limit": {
"description": "The number of allowed requests in the current period",
"schema": {
"type": "integer"
}
},
"X-Rate-Limit-Remaining": {
"description": "The number of remaining requests in the current period",
"schema": {
"type": "integer"
}
},
"X-Rate-Limit-Reset": {
"description": "The number of seconds left in the current period",
"schema": {
"type": "integer"
}
}
},
"content": {
"text/plain": {
"schema": {
"type": "string",
"example": "whoa!"
}
}
}
}`), &openapi.Response{})
// Response with no return value:
testJSON(t, []byte(`{
"description": "object created"
}`), &openapi.Response{})
}
func TestResponse_Validate_Error(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
p openapi.Response
err string
}{
{openapi.Response{}, "description is required"},
{openapi.Response{
Description: "some description",
Headers: openapi.Headers{"foo": {Value: &openapi.Header{}}},
}, `headers["foo"]: schema or content is required`},
{openapi.Response{
Description: "some description",
Content: openapi.Content{"application/json": {
Schema: &openapi.SchemaRef{Value: &openapi.Schema{}},
}},
}, `content["application/json"].schema.type is required`},
{openapi.Response{
Description: "some description",
Links: openapi.Links{"address": {Value: &openapi.Link{}}},
}, `links.address: operationRef or operationId must be set`},
} {
t.Run(tc.err, func(t *testing.T) {
if err := tc.p.Validate(); err == nil || err.Error() != tc.err {
t.Fatalf("expected: %s, got: %s", tc.err, err)
}
})
}
}