forked from smarty/cproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hostname_filter_test.go
52 lines (42 loc) · 1.48 KB
/
hostname_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
47
48
49
50
51
52
package cproxy
import (
"net/http/httptest"
"testing"
"github.com/smartystreets/assertions/should"
"github.com/smartystreets/gunit"
)
func TestHostnameFilterFixture(t *testing.T) {
gunit.Run(new(HostnameFilterFixture), t)
}
type HostnameFilterFixture struct {
*gunit.Fixture
filter Filter
}
func (this *HostnameFilterFixture) Setup() {
this.filter = NewHostnameFilter([]string{"domain:1234", "sub.domain2:5678", "*.xyz.domain3:9012"})
}
func (this *HostnameFilterFixture) TestDenied() {
this.assertUnauthorized("")
this.assertUnauthorized("a")
this.assertUnauthorized("domain:12345")
this.assertUnauthorized("DOMAIN:1234")
this.assertUnauthorized("whatever.domain2:5678")
this.assertUnauthorized("whatever.sub.domain2:5678")
this.assertUnauthorized("somedomain:1234") // must match exactly
this.assertUnauthorized("domain3:1234")
this.assertUnauthorized("xyz.domain3:9012")
}
func (this *HostnameFilterFixture) assertUnauthorized(domain string) {
request := httptest.NewRequest("CONNECT", domain, nil)
this.So(this.filter.IsAuthorized(nil, request), should.BeFalse)
}
func (this *HostnameFilterFixture) TestAuthorized() {
this.assertAuthorized("domain:1234")
this.assertAuthorized("sub.domain2:5678")
this.assertAuthorized("a.xyz.domain3:9012")
this.assertAuthorized("b.xyz.domain3:9012")
}
func (this *HostnameFilterFixture) assertAuthorized(domain string) {
request := httptest.NewRequest("CONNECT", domain, nil)
this.So(this.filter.IsAuthorized(nil, request), should.BeTrue)
}