-
Notifications
You must be signed in to change notification settings - Fork 4
/
security_requirement_test.go
95 lines (90 loc) · 2.29 KB
/
security_requirement_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
package openapi_test
import (
"encoding/json"
"reflect"
"testing"
openapi "github.com/nasa9084/go-openapi"
yaml "gopkg.in/yaml.v2"
)
func TestSecurityRequirement_Validate(t *testing.T) {
candidates := []candidate{
{"empty", openapi.SecurityRequirement{}, nil},
}
testValidater(t, candidates)
}
func TestSecurityRequirement_UnmarshalYAML(t *testing.T) {
yml := `apiKey: []`
secReq := openapi.SecurityRequirement{}
if err := yaml.Unmarshal([]byte(yml), &secReq); err != nil {
t.Error(err)
return
}
ar := secReq.Get("apiKey")
if ar == nil {
t.Error("securityRequirement.Get(`apiKey`) should be [], not nil")
return
}
if len(ar) != 0 {
t.Error("securityRequirement.Get(`apiKey`) should be zero length array")
return
}
yml = `apiKey:
- foo
- bar
`
secReq = openapi.SecurityRequirement{}
if err := yaml.Unmarshal([]byte(yml), &secReq); err != nil {
t.Error(err)
return
}
ar = secReq.Get("apiKey")
if ar == nil {
t.Error("securityRequirement.Get(`apiKey`) should not be nil, but nil")
return
}
if !reflect.DeepEqual(ar, []string{"foo", "bar"}) {
t.Errorf("securityRequirement.Get(`apiKey`) should be [foo bar], but %v", ar)
return
}
}
func TestSecurityRequirement_UnmarshalJSON(t *testing.T) {
// empty list case
jsn := `{"apiKey": []}`
secReq := openapi.SecurityRequirement{}
if err := json.Unmarshal([]byte(jsn), &secReq); err != nil {
t.Error(err)
return
}
ar := secReq.Get("apiKey")
if ar == nil {
t.Error("securityRequirement.Get(`apiKey`) should be [], not nil")
return
}
if len(ar) != 0 {
t.Error("securityRequirement.Get(`apiKey`) should be zero length array")
return
}
// not empty list case
jsn = `{"apiKey": ["foo", "bar"]}`
secReq = openapi.SecurityRequirement{}
if err := json.Unmarshal([]byte(jsn), &secReq); err != nil {
t.Error(err)
return
}
ar = secReq.Get("apiKey")
if ar == nil {
t.Error("securityRequirement.Get(`apiKey`) should not be nil, but nil")
return
}
if !reflect.DeepEqual(ar, []string{"foo", "bar"}) {
t.Errorf("securityRequirement.Get(`apiKey`) should be [foo bar], but %v", ar)
return
}
// invalid case
jsn = `{"apiKey": "foo"}` // value should be array
secReq = openapi.SecurityRequirement{}
if err := json.Unmarshal([]byte(jsn), &secReq); err == nil {
t.Error("error should be occurred, but not")
return
}
}