The constructor
diff --git a/chain.go b/chain.go new file mode 100644 index 0000000..29aeee1 --- /dev/null +++ b/chain.go
@@ -0,0 +1,20 @@ +package alice + +import "net/http" + +// A constructor for a piece of middleware. +// Most middleware use this constructor out of the box, +// so in most cases you can just pass somepackage.New +type Constructor func(http.Handler) http.Handler + +type Chain struct { + constructors []Constructor +} + +// Creates a new chain, memorizing the given middleware constructors +func New(constructors ...Constructor) Chain { + c := Chain{} + c.constructors = append(c.constructors, constructors...) + + return c +}
diff --git a/chain_test.go b/chain_test.go new file mode 100644 index 0000000..bbea276 --- /dev/null +++ b/chain_test.go
@@ -0,0 +1,24 @@ +package alice + +import ( + "net/http" + "testing" + + "github.com/stretchr/testify/assert" +) + +// Tests creating a new chain +func TestNew(t *testing.T) { + c1 := func(h http.Handler) http.Handler { + return nil + } + c2 := func(h http.Handler) http.Handler { + return http.StripPrefix("potato", nil) + } + + slice := []Constructor{c1, c2} + + chain := New(slice...) + assert.Equal(t, chain.constructors[0], slice[0]) + assert.Equal(t, chain.constructors[1], slice[1]) +}