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