Commit 1017142d authored by Dipen Patel's avatar Dipen Patel

Made style adjustments to browse and redir tests

parent 2b1cc77f
......@@ -5,13 +5,10 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
"github.com/mholt/caddy/caddy/parse"
"github.com/mholt/caddy/middleware/browse"
"github.com/mholt/caddy/server"
)
func TestBrowse(t *testing.T) {
......@@ -30,45 +27,37 @@ func TestBrowse(t *testing.T) {
tempTemplatePath := filepath.Join(".", tempTemplate.Name())
testTokens := []string{
"browse " + tempDirPath + "\n browse .",
"browse /",
"browse . " + tempTemplatePath,
"browse . " + nonExistantDirPath,
"browse " + tempDirPath + "\n browse " + tempDirPath,
}
tests := []struct {
for i, test := range []struct {
input string
expectedPathScope []string
shouldErr bool
}{
// test case #0 tests handling of multiple pathscopes
{[]string{tempDirPath, "."}, false},
{"browse " + tempDirPath + "\n browse .", []string{tempDirPath, "."}, false},
// test case #1 tests instantiation of browse.Config with default values
{[]string{"/"}, false},
{"browse /", []string{"/"}, false},
// test case #2 tests detectaction of custom template
{[]string{"."}, false},
{"browse . " + tempTemplatePath, []string{"."}, false},
// test case #3 tests detection of non-existant template
{nil, true},
{"browse . " + nonExistantDirPath, nil, true},
// test case #4 tests detection of duplicate pathscopes
{nil, true},
}
{"browse " + tempDirPath + "\n browse " + tempDirPath, nil, true},
} {
for i, test := range tests {
c := &Controller{Config: &server.Config{Root: "."}, Dispenser: parse.NewDispenser("", strings.NewReader(testTokens[i]))}
retrievedFunc, err := Browse(c)
// c := &Controller{Config: &server.Config{Root: "."}, Dispenser: parse.NewDispenser("", strings.NewReader(testTokens[i]))}
recievedFunc, err := Browse(NewTestController(test.input))
if err != nil && !test.shouldErr {
t.Errorf("Test case #%d recieved an error of %v", i, err)
}
if test.expectedPathScope == nil {
continue
}
retrievedConfigs := retrievedFunc(nil).(browse.Browse).Configs
for j, config := range retrievedConfigs {
recievedConfigs := recievedFunc(nil).(browse.Browse).Configs
for j, config := range recievedConfigs {
if config.PathScope != test.expectedPathScope[j] {
t.Errorf("Test case #%d expected a pathscope of %v, but got %v", i, test.expectedPathScope, config.PathScope)
}
......
......@@ -8,8 +8,8 @@ import (
func TestRedir(t *testing.T) {
tests := []struct {
testToken string
for j, test := range []struct {
input string
shouldErr bool
expectedRules []redirect.Rule
}{
......@@ -42,27 +42,24 @@ func TestRedir(t *testing.T) {
// test case #9 tests the detection of duplicate redirections
{"redir {\n /bar /foo 304 \n} redir {\n /bar /foo 304 \n}", true, []redirect.Rule{redirect.Rule{}}},
}
for j, test := range tests {
c := NewTestController(test.testToken)
retrievedFunc, err := Redir(c)
} {
recievedFunc, err := Redir(NewTestController(test.input))
if err != nil && !test.shouldErr {
t.Errorf("Test case #%d recieved an error of %v", j, err)
} else if test.shouldErr {
continue
}
retrievedRules := retrievedFunc(nil).(redirect.Redirect).Rules
recievedRules := recievedFunc(nil).(redirect.Redirect).Rules
for i, retrievedRule := range retrievedRules {
if retrievedRule.FromPath != test.expectedRules[i].FromPath {
t.Errorf("Test case #%d.%d expected a from path of %s, but recieved a from path of %s", j, i, test.expectedRules[i].FromPath, retrievedRule.FromPath)
for i, recievedRule := range recievedRules {
if recievedRule.FromPath != test.expectedRules[i].FromPath {
t.Errorf("Test case #%d.%d expected a from path of %s, but recieved a from path of %s", j, i, test.expectedRules[i].FromPath, recievedRule.FromPath)
}
if retrievedRule.To != test.expectedRules[i].To {
t.Errorf("Test case #%d.%d expected a TO path of %s, but recieved a TO path of %s", j, i, test.expectedRules[i].To, retrievedRule.To)
if recievedRule.To != test.expectedRules[i].To {
t.Errorf("Test case #%d.%d expected a TO path of %s, but recieved a TO path of %s", j, i, test.expectedRules[i].To, recievedRule.To)
}
if retrievedRule.Code != test.expectedRules[i].Code {
t.Errorf("Test case #%d.%d expected a HTTP status code of %d, but recieved a code of %d", j, i, test.expectedRules[i].Code, retrievedRule.Code)
if recievedRule.Code != test.expectedRules[i].Code {
t.Errorf("Test case #%d.%d expected a HTTP status code of %d, but recieved a code of %d", j, i, test.expectedRules[i].Code, recievedRule.Code)
}
}
}
......
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