-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_test.go
120 lines (111 loc) · 2.36 KB
/
command_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
package gurl
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"net/url"
"testing"
)
func mustURL(t *testing.T, u string) *url.URL {
result, err := url.Parse(u)
require.NoError(t, err)
return result
}
func TestArgsValidator(t *testing.T) {
tt := []struct {
name string
args []string
err string
expectedConfig *Config
}{
{
name: "a good execution with a valid url",
args: []string{
"https://example.com/request",
},
expectedConfig: &Config{
Url: mustURL(t, "https://example.com/request"),
},
},
{
name: "an invalid url",
args: []string{
"not a valid url\t",
},
err: "the URL provided is invalid: not a valid url\t: parse \"not a valid url\\t\": net/url: invalid control character in URL",
},
{
name: "no url",
args: []string{},
err: "you must provide a single URL to be called but you provided 0",
},
{
name: "too many urls",
args: []string{
"https://example,com/request",
"https://example.com/response",
},
err: "you must provide a single URL to be called but you provided 2",
},
}
for _, ts := range tt {
t.Run(ts.name, func(t *testing.T) {
c := &Config{}
err := ArgsValidator(c)(nil, ts.args)
if ts.err != "" {
assert.EqualError(t, err, ts.err)
} else {
require.NoError(t, err)
assert.Equal(t, ts.expectedConfig, c)
}
})
}
}
func TestOptionsValidator(t *testing.T) {
tt := []struct {
name string
expectedConfig *Config
headers []string
err string
}{
{
name: "with valid headers",
headers: []string{
"User-Agent: Sample",
"Cache: None",
"Cache: Respect",
},
expectedConfig: &Config{
Headers: map[string][]string{
"User-Agent": {
"Sample",
},
"Cache": {
"None",
"Respect",
},
},
},
},
{
name: "with invalid headers",
headers: []string{
"User-Agent",
},
err: "header is not a valid http header separated by `:`, value was: [User-Agent]",
},
}
for _, ts := range tt {
t.Run(ts.name, func(t *testing.T) {
c := &Config{
Headers: map[string][]string{},
}
err := OptionsValidator(c, ts.headers)(nil, nil)
if ts.err != "" {
assert.EqualError(t, err, ts.err)
} else {
require.NoError(t, err)
assert.Equal(t, ts.expectedConfig, c)
}
})
}
}