Skip to content

Commit

Permalink
Add tests for setup.go
Browse files Browse the repository at this point in the history
  • Loading branch information
mholt committed Jun 25, 2017
1 parent ea54c80 commit 89e9c03
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions setup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package restic

import (
"testing"

"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver"
)

func TestSetup(t *testing.T) {
c := caddy.NewTestController("http", `restic /basepath`)
err := setup(c)
if err != nil {
t.Fatalf("Expected no errors, got: %v", err)
}

mids := httpserver.GetConfig(c).Middleware()
if len(mids) == 0 {
t.Fatal("Expected middleware, had 0 instead")
}

handler := mids[0](httpserver.EmptyNext)
myHandler, ok := handler.(ResticHandler)
if !ok {
t.Fatalf("Expected handler to be type ResticHandler, got: %#v", handler)
}

if myHandler.BasePath != "/basepath" {
t.Error("Expected base path to be /basepath")
}
if !httpserver.SameNext(myHandler.Next, httpserver.EmptyNext) {
t.Error("'Next' field of handler was not set properly")
}

}

func TestExtParse(t *testing.T) {
tests := []struct {
inputStr string
shouldErr bool
expectedBasePath string
}{
{`restic`, false, "/"},
{`restic /basepath`, false, "/basepath"},
{`restic /basepath /backups`, false, "/basepath"},
{`restic /basepath /backups extra`, true, "/basepath"},
}
for i, test := range tests {
c := caddy.NewTestController("http", test.inputStr)
err := setup(c)
if err == nil && test.shouldErr {
t.Errorf("Test %d didn't error, but it should have", i)
} else if err != nil && !test.shouldErr {
t.Errorf("Test %d errored, but it shouldn't have; got '%v'", i, err)
}
if test.shouldErr {
continue
}

mids := httpserver.GetConfig(c).Middleware()
if len(mids) == 0 {
t.Fatalf("Test %d: Expected middleware, had 0 instead", i)
}

handler := mids[0](httpserver.EmptyNext)
myHandler, ok := handler.(ResticHandler)
if !ok {
t.Fatalf("Expected handler to be type ResticHandler, got: %#v", handler)
}

if test.expectedBasePath != myHandler.BasePath {
t.Errorf("Test %d: Expected base path to be %s but was %s", i, test.expectedBasePath, myHandler.BasePath)
}
}

}

0 comments on commit 89e9c03

Please sign in to comment.