42 lines
1.5 KiB
Go
42 lines
1.5 KiB
Go
package model
|
|
|
|
import "testing"
|
|
|
|
func TestRedirectLocation(t *testing.T) {
|
|
cases := []struct {
|
|
m Mapping
|
|
uri string
|
|
want string
|
|
}{
|
|
{Mapping{RedirectTo: "https://new.example.com"}, "/a?b=1", "https://new.example.com"},
|
|
{Mapping{RedirectTo: "https://new.example.com", RedirectKeepPath: true}, "/a?b=1", "https://new.example.com/a?b=1"},
|
|
{Mapping{RedirectTo: "https://new.example.com/", RedirectKeepPath: true}, "/", "https://new.example.com/"},
|
|
{Mapping{RedirectTo: "https://new.example.com/base", RedirectKeepPath: true}, "/x/y", "https://new.example.com/base/x/y"},
|
|
{Mapping{RedirectTo: "https://new.example.com", RedirectKeepPath: true}, "", "https://new.example.com/"},
|
|
}
|
|
for _, c := range cases {
|
|
if got := c.m.RedirectLocation(c.uri); got != c.want {
|
|
t.Errorf("%+v %q: got %q, want %q", c.m, c.uri, got, c.want)
|
|
}
|
|
}
|
|
if (Mapping{}).IsRedirect() || !(Mapping{RedirectTo: "https://x"}).IsRedirect() {
|
|
t.Error("IsRedirect is wrong")
|
|
}
|
|
}
|
|
|
|
func TestTopSources(t *testing.T) {
|
|
all := map[string]*SourceStat{
|
|
"a": {IP: "a", Conns: 1, BytesIn: 10, BytesOut: 5},
|
|
"b": {IP: "b", Conns: 9, BytesIn: 1},
|
|
"c": {IP: "c", Conns: 2, BytesIn: 100},
|
|
"d": {IP: "d", Conns: 3, BytesIn: 1},
|
|
}
|
|
got := TopSources(all, 3)
|
|
if len(got) != 3 || got[0].IP != "c" || got[1].IP != "a" || got[2].IP != "b" {
|
|
t.Fatalf("order: %+v", got) // c most bytes, a next, then b beats d on connections
|
|
}
|
|
if got := TopSources(all, 10); len(got) != 4 {
|
|
t.Fatalf("all: %+v", got)
|
|
}
|
|
}
|