2017-04-17 23:47:53 +03:00
package server
2016-06-06 16:27:16 +03:00
import (
"net/http"
2016-06-06 23:40:42 +03:00
"net/url"
2016-06-06 16:27:16 +03:00
"testing"
2017-04-17 23:47:53 +03:00
"github.com/containous/mux"
2017-06-03 15:58:35 +03:00
"github.com/containous/traefik/testhelpers"
2017-05-28 06:50:03 +03:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2016-06-06 16:27:16 +03:00
)
func TestParseOneRule ( t * testing . T ) {
router := mux . NewRouter ( )
route := router . NewRoute ( )
serverRoute := & serverRoute { route : route }
rules := & Rules { route : serverRoute }
expression := "Host:foo.bar"
routeResult , err := rules . Parse ( expression )
2017-05-28 06:50:03 +03:00
require . NoError ( t , err , "Error while building route for %s" , expression )
2016-06-06 16:27:16 +03:00
2017-06-03 15:58:35 +03:00
request := testhelpers . MustNewRequest ( http . MethodGet , "http://foo.bar" , nil )
2016-06-06 16:27:16 +03:00
routeMatch := routeResult . Match ( request , & mux . RouteMatch { Route : routeResult } )
2017-05-28 06:50:03 +03:00
assert . True ( t , routeMatch , "Rule %s don't match." , expression )
2016-06-06 16:27:16 +03:00
}
func TestParseTwoRules ( t * testing . T ) {
router := mux . NewRouter ( )
route := router . NewRoute ( )
serverRoute := & serverRoute { route : route }
rules := & Rules { route : serverRoute }
2016-10-14 17:04:09 +03:00
expression := "Host: Foo.Bar ; Path:/FOObar"
2016-06-06 16:27:16 +03:00
routeResult , err := rules . Parse ( expression )
2017-05-28 06:50:03 +03:00
require . NoError ( t , err , "Error while building route for %s." , expression )
2016-06-06 16:27:16 +03:00
2017-06-03 15:58:35 +03:00
request := testhelpers . MustNewRequest ( http . MethodGet , "http://foo.bar/foobar" , nil )
2016-06-06 16:27:16 +03:00
routeMatch := routeResult . Match ( request , & mux . RouteMatch { Route : routeResult } )
2017-05-28 06:50:03 +03:00
assert . False ( t , routeMatch , "Rule %s don't match." , expression )
2016-11-17 17:36:10 +03:00
2017-06-03 15:58:35 +03:00
request = testhelpers . MustNewRequest ( http . MethodGet , "http://foo.bar/FOObar" , nil )
2016-11-17 17:36:10 +03:00
routeMatch = routeResult . Match ( request , & mux . RouteMatch { Route : routeResult } )
2017-05-28 06:50:03 +03:00
assert . True ( t , routeMatch , "Rule %s don't match." , expression )
2016-06-06 16:27:16 +03:00
}
2016-06-06 15:54:45 +03:00
2016-08-05 21:42:45 +03:00
func TestParseDomains ( t * testing . T ) {
rules := & Rules { }
2017-05-28 06:50:03 +03:00
tests := [ ] struct {
expression string
domain [ ] string
} {
{
expression : "Host:foo.bar,test.bar" ,
domain : [ ] string { "foo.bar" , "test.bar" } ,
} ,
{
expression : "Path:/test" ,
domain : [ ] string { } ,
} ,
{
expression : "Host:foo.bar;Path:/test" ,
domain : [ ] string { "foo.bar" } ,
} ,
{
expression : "Host: Foo.Bar ;Path:/test" ,
domain : [ ] string { "foo.bar" } ,
} ,
}
for _ , test := range tests {
test := test
t . Run ( test . expression , func ( t * testing . T ) {
t . Parallel ( )
domains , err := rules . ParseDomains ( test . expression )
require . NoError ( t , err , "%s: Error while parsing domain." , test . expression )
assert . EqualValues ( t , test . domain , domains , "%s: Error parsing domains from expression." , test . expression )
} )
2016-08-05 21:42:45 +03:00
}
}
2016-06-06 15:54:45 +03:00
func TestPriorites ( t * testing . T ) {
router := mux . NewRouter ( )
router . StrictSlash ( true )
rules := & Rules { route : & serverRoute { route : router . NewRoute ( ) } }
2017-05-28 06:50:03 +03:00
expression01 := "PathPrefix:/foo"
routeFoo , err := rules . Parse ( expression01 )
require . NoError ( t , err , "Error while building route for %s" , expression01 )
2016-06-06 15:54:45 +03:00
fooHandler := & fakeHandler { name : "fooHandler" }
routeFoo . Handler ( fooHandler )
2017-05-28 06:50:03 +03:00
routeMatch := router . Match ( & http . Request { URL : & url . URL { Path : "/foo" } } , & mux . RouteMatch { } )
assert . True ( t , routeMatch , "Error matching route" )
2016-06-06 15:54:45 +03:00
2017-05-28 06:50:03 +03:00
routeMatch = router . Match ( & http . Request { URL : & url . URL { Path : "/fo" } } , & mux . RouteMatch { } )
assert . False ( t , routeMatch , "Error matching route" )
2016-06-06 15:54:45 +03:00
multipleRules := & Rules { route : & serverRoute { route : router . NewRoute ( ) } }
2017-05-28 06:50:03 +03:00
expression02 := "PathPrefix:/foobar"
routeFoobar , err := multipleRules . Parse ( expression02 )
require . NoError ( t , err , "Error while building route for %s" , expression02 )
2016-06-06 15:54:45 +03:00
foobarHandler := & fakeHandler { name : "foobarHandler" }
routeFoobar . Handler ( foobarHandler )
2017-05-28 06:50:03 +03:00
routeMatch = router . Match ( & http . Request { URL : & url . URL { Path : "/foo" } } , & mux . RouteMatch { } )
2016-06-06 15:54:45 +03:00
2017-05-28 06:50:03 +03:00
assert . True ( t , routeMatch , "Error matching route" )
2016-06-06 15:54:45 +03:00
2017-05-28 06:50:03 +03:00
fooMatcher := & mux . RouteMatch { }
routeMatch = router . Match ( & http . Request { URL : & url . URL { Path : "/foobar" } } , fooMatcher )
assert . True ( t , routeMatch , "Error matching route" )
assert . NotEqual ( t , fooMatcher . Handler , foobarHandler , "Error matching priority" )
assert . Equal ( t , fooMatcher . Handler , fooHandler , "Error matching priority" )
2016-06-06 15:54:45 +03:00
routeFoo . Priority ( 1 )
routeFoobar . Priority ( 10 )
router . SortRoutes ( )
foobarMatcher := & mux . RouteMatch { }
2017-05-28 06:50:03 +03:00
routeMatch = router . Match ( & http . Request { URL : & url . URL { Path : "/foobar" } } , foobarMatcher )
2016-06-06 15:54:45 +03:00
2017-05-28 06:50:03 +03:00
assert . True ( t , routeMatch , "Error matching route" )
assert . Equal ( t , foobarMatcher . Handler , foobarHandler , "Error matching priority" )
assert . NotEqual ( t , foobarMatcher . Handler , fooHandler , "Error matching priority" )
2016-06-06 15:54:45 +03:00
}
type fakeHandler struct {
name string
}
2017-05-26 18:03:14 +03:00
func ( h * fakeHandler ) ServeHTTP ( http . ResponseWriter , * http . Request ) { }