1package negroni 2 3import ( 4 "bytes" 5 "net/http" 6 "net/http/httptest" 7 "testing" 8) 9 10func TestStatic(t *testing.T) { 11 response := httptest.NewRecorder() 12 response.Body = new(bytes.Buffer) 13 14 n := New() 15 n.Use(NewStatic(http.Dir("."))) 16 17 req, err := http.NewRequest("GET", "http://localhost:3000/negroni.go", nil) 18 if err != nil { 19 t.Error(err) 20 } 21 n.ServeHTTP(response, req) 22 expect(t, response.Code, http.StatusOK) 23 expect(t, response.Header().Get("Expires"), "") 24 if response.Body.Len() == 0 { 25 t.Errorf("Got empty body for GET request") 26 } 27} 28 29func TestStaticHead(t *testing.T) { 30 response := httptest.NewRecorder() 31 response.Body = new(bytes.Buffer) 32 33 n := New() 34 n.Use(NewStatic(http.Dir("."))) 35 n.UseHandler(http.NotFoundHandler()) 36 37 req, err := http.NewRequest("HEAD", "http://localhost:3000/negroni.go", nil) 38 if err != nil { 39 t.Error(err) 40 } 41 42 n.ServeHTTP(response, req) 43 expect(t, response.Code, http.StatusOK) 44 if response.Body.Len() != 0 { 45 t.Errorf("Got non-empty body for HEAD request") 46 } 47} 48 49func TestStaticAsPost(t *testing.T) { 50 response := httptest.NewRecorder() 51 52 n := New() 53 n.Use(NewStatic(http.Dir("."))) 54 n.UseHandler(http.NotFoundHandler()) 55 56 req, err := http.NewRequest("POST", "http://localhost:3000/negroni.go", nil) 57 if err != nil { 58 t.Error(err) 59 } 60 61 n.ServeHTTP(response, req) 62 expect(t, response.Code, http.StatusNotFound) 63} 64 65func TestStaticBadDir(t *testing.T) { 66 response := httptest.NewRecorder() 67 68 n := Classic() 69 n.UseHandler(http.NotFoundHandler()) 70 71 req, err := http.NewRequest("GET", "http://localhost:3000/negroni.go", nil) 72 if err != nil { 73 t.Error(err) 74 } 75 76 n.ServeHTTP(response, req) 77 refute(t, response.Code, http.StatusOK) 78} 79 80func TestStaticOptionsServeIndex(t *testing.T) { 81 response := httptest.NewRecorder() 82 83 n := New() 84 s := NewStatic(http.Dir(".")) 85 s.IndexFile = "negroni.go" 86 n.Use(s) 87 88 req, err := http.NewRequest("GET", "http://localhost:3000/", nil) 89 if err != nil { 90 t.Error(err) 91 } 92 93 n.ServeHTTP(response, req) 94 expect(t, response.Code, http.StatusOK) 95} 96 97func TestStaticOptionsPrefix(t *testing.T) { 98 response := httptest.NewRecorder() 99 100 n := New() 101 s := NewStatic(http.Dir(".")) 102 s.Prefix = "/public" 103 n.Use(s) 104 105 // Check file content behaviour 106 req, err := http.NewRequest("GET", "http://localhost:3000/public/negroni.go", nil) 107 if err != nil { 108 t.Error(err) 109 } 110 111 n.ServeHTTP(response, req) 112 expect(t, response.Code, http.StatusOK) 113} 114