-
Notifications
You must be signed in to change notification settings - Fork 0
/
parameter_collection_test.go
112 lines (104 loc) · 3.3 KB
/
parameter_collection_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
package rest_test
import (
"reflect"
"testing"
"github.com/ehsoc/rest"
"github.com/ehsoc/rest/encdec"
)
var testParameters = []struct {
parameter rest.Parameter
}{
{rest.NewQueryParameter("foo", reflect.String)},
{rest.NewURIParameter("foo", reflect.String)},
{rest.NewFileParameter("foo")},
{rest.NewFormDataParameter("foo", reflect.String, encdec.JSONEncoderDecoder{})},
{rest.NewHeaderParameter("foo", reflect.String)},
}
func TestGetParameter(t *testing.T) {
for _, tt := range testParameters {
params := rest.ParameterCollection{}
params.AddParameter(tt.parameter)
t.Run(string(tt.parameter.HTTPType), func(t *testing.T) {
gotParam, err := params.GetParameter(tt.parameter.HTTPType, tt.parameter.Name)
assertNoErrorFatal(t, err)
if !reflect.DeepEqual(gotParam, tt.parameter) {
t.Errorf("got: %v want: %v", gotParam, tt.parameter)
}
})
}
t.Run("parameter nil collection", func(t *testing.T) {
params := rest.ParameterCollection{}
_, err := params.GetParameter(rest.QueryParameter, "foo")
if _, ok := err.(*rest.ErrorParameterNotDefined); !ok {
t.Errorf("got: %v want: %T", err, rest.ErrorParameterNotDefined{})
}
})
t.Run("parameter not defined (empty parameter type)", func(t *testing.T) {
params := rest.ParameterCollection{}
params.AddParameter(rest.NewURIParameter("", reflect.Int))
_, err := params.GetParameter(rest.QueryParameter, "foo")
if _, ok := err.(*rest.ErrorParameterNotDefined); !ok {
t.Errorf("got: %v want: %T", err, rest.ErrorParameterNotDefined{})
}
})
}
func TestWithBody(t *testing.T) {
t.Run("set body", func(t *testing.T) {
car := Car{}
p := rest.NewFormDataParameter("car", reflect.Struct, encdec.JSONDecoder{}).WithBody(car)
if !reflect.DeepEqual(p.Body, car) {
t.Errorf("got: %v want: %v", p.Body, car)
}
})
t.Run("method has not a pointer receiver", func(t *testing.T) {
car := Car{}
p := rest.NewFormDataParameter("car", reflect.Struct, encdec.JSONDecoder{})
p.WithBody(car)
// WithBody shouldn't change p properties
if p.Body != nil {
t.Errorf("got: %v want: %v", p.Body, nil)
}
})
}
func TestAsOptional(t *testing.T) {
t.Run("as optional", func(t *testing.T) {
p := rest.NewURIParameter("foo", reflect.Int).AsOptional()
if p.Required {
t.Errorf("expecting to be false")
}
})
t.Run("method has not a pointer receiver", func(t *testing.T) {
p := rest.NewURIParameter("foo", reflect.Int)
p.AsOptional()
if !p.Required {
t.Errorf("expecting to be true")
}
})
}
func TestAsRequired(t *testing.T) {
t.Run("as required", func(t *testing.T) {
p := rest.NewQueryParameter("foo", reflect.String).AsRequired()
if !p.Required {
t.Errorf("expecting to be true")
}
})
t.Run("method has not a pointer receiver", func(t *testing.T) {
p := rest.NewQueryParameter("foo", reflect.String)
p.AsRequired()
if p.Required {
t.Errorf("expecting to be false")
}
})
}
func TestWithDescription(t *testing.T) {
description := "My description"
t.Run("set description", func(t *testing.T) {
p := rest.NewQueryParameter("foo", reflect.String).WithDescription(description)
assertStringEqual(t, p.Description, description)
})
t.Run("method has not a pointer receiver", func(t *testing.T) {
p := rest.NewQueryParameter("foo", reflect.String)
p.WithDescription(description)
assertStringEqual(t, p.Description, "")
})
}