Skip to content

Commit

Permalink
Static : add support for HEAD requests (also avoid useless filesystem…
Browse files Browse the repository at this point in the history
… call for non HEAD/GET requests)
  • Loading branch information
vanackere committed Jan 7, 2014
1 parent 50f2d3f commit 94b4285
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion static.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ import (
func Static(directory string) Handler {
dir := http.Dir(directory)
return func(res http.ResponseWriter, req *http.Request, log *log.Logger) {
if req.Method != "GET" && req.Method != "HEAD" {
return
}
file := req.URL.Path
f, err := dir.Open(file)
if err != nil || req.Method != "GET" {
if err != nil {
// discard the error?
return
}
Expand Down
16 changes: 16 additions & 0 deletions static_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ func Test_Static(t *testing.T) {
expect(t, response.Code, http.StatusOK)
}

func Test_Static_Head(t *testing.T) {
response := httptest.NewRecorder()

m := New()

m.Use(Static("."))

req, err := http.NewRequest("HEAD", "http://localhost:3000/martini.go", nil)
if err != nil {
t.Error(err)
}

m.ServeHTTP(response, req)
expect(t, response.Code, http.StatusOK)
}

func Test_Static_As_Post(t *testing.T) {
response := httptest.NewRecorder()

Expand Down

0 comments on commit 94b4285

Please sign in to comment.