-
Notifications
You must be signed in to change notification settings - Fork 33
/
hostname_suffix_filter_test.go
46 lines (36 loc) · 1.22 KB
/
hostname_suffix_filter_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
package cproxy
import (
"net/http/httptest"
"testing"
"github.com/smarty/assertions/should"
"github.com/smarty/gunit"
)
func TestHostnameSuffixFilterFixture(t *testing.T) {
gunit.Run(new(HostnameSuffixFilterFixture), t)
}
type HostnameSuffixFilterFixture struct {
*gunit.Fixture
filter Filter
}
func (this *HostnameSuffixFilterFixture) Setup() {
this.filter = NewHostnameSuffixFilter([]string{"domain:1234", ".domain2:5678"})
}
func (this *HostnameSuffixFilterFixture) TestDenied() {
this.assertUnauthorized("")
this.assertUnauthorized("a")
this.assertUnauthorized("domain:12345")
this.assertUnauthorized("DOMAIN:1234")
}
func (this *HostnameSuffixFilterFixture) assertUnauthorized(domain string) {
request := httptest.NewRequest("CONNECT", domain, nil)
this.So(this.filter.IsAuthorized(nil, request), should.BeFalse)
}
func (this *HostnameSuffixFilterFixture) TestAuthorized() {
this.assertAuthorized("domain:1234")
this.assertAuthorized("whatever.domain2:5678")
this.assertAuthorized("somedomain:1234") // only matches suffix
}
func (this *HostnameSuffixFilterFixture) assertAuthorized(domain string) {
request := httptest.NewRequest("CONNECT", domain, nil)
this.So(this.filter.IsAuthorized(nil, request), should.BeTrue)
}