Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions caddy/caddy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1383,14 +1383,12 @@ func TestWorkerMatchDirectiveWithMultipleWorkers(t *testing.T) {
require.NoError(t, err, "static.txt file must be readable for this test")
tester.AssertGetResponse("http://localhost:"+testPort+"/files/static.txt", http.StatusOK, string(expectedFileResponse))

// 404 if the request falls through
tester.AssertGetResponse("http://localhost:"+testPort+"/not-matched", http.StatusNotFound, "")

// serve php file directly as fallback
tester.AssertGetResponse("http://localhost:"+testPort+"/hello.php", http.StatusOK, "Hello from PHP")

// serve worker file directly as fallback
// serve index.php file directly as fallback
tester.AssertGetResponse("http://localhost:"+testPort+"/index.php", http.StatusOK, "I am by birth a Genevese (i not set)")
tester.AssertGetResponse("http://localhost:"+testPort+"/not-matched", http.StatusOK, "I am by birth a Genevese (i not set)")
}

func TestWorkerMatchDirectiveWithoutFileServer(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions caddy/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ func parsePhpServer(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error)
redirMatcherSet := caddy.ModuleMap{
"file": h.JSON(fileserver.MatchFile{
TryFiles: []string{dirIndex},
Root: phpsrv.Root,
}),
"not": h.JSON(caddyhttp.MatchNot{
MatcherSetsRaw: []caddy.ModuleMap{
Expand Down Expand Up @@ -491,6 +492,7 @@ func parsePhpServer(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error)
TryFiles: tryFiles,
TryPolicy: tryPolicy,
SplitPath: extensions,
Root: phpsrv.Root,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one question though because I currently can't test it - this still falls back to Caddy's root if the one in phpsrv is empty?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it will fallback, an empty string is the default in the struct (otherwise tests would also fail)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add some more tests for all cases

}),
}
rewriteHandler := rewrite.Rewrite{
Expand Down
52 changes: 52 additions & 0 deletions caddy/module_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package caddy_test

import (
"net/http"
"os"
"strconv"
"testing"

"github.com/caddyserver/caddy/v2/caddytest"
)

func TestRootBehavesTheSameOutsideAndInsidePhpServer(t *testing.T) {
tester := caddytest.NewTester(t)
testPortNum, _ := strconv.Atoi(testPort)
testPortTwo := strconv.Itoa(testPortNum + 1)
expectedFileResponse, _ := os.ReadFile("../testdata/files/static.txt")
hostWithRootOutside := "http://localhost:" + testPort
hostWithRootInside := "http://localhost:" + testPortTwo
tester.InitServer(`
{
skip_install_trust
admin localhost:2999
}

`+hostWithRootOutside+` {
root ../testdata
php_server
}

`+hostWithRootInside+` {
php_server {
root ../testdata
}
}
`, "caddyfile")

// serve a static file
tester.AssertGetResponse(hostWithRootOutside+"/files/static.txt", http.StatusOK, string(expectedFileResponse))
tester.AssertGetResponse(hostWithRootInside+"/files/static.txt", http.StatusOK, string(expectedFileResponse))

// serve a php file
tester.AssertGetResponse(hostWithRootOutside+"/hello.php", http.StatusOK, "Hello from PHP")
tester.AssertGetResponse(hostWithRootInside+"/hello.php", http.StatusOK, "Hello from PHP")

// fallback to index.php
tester.AssertGetResponse(hostWithRootOutside+"/some-path", http.StatusOK, "I am by birth a Genevese (i not set)")
tester.AssertGetResponse(hostWithRootInside+"/some-path", http.StatusOK, "I am by birth a Genevese (i not set)")

// fallback to directory index ('dirIndex' in module.go)
tester.AssertGetResponse(hostWithRootOutside+"/dirindex/", http.StatusOK, "Hello from directory index.php")
tester.AssertGetResponse(hostWithRootInside+"/dirindex/", http.StatusOK, "Hello from directory index.php")
}
3 changes: 3 additions & 0 deletions testdata/dirindex/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

echo "Hello from directory index.php";