-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontact_test.go
78 lines (62 loc) · 1.79 KB
/
contact_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
package openapi_test
import (
"net/url"
"testing"
"github.com/MarkRosemaker/openapi"
"github.com/go-api-libs/types"
)
func TestContact_JSON(t *testing.T) {
t.Parallel()
testJSON(t, []byte(`{
"name": "API Support",
"url": "https://www.example.com/support",
"email": "support@example.com"
}`), &openapi.Contact{})
testJSON(t, []byte(`{
"name": "API Support",
"url": "https://www.example.com/support",
"email": "support@example.com",
"x-foo": true,
"x-bar": ["one", "two"]
}`), &openapi.Contact{})
}
func TestContact_Validate(t *testing.T) {
t.Parallel()
t.Run("empty", func(t *testing.T) {
c := openapi.Contact{}
if err := c.Validate(); err != nil {
t.Fatal(err)
}
})
t.Run("fix URL", func(t *testing.T) {
c := openapi.Contact{URL: &url.URL{Host: "example.com"}}
if err := c.Validate(); err != nil {
t.Fatal(err)
}
if want := "https://example.com"; c.URL.String() != want {
t.Fatalf("url not fixed, want: %q, got: %q", want, c.URL)
}
})
t.Run("valid email", func(t *testing.T) {
c := openapi.Contact{Email: types.Email("max@example.com")}
if err := c.Validate(); err != nil {
t.Fatal(err)
}
})
t.Run("invalid email", func(t *testing.T) {
c := openapi.Contact{Email: types.Email("max[at]example.com")}
if err := c.Validate(); err == nil {
t.Fatal("expected error")
} else if want := `email: invalid email: "max[at]example.com"`; err.Error() != want {
t.Fatalf("got: %v, want: %v", err, want)
}
})
t.Run("invalid extension", func(t *testing.T) {
c := openapi.Contact{Extensions: openapi.Extensions(`{"foo":"bar"}`)}
if err := c.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)
}
})
}