@ -22,69 +22,84 @@ import (
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
)
func TestPhpFileMatcher ( t * testing . T ) {
func TestPHPFileMatcher ( t * testing . T ) {
for i , tc := range [ ] struct {
path string
path string
expectedPath string
matched bool
matched bool
} {
{
path : "/index.php" ,
path : "/index.php" ,
expectedPath : "/index.php" ,
matched : true ,
matched : true ,
} ,
{
path : "/index.php/somewhere" ,
path : "/index.php/somewhere" ,
expectedPath : "/index.php" ,
matched : true ,
matched : true ,
} ,
{
path : "/remote.php" ,
path : "/remote.php" ,
expectedPath : "/remote.php" ,
matched : true ,
matched : true ,
} ,
{
path : "/remote.php/somewhere" ,
path : "/remote.php/somewhere" ,
expectedPath : "/remote.php" ,
matched : true ,
matched : true ,
} ,
{
path : "/missingfile.php" ,
path : "/missingfile.php" ,
matched : false ,
} ,
{
path : "/notphp.php.txt" ,
path : "/notphp.php.txt" ,
expectedPath : "/notphp.php.txt" ,
matched : true ,
matched : true ,
} ,
{
path : "/notphp.php.txt/" ,
path : "/notphp.php.txt/" ,
expectedPath : "/notphp.php.txt" ,
matched : true ,
matched : true ,
} ,
{
path : "/notphp.php.txt.suffixed" ,
path : "/notphp.php.txt.suffixed" ,
matched : false ,
} ,
{
path : "/foo.php.php/index.php" ,
path : "/foo.php.php/index.php" ,
expectedPath : "/foo.php.php/index.php" ,
matched : true ,
matched : true ,
} ,
{
path : "/foo.php.PHP/index.php" ,
expectedPath : "/foo.php.PHP/index.php" ,
matched : true ,
} ,
{
// See https://github.com/caddyserver/caddy/issues/3623
path : "/%E2%C3" ,
expectedPath : "/%E2%C3" ,
matched : false ,
} ,
} {
m := & MatchFile {
Root : "./testdata" ,
TryFiles : [ ] string { "{http.request.uri.path}" } ,
TryFiles : [ ] string { "{http.request.uri.path}" , "{http.request.uri.path}/index.php" } ,
SplitPath : [ ] string { ".php" } ,
}
req := & http . Request { URL : & url . URL { Path : tc . path } }
u , err := url . Parse ( tc . path )
if err != nil {
t . Fatalf ( "Test %d: parsing path: %v" , i , err )
}
req := & http . Request { URL : u }
repl := caddyhttp . NewTestReplacer ( req )
result := m . Match ( req )
if result != tc . matched {
t . Fatalf ( "Test %d: match bool result: %v, expected: %v" , i , result , tc . matched )
t . Fatalf ( "Test %d: expected match=%t, got %t " , i , tc . matched , result )
}
rel , ok := repl . Get ( "http.matchers.file.relative" )
@ -99,4 +114,4 @@ func TestPhpFileMatcher(t *testing.T) {
t . Fatalf ( "Test %d: actual path: %v, expected: %v" , i , rel , tc . expectedPath )
}
}
}
}