-
Notifications
You must be signed in to change notification settings - Fork 0
/
urlegit_test.go
111 lines (90 loc) · 2.22 KB
/
urlegit_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
// SPDX-FileCopyrightText: 2023 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package urlegit
import (
"errors"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
)
var (
errAny = errors.New("any error")
)
func TestNew(t *testing.T) {
tests := []struct {
description string
opts []Option
expectedErr error
}{
{
description: "simple case",
},
{
description: "error case",
opts: []Option{Error(ErrSchemeNotAllowed)},
expectedErr: ErrSchemeNotAllowed,
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
assert := assert.New(t)
got, err := New(tc.opts...)
assert.ErrorIs(err, tc.expectedErr)
if tc.expectedErr == nil {
assert.NotNil(got)
}
})
}
}
func TestMust(t *testing.T) {
assert := assert.New(t)
assert.Panics(func() {
Must(Error(ErrSchemeNotAllowed))
})
c := Must()
assert.NotNil(c)
}
func TestText(t *testing.T) {
tests := []sharedTest{
{
description: "no hostname",
host: "http://",
expectedErr: ErrHostnameEmpty,
}, {
description: "url.Parse error",
host: ":invalid",
expectedErr: errAny,
},
}
testCommon(t, tests)
}
func TestURL(t *testing.T) {
assert := assert.New(t)
c := Must()
err := c.URL(nil)
assert.ErrorIs(err, ErrInvalidInput)
}
func TestLegit(t *testing.T) {
c := Must(OnlyAllowSchemes("http"))
assert.Equal(t, true, c.Legit("http://example.com"))
assert.Equal(t, false, c.Legit("https://example.com"))
}
func TestURLegit(t *testing.T) {
good, _ := url.Parse("http://example.com")
bad, _ := url.Parse("https://example.com")
c := Must(OnlyAllowSchemes("http"))
assert.Equal(t, true, c.URLegit(good))
assert.Equal(t, false, c.URLegit(bad))
}
func TestCheckerString(t *testing.T) {
c := Must()
assert.Equal(t, "urlegit.Checker{}", c.String())
c = Must(OnlyAllowSchemes("http"))
assert.Equal(t, "urlegit.Checker{ OnlyAllowSchemes('http') }", c.String())
c = Must(OnlyAllowSchemes("http", "https"))
assert.Equal(t, "urlegit.Checker{ OnlyAllowSchemes('http', 'https') }", c.String())
}
func TestErrorString(t *testing.T) {
assert.Equal(t, "Error()", Error(nil).String())
assert.Equal(t, "Error('any error')", Error(errAny).String())
}