Skip to content

Commit

Permalink
Static : fix and improve tests.
Browse files Browse the repository at this point in the history
- The tests were previously always passing (response.Code is initialised to http.StatusOK, without a router it won't be modified even if the file does not exists).
- Also check response.Body for HEAD and GET requests.
  • Loading branch information
vanackere committed Jan 7, 2014
1 parent 94b4285 commit 3185ecd
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion static_test.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,42 @@
package martini

import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
)

func Test_Static(t *testing.T) {
response := httptest.NewRecorder()
response.Body = new(bytes.Buffer)

m := New()
r := NewRouter()

m.Use(Static("."))
m.Action(r.Handle)

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

m.ServeHTTP(response, req)
expect(t, response.Code, http.StatusOK)
if response.Body.Len() == 0 {
t.Errorf("Got empty body for GET request")
}
}

func Test_Static_Head(t *testing.T) {
response := httptest.NewRecorder()
response.Body = new(bytes.Buffer)

m := New()
r := NewRouter()

m.Use(Static("."))
m.Action(r.Handle)

req, err := http.NewRequest("HEAD", "http://localhost:3000/martini.go", nil)
if err != nil {
Expand All @@ -36,6 +45,9 @@ func Test_Static_Head(t *testing.T) {

m.ServeHTTP(response, req)
expect(t, response.Code, http.StatusOK)
if response.Body.Len() != 0 {
t.Errorf("Got non-empty body for HEAD request")
}
}

func Test_Static_As_Post(t *testing.T) {
Expand Down

0 comments on commit 3185ecd

Please sign in to comment.