Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 01:00 UTC
| 1 | package integration |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/caddyserver/caddy/v2/caddytest" |
| 11 | ) |
| 12 | |
| 13 | func TestTryFilesErrorCodeAfterQueryItem(t *testing.T) { |
| 14 | root := t.TempDir() |
| 15 | for name, content := range map[string]string{ |
| 16 | "style.css": "css", |
| 17 | "about.html": "about", |
| 18 | } { |
| 19 | if err := os.WriteFile(filepath.Join(root, name), []byte(content), 0o600); err != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | tester := caddytest.NewTester(t) |
| 25 | tester.InitServer(fmt.Sprintf(` |
| 26 | { |
| 27 | skip_install_trust |
| 28 | admin localhost:2999 |
| 29 | http_port 9080 |
| 30 | grace_period 1ns |
| 31 | } |
| 32 | http://localhost:9080 { |
| 33 | root * %q |
| 34 | try_files {path} {path}.html?{query} =404 |
| 35 | file_server |
| 36 | } |
| 37 | `, filepath.ToSlash(root)), "caddyfile") |
| 38 | |
| 39 | tester.AssertGetResponse("http://localhost:9080/style.css", 200, "css") |
| 40 | tester.AssertGetResponse("http://localhost:9080/about?x=1", 200, "about") |
| 41 | |
| 42 | req, err := http.NewRequest(http.MethodGet, "http://localhost:9080/missing", nil) |
| 43 | if err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | tester.AssertResponseCode(req, http.StatusNotFound) |
| 47 | } |
| 48 | |
| 49 | func TestTryFilesMatchesSiteRootDirectory(t *testing.T) { |
| 50 | root := t.TempDir() |
| 51 | if err := os.WriteFile(filepath.Join(root, "index.html"), []byte("root"), 0o600); err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | |
| 55 | tester := caddytest.NewTester(t) |
| 56 | tester.InitServer(fmt.Sprintf(` |
| 57 | { |
| 58 | skip_install_trust |
| 59 | admin localhost:2999 |
| 60 | http_port 9080 |
| 61 | grace_period 1ns |
| 62 | } |
| 63 | http://localhost:9080 { |
| 64 | root * %q |
| 65 | route { |
| 66 | try_files {path} {path}/ =404 |
| 67 | file_server |
| 68 | } |
| 69 | } |
| 70 | `, filepath.ToSlash(root)), "caddyfile") |
| 71 | |
| 72 | tester.AssertGetResponse("http://localhost:9080/", http.StatusOK, "root") |
| 73 | } |
| 74 | |
| 75 | func TestSkippedGroupedRouteKeepsRegexpCapture(t *testing.T) { |
| 76 | tester := caddytest.NewTester(t) |
| 77 | tester.InitServer(` |
| 78 | { |
| 79 | skip_install_trust |
| 80 | admin localhost:2999 |
| 81 | http_port 9080 |
| 82 | grace_period 1ns |
| 83 | } |
| 84 | http://localhost:9080 { |
| 85 | @x path_regexp x ^/x/(.*)$ |
| 86 | @y path_regexp x ^/(.*)$ |
| 87 | rewrite @x /one/{re.x.1} |
| 88 | rewrite @y /two/{re.x.1} |
| 89 | respond "path={path} cap={re.x.1}" |
| 90 | } |
| 91 | `, "caddyfile") |
| 92 | |
| 93 | tester.AssertGetResponse("http://localhost:9080/x/q", 200, "path=/one/q cap=q") |
| 94 | tester.AssertGetResponse("http://localhost:9080/z", 200, "path=/two/z cap=z") |
| 95 | } |