2022-01-08 22:41:34 +02:00
|
|
|
package echo
|
|
|
|
|
|
|
|
import (
|
2022-01-15 11:04:07 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-01-08 22:41:34 +02:00
|
|
|
"io/fs"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestContext_File(t *testing.T) {
|
|
|
|
var testCases = []struct {
|
|
|
|
name string
|
|
|
|
whenFile string
|
|
|
|
whenFS fs.FS
|
|
|
|
expectStatus int
|
|
|
|
expectStartsWith []byte
|
|
|
|
expectError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "ok, from default file system",
|
|
|
|
whenFile: "_fixture/images/walle.png",
|
|
|
|
whenFS: nil,
|
|
|
|
expectStatus: http.StatusOK,
|
|
|
|
expectStartsWith: []byte{0x89, 0x50, 0x4e},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ok, from custom file system",
|
|
|
|
whenFile: "walle.png",
|
|
|
|
whenFS: os.DirFS("_fixture/images"),
|
|
|
|
expectStatus: http.StatusOK,
|
|
|
|
expectStartsWith: []byte{0x89, 0x50, 0x4e},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nok, not existent file",
|
|
|
|
whenFile: "not.png",
|
|
|
|
whenFS: os.DirFS("_fixture/images"),
|
|
|
|
expectStatus: http.StatusOK,
|
|
|
|
expectStartsWith: nil,
|
|
|
|
expectError: "code=404, message=Not Found",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
if tc.whenFS != nil {
|
|
|
|
e.Filesystem = tc.whenFS
|
|
|
|
}
|
|
|
|
|
|
|
|
handler := func(ec Context) error {
|
|
|
|
return ec.(*context).File(tc.whenFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/match.png", nil)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := e.NewContext(req, rec)
|
|
|
|
|
|
|
|
err := handler(c)
|
|
|
|
|
2022-01-15 11:04:07 +02:00
|
|
|
assert.Equal(t, tc.expectStatus, rec.Code)
|
2022-01-08 22:41:34 +02:00
|
|
|
if tc.expectError != "" {
|
2022-01-15 11:04:07 +02:00
|
|
|
assert.EqualError(t, err, tc.expectError)
|
2022-01-08 22:41:34 +02:00
|
|
|
} else {
|
2022-01-15 11:04:07 +02:00
|
|
|
assert.NoError(t, err)
|
2022-01-08 22:41:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
body := rec.Body.Bytes()
|
|
|
|
if len(body) > len(tc.expectStartsWith) {
|
|
|
|
body = body[:len(tc.expectStartsWith)]
|
|
|
|
}
|
2022-01-15 11:04:07 +02:00
|
|
|
assert.Equal(t, tc.expectStartsWith, body)
|
2022-01-08 22:41:34 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_FileFS(t *testing.T) {
|
|
|
|
var testCases = []struct {
|
|
|
|
name string
|
|
|
|
whenFile string
|
|
|
|
whenFS fs.FS
|
|
|
|
expectStatus int
|
|
|
|
expectStartsWith []byte
|
|
|
|
expectError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "ok",
|
|
|
|
whenFile: "walle.png",
|
|
|
|
whenFS: os.DirFS("_fixture/images"),
|
|
|
|
expectStatus: http.StatusOK,
|
|
|
|
expectStartsWith: []byte{0x89, 0x50, 0x4e},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nok, not existent file",
|
|
|
|
whenFile: "not.png",
|
|
|
|
whenFS: os.DirFS("_fixture/images"),
|
|
|
|
expectStatus: http.StatusOK,
|
|
|
|
expectStartsWith: nil,
|
|
|
|
expectError: "code=404, message=Not Found",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
|
|
|
|
handler := func(ec Context) error {
|
|
|
|
return ec.(*context).FileFS(tc.whenFile, tc.whenFS)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/match.png", nil)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := e.NewContext(req, rec)
|
|
|
|
|
|
|
|
err := handler(c)
|
|
|
|
|
2022-01-15 11:04:07 +02:00
|
|
|
assert.Equal(t, tc.expectStatus, rec.Code)
|
2022-01-08 22:41:34 +02:00
|
|
|
if tc.expectError != "" {
|
2022-01-15 11:04:07 +02:00
|
|
|
assert.EqualError(t, err, tc.expectError)
|
2022-01-08 22:41:34 +02:00
|
|
|
} else {
|
2022-01-15 11:04:07 +02:00
|
|
|
assert.NoError(t, err)
|
2022-01-08 22:41:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
body := rec.Body.Bytes()
|
|
|
|
if len(body) > len(tc.expectStartsWith) {
|
|
|
|
body = body[:len(tc.expectStartsWith)]
|
|
|
|
}
|
2022-01-15 11:04:07 +02:00
|
|
|
assert.Equal(t, tc.expectStartsWith, body)
|
2022-01-08 22:41:34 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|