1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-18 16:20:53 +02:00
echo/middleware/static_test.go
Alex Ungur 6d9e043284 Introduced Go module support as v4, removed obsolete CloseNotifier() mechanism
This reintroduces support for Go modules, as v4.

CloseNotifier() is removed as it has been obsoleted, see https://golang.org/doc/go1.11#net/http

It was already NOT working (not sending signals) as of 1.11 the functionality was gone, we merely
deleted the functions that exposed it. If anyone still relies on it they should migrate to using
`c.Request().Context().Done()` instead.

Closes #1268, #1255
2019-01-30 16:11:42 +02:00

71 lines
1.7 KiB
Go

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