Oops. status code check should be after all validations.

This commit is contained in:
Abiola Ibrahim 2016-01-02 08:08:55 +01:00
parent 48d7f1ead2
commit 0a04fa40f4
2 changed files with 30 additions and 14 deletions

View file

@ -145,11 +145,6 @@ func (r *ComplexRule) Rewrite(fs http.FileSystem, req *http.Request) (re Rewrite
return
}
// if status is present, stop rewrite and return it.
if r.Status != 0 {
return RewriteStatus
}
// validate extensions
if !r.matchExt(rPath) {
return
@ -183,6 +178,11 @@ func (r *ComplexRule) Rewrite(fs http.FileSystem, req *http.Request) (re Rewrite
}
}
// if status is present, stop rewrite and return it.
if r.Status != 0 {
return RewriteStatus
}
// attempt rewrite
return To(fs, req, r.To, replacer)
}

View file

@ -107,17 +107,27 @@ func TestRewrite(t *testing.T) {
}
}
statusTests := []int{
401, 405, 403, 400,
statusTests := []struct {
status int
base string
to string
regexp string
statusExpected bool
}{
{400, "/status", "", "", true},
{400, "/ignore", "", "", false},
{400, "/", "", "^/ignore", false},
{400, "/", "", "(.*)", true},
{400, "/status", "", "", true},
}
for i, s := range statusTests {
urlPath := fmt.Sprintf("/status%d", i)
rule, err := NewComplexRule(urlPath, "", "", s, nil, nil)
rule, err := NewComplexRule(s.base, s.regexp, s.to, s.status, nil, nil)
if err != nil {
t.Fatalf("Test %d: No error expected for rule but found %v", i, err)
}
rw.Rules = append(rw.Rules, rule)
rw.Rules = []Rule{rule}
req, err := http.NewRequest("GET", urlPath, nil)
if err != nil {
t.Fatalf("Test %d: Could not create HTTP request: %v", i, err)
@ -128,11 +138,17 @@ func TestRewrite(t *testing.T) {
if err != nil {
t.Fatalf("Test %d: No error expected for handler but found %v", i, err)
}
if rec.Body.String() != "" {
t.Errorf("Test %d: Expected empty body but found %s", i, rec.Body.String())
}
if code != s {
t.Errorf("Text %d: Expected status code %d found %d", i, s, code)
if s.statusExpected {
if rec.Body.String() != "" {
t.Errorf("Test %d: Expected empty body but found %s", i, rec.Body.String())
}
if code != s.status {
t.Errorf("Test %d: Expected status code %d found %d", i, s.status, code)
}
} else {
if code != 0 {
t.Errorf("Test %d: Expected no status code found %d", i, code)
}
}
}
}