2015-05-08 03:10:48 +08:00
|
|
|
package setup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mholt/caddy/middleware/gzip"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGzip(t *testing.T) {
|
2015-06-20 21:59:33 +08:00
|
|
|
c := NewTestController(`gzip`)
|
2015-05-08 03:10:48 +08:00
|
|
|
|
|
|
|
mid, err := Gzip(c)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected no errors, but got: %v", err)
|
|
|
|
}
|
|
|
|
if mid == nil {
|
|
|
|
t.Fatal("Expected middleware, was nil instead")
|
|
|
|
}
|
|
|
|
|
2015-06-20 21:59:33 +08:00
|
|
|
handler := mid(EmptyNext)
|
2015-05-08 03:10:48 +08:00
|
|
|
myHandler, ok := handler.(gzip.Gzip)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Expected handler to be type Gzip, got: %#v", handler)
|
|
|
|
}
|
|
|
|
|
2015-06-20 21:59:33 +08:00
|
|
|
if !SameNext(myHandler.Next, EmptyNext) {
|
2015-05-08 03:10:48 +08:00
|
|
|
t.Error("'Next' field of handler was not set properly")
|
|
|
|
}
|
2015-06-07 07:58:37 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
input string
|
|
|
|
shouldErr bool
|
|
|
|
}{
|
|
|
|
{`gzip {`, true},
|
|
|
|
{`gzip {}`, true},
|
|
|
|
{`gzip a b`, true},
|
|
|
|
{`gzip a {`, true},
|
|
|
|
{`gzip { not f } `, true},
|
|
|
|
{`gzip { not } `, true},
|
|
|
|
{`gzip { not /file
|
|
|
|
ext .html
|
|
|
|
level 1
|
|
|
|
} `, false},
|
|
|
|
{`gzip { level 9 } `, false},
|
|
|
|
{`gzip { ext } `, true},
|
|
|
|
{`gzip { ext /f
|
|
|
|
} `, true},
|
|
|
|
{`gzip { not /file
|
|
|
|
ext .html
|
|
|
|
level 1
|
|
|
|
}
|
|
|
|
gzip`, false},
|
|
|
|
{`gzip { not /file
|
|
|
|
ext .html
|
|
|
|
level 1
|
|
|
|
}
|
|
|
|
gzip { not /file1
|
|
|
|
ext .htm
|
|
|
|
level 3
|
|
|
|
}
|
|
|
|
`, false},
|
2015-06-11 04:59:08 +08:00
|
|
|
{`gzip { not /file
|
|
|
|
ext .html
|
|
|
|
level 1
|
|
|
|
}
|
|
|
|
gzip { not /file1
|
|
|
|
ext .htm
|
|
|
|
level 3
|
|
|
|
}
|
|
|
|
`, false},
|
2015-07-06 13:36:48 +08:00
|
|
|
{`gzip { not /file
|
|
|
|
ext *
|
|
|
|
level 1
|
|
|
|
}
|
|
|
|
`, false},
|
2015-06-07 07:58:37 +08:00
|
|
|
}
|
|
|
|
for i, test := range tests {
|
2015-06-20 21:59:33 +08:00
|
|
|
c := NewTestController(test.input)
|
2015-06-07 07:58:37 +08:00
|
|
|
_, err := gzipParse(c)
|
|
|
|
if test.shouldErr && err == nil {
|
2015-06-11 04:59:08 +08:00
|
|
|
t.Errorf("Test %v: Expected error but found nil", i)
|
2015-06-07 07:58:37 +08:00
|
|
|
} else if !test.shouldErr && err != nil {
|
2015-06-11 04:59:08 +08:00
|
|
|
t.Errorf("Test %v: Expected no error but found error: %v", i, err)
|
2015-06-07 07:58:37 +08:00
|
|
|
}
|
|
|
|
}
|
2015-05-08 03:10:48 +08:00
|
|
|
}
|