Commit 0a04fa40 authored by Abiola Ibrahim's avatar Abiola Ibrahim

Oops. status code check should be after all validations.

parent 48d7f1ea
...@@ -145,11 +145,6 @@ func (r *ComplexRule) Rewrite(fs http.FileSystem, req *http.Request) (re Rewrite ...@@ -145,11 +145,6 @@ func (r *ComplexRule) Rewrite(fs http.FileSystem, req *http.Request) (re Rewrite
return return
} }
// if status is present, stop rewrite and return it.
if r.Status != 0 {
return RewriteStatus
}
// validate extensions // validate extensions
if !r.matchExt(rPath) { if !r.matchExt(rPath) {
return return
...@@ -183,6 +178,11 @@ func (r *ComplexRule) Rewrite(fs http.FileSystem, req *http.Request) (re Rewrite ...@@ -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 // attempt rewrite
return To(fs, req, r.To, replacer) return To(fs, req, r.To, replacer)
} }
......
...@@ -107,17 +107,27 @@ func TestRewrite(t *testing.T) { ...@@ -107,17 +107,27 @@ func TestRewrite(t *testing.T) {
} }
} }
statusTests := []int{ statusTests := []struct {
401, 405, 403, 400, 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 { for i, s := range statusTests {
urlPath := fmt.Sprintf("/status%d", i) 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 { if err != nil {
t.Fatalf("Test %d: No error expected for rule but found %v", i, err) 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) req, err := http.NewRequest("GET", urlPath, nil)
if err != nil { if err != nil {
t.Fatalf("Test %d: Could not create HTTP request: %v", i, err) t.Fatalf("Test %d: Could not create HTTP request: %v", i, err)
...@@ -128,11 +138,17 @@ func TestRewrite(t *testing.T) { ...@@ -128,11 +138,17 @@ func TestRewrite(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Test %d: No error expected for handler but found %v", i, err) t.Fatalf("Test %d: No error expected for handler but found %v", i, err)
} }
if s.statusExpected {
if rec.Body.String() != "" { if rec.Body.String() != "" {
t.Errorf("Test %d: Expected empty body but found %s", i, rec.Body.String()) t.Errorf("Test %d: Expected empty body but found %s", i, rec.Body.String())
} }
if code != s { if code != s.status {
t.Errorf("Text %d: Expected status code %d found %d", i, s, code) 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)
}
} }
} }
} }
......
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