1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-18 16:20:53 +02:00
echo/middleware/static_test.go
Vishal Rana 29fd5831ff Fixed test
Signed-off-by: Vishal Rana <vr@labstack.com>
2017-02-22 15:57:12 -08:00

67 lines
1.6 KiB
Go

package middleware
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/labstack/echo"
"github.com/stretchr/testify/assert"
)
func TestStatic(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(echo.GET, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
config := StaticConfig{
Root: "../_fixture",
}
// Directory
h := StaticWithConfig(config)(echo.NotFoundHandler)
if assert.NoError(t, h(c)) {
assert.Contains(t, rec.Body.String(), "Echo")
}
// File found
req = httptest.NewRequest(echo.GET, "/images/walle.png", nil)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
if assert.NoError(t, h(c)) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, rec.Header().Get(echo.HeaderContentLength), "219885")
}
// File not found
req = httptest.NewRequest(echo.GET, "/none", nil)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
he := h(c).(*echo.HTTPError)
assert.Equal(t, http.StatusNotFound, he.Code)
// HTML5
req = httptest.NewRequest(echo.GET, "/random", nil)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
config.HTML5 = true
static := StaticWithConfig(config)
h = static(echo.NotFoundHandler)
if assert.NoError(t, h(c)) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Contains(t, rec.Body.String(), "Echo")
}
// Browse
req = httptest.NewRequest(echo.GET, "/", nil)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
config.Browse = true
static = StaticWithConfig(config)
h = static(echo.NotFoundHandler)
if assert.NoError(t, h(c)) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Contains(t, rec.Body.String(), "images")
}
}