Commit 257952ff authored by Jacob Vosmaer's avatar Jacob Vosmaer

Rename 'handleDevelopmentMode' to 'NotFoundUnless'

parent 61e4d00c
package upstream
import "net/http"
func handleDevelopmentMode(developmentMode bool, handler http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if !developmentMode {
http.NotFound(w, r)
return
}
handler(w, r)
}
}
......@@ -14,9 +14,9 @@ func TestDevelopmentModeEnabled(t *testing.T) {
w := httptest.NewRecorder()
executed := false
handleDevelopmentMode(developmentMode, func(_ http.ResponseWriter, _ *http.Request) {
NotFoundUnless(developmentMode, http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
executed = true
})(w, r)
})).ServeHTTP(w, r)
if !executed {
t.Error("The handler should get executed")
}
......@@ -29,9 +29,9 @@ func TestDevelopmentModeDisabled(t *testing.T) {
w := httptest.NewRecorder()
executed := false
handleDevelopmentMode(developmentMode, func(_ http.ResponseWriter, _ *http.Request) {
NotFoundUnless(developmentMode, http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
executed = true
})(w, r)
})).ServeHTTP(w, r)
if executed {
t.Error("The handler should not get executed")
}
......
package upstream
import "net/http"
func NotFoundUnless(pass bool, handler http.Handler) http.Handler {
if pass {
return handler
} else {
return http.HandlerFunc(http.NotFound)
}
}
......@@ -64,7 +64,7 @@ func (u *Upstream) configureRoutes() {
// Serve assets
route{"", regexp.MustCompile(`^/assets/`),
handleServeFile(u.DocumentRoot, u.URLPrefix(), CacheExpireMax,
handleDevelopmentMode(u.DevelopmentMode,
NotFoundUnless(u.DevelopmentMode,
handleDeployPage(u.DocumentRoot,
errorpage.Inject(u.DocumentRoot,
u.Proxy(),
......
......@@ -17,8 +17,8 @@ const (
CacheExpireMax
)
func handleServeFile(documentRoot string, prefix urlPrefix, cache CacheMode, notFoundHandler http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
func handleServeFile(documentRoot string, prefix urlPrefix, cache CacheMode, notFoundHandler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
file := filepath.Join(documentRoot, prefix.strip(r.URL.Path))
// The filepath.Join does Clean traversing directories up
......@@ -49,7 +49,7 @@ func handleServeFile(documentRoot string, prefix urlPrefix, cache CacheMode, not
}
if err != nil {
if notFoundHandler != nil {
notFoundHandler(w, r)
notFoundHandler.ServeHTTP(w, r)
} else {
http.NotFound(w, r)
}
......@@ -67,5 +67,5 @@ func handleServeFile(documentRoot string, prefix urlPrefix, cache CacheMode, not
log.Printf("Send static file %q (%q) for %s %q", file, w.Header().Get("Content-Encoding"), r.Method, r.RequestURI)
http.ServeContent(w, r, filepath.Base(file), fi.ModTime(), content)
}
})
}
......@@ -17,7 +17,7 @@ func TestServingNonExistingFile(t *testing.T) {
httpRequest, _ := http.NewRequest("GET", "/file", nil)
w := httptest.NewRecorder()
handleServeFile(dir, "/", CacheDisabled, nil)(w, httpRequest)
handleServeFile(dir, "/", CacheDisabled, nil).ServeHTTP(w, httpRequest)
helper.AssertResponseCode(t, w, 404)
}
......@@ -30,7 +30,7 @@ func TestServingDirectory(t *testing.T) {
httpRequest, _ := http.NewRequest("GET", "/file", nil)
w := httptest.NewRecorder()
handleServeFile(dir, "/", CacheDisabled, nil)(w, httpRequest)
handleServeFile(dir, "/", CacheDisabled, nil).ServeHTTP(w, httpRequest)
helper.AssertResponseCode(t, w, 404)
}
......@@ -39,7 +39,7 @@ func TestServingMalformedUri(t *testing.T) {
httpRequest, _ := http.NewRequest("GET", "/../../../static/file", nil)
w := httptest.NewRecorder()
handleServeFile(dir, "/", CacheDisabled, nil)(w, httpRequest)
handleServeFile(dir, "/", CacheDisabled, nil).ServeHTTP(w, httpRequest)
helper.AssertResponseCode(t, w, 404)
}
......@@ -48,9 +48,9 @@ func TestExecutingHandlerWhenNoFileFound(t *testing.T) {
httpRequest, _ := http.NewRequest("GET", "/file", nil)
executed := false
handleServeFile(dir, "/", CacheDisabled, func(_ http.ResponseWriter, r *http.Request) {
handleServeFile(dir, "/", CacheDisabled, http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
executed = (r == httpRequest)
})(nil, httpRequest)
})).ServeHTTP(nil, httpRequest)
if !executed {
t.Error("The handler should get executed")
}
......@@ -69,7 +69,7 @@ func TestServingTheActualFile(t *testing.T) {
ioutil.WriteFile(filepath.Join(dir, "file"), []byte(fileContent), 0600)
w := httptest.NewRecorder()
handleServeFile(dir, "/", CacheDisabled, nil)(w, httpRequest)
handleServeFile(dir, "/", CacheDisabled, nil).ServeHTTP(w, httpRequest)
helper.AssertResponseCode(t, w, 200)
if w.Body.String() != fileContent {
t.Error("We should serve the file: ", w.Body.String())
......@@ -100,7 +100,7 @@ func testServingThePregzippedFile(t *testing.T, enableGzip bool) {
ioutil.WriteFile(filepath.Join(dir, "file"), []byte(fileContent), 0600)
w := httptest.NewRecorder()
handleServeFile(dir, "/", CacheDisabled, nil)(w, httpRequest)
handleServeFile(dir, "/", CacheDisabled, nil).ServeHTTP(w, httpRequest)
helper.AssertResponseCode(t, w, 200)
if enableGzip {
helper.AssertResponseHeader(t, w, "Content-Encoding", "gzip")
......
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