-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfo_test.go
135 lines (117 loc) · 3.36 KB
/
info_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
package openapi_test
import (
"net/url"
"testing"
"github.com/MarkRosemaker/openapi"
)
func TestInfo_JSON(t *testing.T) {
t.Parallel()
testJSON(t, []byte(`{
"title": "Sample Pet Store App",
"summary": "A pet store manager.",
"description": "This is a sample server for a pet store.",
"termsOfService": "https://example.com/terms/",
"contact": {
"name": "API Support",
"url": "https://www.example.com/support",
"email": "support@example.com"
},
"license": {
"name": "Apache 2.0",
"url": "https://www.apache.org/licenses/LICENSE-2.0.html"
},
"version": "1.0.1"
}`), &openapi.Info{})
testJSON(t, []byte(`{
"title": "Sample Pet Store App",
"summary": "A pet store manager.",
"description": "This is a sample server for a pet store.",
"termsOfService": "https://example.com/terms/",
"contact": {
"name": "API Support",
"url": "https://www.example.com/support",
"email": "support@example.com"
},
"license": {
"name": "Apache 2.0",
"url": "https://www.apache.org/licenses/LICENSE-2.0.html"
},
"version": "1.0.1",
"x-foo": true,
"x-bar": ["one", "two"]
}`), &openapi.Info{})
}
func TestInfo_Validate(t *testing.T) {
t.Parallel()
t.Run("empty", func(t *testing.T) {
i := openapi.Info{}
if err := i.Validate(); err == nil {
t.Fatal("expected error")
} else if want := "title is required"; err.Error() != want {
t.Fatalf("got: %v, want: %v", err, want)
}
})
t.Run("no version", func(t *testing.T) {
i := openapi.Info{Title: "Sample Pet Store App"}
if err := i.Validate(); err == nil {
t.Fatal("expected error")
} else if want := "version is required"; err.Error() != want {
t.Fatalf("got: %v, want: %v", err, want)
}
})
t.Run("valid", func(t *testing.T) {
i := openapi.Info{Title: "Sample Pet Store App", Version: "1.0.1"}
if err := i.Validate(); err != nil {
t.Fatal(err)
}
})
t.Run("fix TOS URL", func(t *testing.T) {
i := openapi.Info{
Title: "Sample Pet Store App",
TermsOfService: &url.URL{Host: "example.com"},
Version: "1.0.1",
}
if err := i.Validate(); err != nil {
t.Fatal(err)
}
if want := "https://example.com"; i.TermsOfService.String() != want {
t.Fatalf("url not fixed, want: %q, got: %q", want, i.TermsOfService)
}
})
t.Run("invalid contact", func(t *testing.T) {
i := openapi.Info{
Title: "Sample Pet Store App",
Contact: &openapi.Contact{Email: "foo"},
Version: "1.0.1",
}
if err := i.Validate(); err == nil {
t.Fatal("expected error")
} else if want := `contact.email: invalid email: "foo"`; err.Error() != want {
t.Fatalf("got: %v, want: %v", err, want)
}
})
t.Run("invalid license", func(t *testing.T) {
i := openapi.Info{
Title: "Sample Pet Store App",
License: &openapi.License{},
Version: "1.0.1",
}
if err := i.Validate(); err == nil {
t.Fatal("expected error")
} else if want := `license.name is required`; err.Error() != want {
t.Fatalf("got: %v, want: %v", err, want)
}
})
t.Run("invalid extension", func(t *testing.T) {
i := openapi.Info{
Title: "Sample Pet Store App",
Version: "1.0.1",
Extensions: openapi.Extensions(`{"foo":"bar"}`),
}
if err := i.Validate(); err == nil {
t.Fatal("expected error")
} else if want := `foo: ` + openapi.ErrUnknownField.Error(); err.Error() != want {
t.Fatalf("got: %v, want: %v", err, want)
}
})
}