From 2b1cc77f4b74d5e32d30078bf343b0fd0141b040 Mon Sep 17 00:00:00 2001 From: Dipen Patel Date: Sat, 7 Nov 2015 18:03:21 -0500 Subject: [PATCH 1/3] Wrote tests for browse.go and redir.go --- caddy/setup/browse_test.go | 77 ++++++++++++++++++++++++++++++++++++++ caddy/setup/redir_test.go | 70 ++++++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 caddy/setup/browse_test.go create mode 100644 caddy/setup/redir_test.go diff --git a/caddy/setup/browse_test.go b/caddy/setup/browse_test.go new file mode 100644 index 00000000..6e605997 --- /dev/null +++ b/caddy/setup/browse_test.go @@ -0,0 +1,77 @@ +package setup + +import ( + "io/ioutil" + "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) { + + tempDirPath, err := getTempDirPath() + if err != nil { + t.Fatalf("BeforeTest: Failed to find an existing directory for testing! Error was: %v", err) + } + nonExistantDirPath := filepath.Join(tempDirPath, strconv.Itoa(int(time.Now().UnixNano()))) + + tempTemplate, err := ioutil.TempFile(".", "tempTemplate") + if err != nil { + t.Fatalf("BeforeTest: Failed to create a temporary file in the working directory! Error was: %v", err) + } + defer os.Remove(tempTemplate.Name()) + + tempTemplatePath := filepath.Join(".", tempTemplate.Name()) + + testTokens := []string{ + "browse " + tempDirPath + "\n browse .", + "browse /", + "browse . " + tempTemplatePath, + "browse . " + nonExistantDirPath, + "browse " + tempDirPath + "\n browse " + tempDirPath, + } + + tests := []struct { + expectedPathScope []string + shouldErr bool + }{ + // test case #0 tests handling of multiple pathscopes + {[]string{tempDirPath, "."}, false}, + + // test case #1 tests instantiation of browse.Config with default values + {[]string{"/"}, false}, + + // test case #2 tests detectaction of custom template + {[]string{"."}, false}, + + // test case #3 tests detection of non-existant template + {nil, true}, + + // test case #4 tests detection of duplicate pathscopes + {nil, true}, + } + + for i, test := range tests { + c := &Controller{Config: &server.Config{Root: "."}, Dispenser: parse.NewDispenser("", strings.NewReader(testTokens[i]))} + retrievedFunc, err := Browse(c) + 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 { + if config.PathScope != test.expectedPathScope[j] { + t.Errorf("Test case #%d expected a pathscope of %v, but got %v", i, test.expectedPathScope, config.PathScope) + } + } + } +} diff --git a/caddy/setup/redir_test.go b/caddy/setup/redir_test.go new file mode 100644 index 00000000..7aef7a7a --- /dev/null +++ b/caddy/setup/redir_test.go @@ -0,0 +1,70 @@ +package setup + +import ( + "testing" + + "github.com/mholt/caddy/middleware/redirect" +) + +func TestRedir(t *testing.T) { + + tests := []struct { + testToken string + shouldErr bool + expectedRules []redirect.Rule + }{ + // test case #0 tests the recognition of a valid HTTP status code defined outside of block statement + {"redir 300 {\n/ /foo\n}", false, []redirect.Rule{redirect.Rule{FromPath: "/", To: "/foo", Code: 300}}}, + + // test case #1 tests the recognition of an invalid HTTP status code defined outside of block statement + {"redir 9000 {\n/ /foo\n}", true, []redirect.Rule{redirect.Rule{}}}, + + // test case #2 tests the detection of a valid HTTP status code outside of a block statement being overriden by an invalid HTTP status code inside statement of a block statement + {"redir 300 {\n/ /foo 9000\n}", true, []redirect.Rule{redirect.Rule{}}}, + + // test case #3 tests the detection of an invalid HTTP status code outside of a block statement being overriden by a valid HTTP status code inside statement of a block statement + {"redir 9000 {\n/ /foo 300\n}", true, []redirect.Rule{redirect.Rule{}}}, + + // test case #4 tests the recognition of a TO redirection in a block statement.The HTTP status code is set to the default of 301 - MovedPermanently + {"redir 302 {\n/foo\n}", false, []redirect.Rule{redirect.Rule{FromPath: "/", To: "/foo", Code: 302}}}, + + // test case #5 tests the recognition of a TO and From redirection in a block statement + {"redir {\n/bar /foo 303\n}", false, []redirect.Rule{redirect.Rule{FromPath: "/bar", To: "/foo", Code: 303}}}, + + // test case #6 tests the recognition of a TO redirection in a non-block statement. The HTTP status code is set to the default of 301 - MovedPermanently + {"redir /foo", false, []redirect.Rule{redirect.Rule{FromPath: "/", To: "/foo", Code: 301}}}, + + // test case #7 tests the recognition of a TO and From redirection in a non-block statement + {"redir /bar /foo 303", false, []redirect.Rule{redirect.Rule{FromPath: "/bar", To: "/foo", Code: 303}}}, + + // test case #8 tests the recognition of multiple redirections + {"redir {\n / /foo 304 \n} \n redir {\n /bar /foobar 305 \n}", false, []redirect.Rule{redirect.Rule{FromPath: "/", To: "/foo", Code: 304}, redirect.Rule{FromPath: "/bar", To: "/foobar", Code: 305}}}, + + // 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) + 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 + + 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) + } + 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 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) + } + } + } + +} From 1017142d9b3d74975c7ae1f2707192be1e1b2fe7 Mon Sep 17 00:00:00 2001 From: Dipen Patel Date: Sat, 7 Nov 2015 22:17:26 -0500 Subject: [PATCH 2/3] Made style adjustments to browse and redir tests --- caddy/setup/browse_test.go | 35 ++++++++++++----------------------- caddy/setup/redir_test.go | 27 ++++++++++++--------------- 2 files changed, 24 insertions(+), 38 deletions(-) diff --git a/caddy/setup/browse_test.go b/caddy/setup/browse_test.go index 6e605997..b664aeb5 100644 --- a/caddy/setup/browse_test.go +++ b/caddy/setup/browse_test.go @@ -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) } diff --git a/caddy/setup/redir_test.go b/caddy/setup/redir_test.go index 7aef7a7a..773666f8 100644 --- a/caddy/setup/redir_test.go +++ b/caddy/setup/redir_test.go @@ -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) } } } From 485af2c6ba3efdf95318c1c8c64e2ccd763aa615 Mon Sep 17 00:00:00 2001 From: Dipen Patel Date: Sun, 8 Nov 2015 08:40:18 -0500 Subject: [PATCH 3/3] removed comment from browse test --- caddy/setup/browse_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/caddy/setup/browse_test.go b/caddy/setup/browse_test.go index b664aeb5..3714a51d 100644 --- a/caddy/setup/browse_test.go +++ b/caddy/setup/browse_test.go @@ -48,7 +48,6 @@ func TestBrowse(t *testing.T) { {"browse " + tempDirPath + "\n browse " + tempDirPath, nil, true}, } { - // 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)