-
Notifications
You must be signed in to change notification settings - Fork 21
/
urlFilter_test.go
63 lines (53 loc) · 2.07 KB
/
urlFilter_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
// SPDX-FileCopyrightText: 2017 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package main
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func testURLFilterInvalidDefaultScheme(t *testing.T) {
assert := assert.New(t)
urlFilter, err := NewURLFilter(&Outbounder{
DefaultScheme: "http",
AllowedSchemes: []string{"https"},
})
assert.Nil(urlFilter)
assert.Error(err)
}
func testURLFilterFilter(t *testing.T) {
var (
assert = assert.New(t)
require = require.New(t)
testData = []struct {
outbounder *Outbounder
input string
expectedFiltered string
expectsError bool
}{
{nil, "foobar.com", "https://foobar.com", false},
{nil, "foobar.com?test=1&a=2", "https://foobar.com?test=1&a=2", false},
{nil, "foobar.com:8080", "https://foobar.com:8080", false},
{nil, "xxx://foobar.com", "", true},
{nil, "http://foobar.com:1234", "", true},
{&Outbounder{DefaultScheme: "ftp", AllowedSchemes: []string{"ftp", "https"}}, "foobar.com", "ftp://foobar.com", false},
{&Outbounder{DefaultScheme: "ftp", AllowedSchemes: []string{"ftp", "https"}}, "foobar.com?test=1", "ftp://foobar.com?test=1", false},
{&Outbounder{DefaultScheme: "ftp", AllowedSchemes: []string{"ftp", "https"}}, "https://foobar.com", "https://foobar.com", false},
{&Outbounder{DefaultScheme: "ftp", AllowedSchemes: []string{"ftp", "https"}}, "https://foobar.com?test=1", "https://foobar.com?test=1", false},
{&Outbounder{DefaultScheme: "ftp", AllowedSchemes: []string{"ftp", "https"}}, "http://foobar.com", "", true},
}
)
for _, record := range testData {
t.Logf("%#v", record)
urlFilter, err := NewURLFilter(record.outbounder)
require.NotNil(urlFilter)
require.NoError(err)
actualFiltered, err := urlFilter.Filter(record.input)
assert.Equal(record.expectedFiltered, actualFiltered)
assert.Equal(record.expectsError, err != nil)
}
}
func TestURLFilter(t *testing.T) {
t.Run("InvalidDefaultScheme", testURLFilterInvalidDefaultScheme)
t.Run("Filter", testURLFilterFilter)
}