Commit a56a8334 authored by elcore's avatar elcore Committed by Matt Holt

caddyhttp: New index directive for alternate index file names (#1567)

* caddyhttp: Allow to alternate Index

* Move Index directive

* Fix misspelling outside this PR
parent 6b66b19d
......@@ -14,6 +14,7 @@ import (
_ "github.com/mholt/caddy/caddyhttp/fastcgi"
_ "github.com/mholt/caddy/caddyhttp/gzip"
_ "github.com/mholt/caddy/caddyhttp/header"
_ "github.com/mholt/caddy/caddyhttp/index"
_ "github.com/mholt/caddy/caddyhttp/internalsrv"
_ "github.com/mholt/caddy/caddyhttp/log"
_ "github.com/mholt/caddy/caddyhttp/markdown"
......
......@@ -11,7 +11,7 @@ import (
// ensure that the standard plugins are in fact plugged in
// and registered properly; this is a quick/naive way to do it.
func TestStandardPlugins(t *testing.T) {
numStandardPlugins := 30 // importing caddyhttp plugs in this many plugins
numStandardPlugins := 31 // importing caddyhttp plugs in this many plugins
s := caddy.DescribePlugins()
if got, want := strings.Count(s, "\n"), numStandardPlugins+5; got != want {
t.Errorf("Expected all standard plugins to be plugged in, got:\n%s", s)
......
......@@ -434,6 +434,7 @@ func RegisterDevDirective(name, before string) {
var directives = []string{
// primitive actions that set up the fundamental vitals of each config
"root",
"index",
"bind",
"maxrequestbody", // TODO: 'limits'
"timeouts",
......
package index
import (
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/staticfiles"
)
func init() {
caddy.RegisterPlugin("index", caddy.Plugin{
ServerType: "http",
Action: setupIndex,
})
}
func setupIndex(c *caddy.Controller) error {
var index []string
for c.Next() {
args := c.RemainingArgs()
if len(args) == 0 {
return c.Errf("Expected at least one index")
}
for _, in := range args {
index = append(index, in)
}
staticfiles.IndexPages = index
}
return nil
}
package index
import (
"testing"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/staticfiles"
)
func TestIndexIncompleteParams(t *testing.T) {
c := caddy.NewTestController("", "index")
err := setupIndex(c)
if err == nil {
t.Error("Expected an error, but didn't get one")
}
}
func TestIndex(t *testing.T) {
c := caddy.NewTestController("", "index a.html b.html c.html")
err := setupIndex(c)
if err != nil {
t.Errorf("Expected no errors, got: %v", err)
}
expectedIndex := []string{"a.html", "b.html", "c.html"}
if len(staticfiles.IndexPages) != 3 {
t.Errorf("Expected 3 values, got %v", len(staticfiles.IndexPages))
}
// Ensure ordering is correct
for i, actual := range staticfiles.IndexPages {
if actual != expectedIndex[i] {
t.Errorf("Expected value in position %d to be %v, got %v", i, expectedIndex[i], actual)
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment