golang-lib/web/file/fs_test.go

112 lines
2.3 KiB
Go
Raw Normal View History

2021-07-26 17:48:20 +02:00
package file_test
import (
2021-07-29 17:17:37 +02:00
"errors"
2021-07-26 17:48:20 +02:00
"fmt"
"io"
2021-07-29 17:17:37 +02:00
"io/fs"
"mime"
"mime/multipart"
2021-07-26 17:48:20 +02:00
"net/http"
2021-07-29 17:17:37 +02:00
"net/http/httptest"
2021-07-26 17:48:20 +02:00
"os"
2021-07-29 17:17:37 +02:00
"testing"
2021-07-26 17:48:20 +02:00
"github.com/google/uuid"
2021-07-29 17:17:37 +02:00
"github.com/stretchr/testify/assert"
2021-07-26 17:48:20 +02:00
2021-07-30 14:09:58 +02:00
"dev.sum7.eu/genofire/golang-lib/web/file"
2021-07-26 17:48:20 +02:00
)
2021-07-29 17:17:37 +02:00
type TestFS struct {
assert *assert.Assertions
filename string
data string
contentType string
}
func (f TestFS) Store(id uuid.UUID, name, contentType string, data io.Reader) error {
f.assert.Equal(f.filename, name)
dat, err := io.ReadAll(data)
f.assert.NoError(err)
f.assert.Equal(f.data, string(dat))
contentType, _, err = mime.ParseMediaType(contentType)
f.assert.NoError(err)
f.assert.Equal(f.contentType, contentType)
return nil
}
func (f TestFS) RemoveUUID(id uuid.UUID) error {
return errors.New("TestFS.RemoveUUID called")
}
func (f TestFS) Open(name string) (fs.File, error) {
return nil, errors.New("TestFS.Open called")
}
func (f TestFS) OpenUUID(uuid.UUID) (fs.File, error) {
return nil, errors.New("TestFS.OpenUUID called")
}
func (f TestFS) Check() error { return nil }
func TestStoreFromHTTP(t *testing.T) {
assert := assert.New(t)
testfs := TestFS{
assert: assert,
filename: "cute-cat.png",
data: "content\nof file",
contentType: "text/plain",
}
r, w := io.Pipe()
m := multipart.NewWriter(w)
rq := httptest.NewRequest("PUT", "/", r)
rq.Header.Set("Content-Type", m.FormDataContentType())
go func() {
f, err := m.CreateFormFile("file", testfs.filename)
assert.NoError(err)
_, err = f.Write([]byte(testfs.data))
assert.NoError(err)
m.Close()
}()
assert.NoError(file.StoreFromHTTP(testfs, rq, "file"))
}
var fstore file.FS
2021-07-26 17:48:20 +02:00
func ExampleFS() {
// generate the UUID for the new file
id := uuid.New()
// store a file
{
f, _ := os.Open("glenda.png")
2021-07-29 17:17:37 +02:00
fstore.Store(id, "glenda.png", "image/png", f)
2021-07-26 17:48:20 +02:00
f.Close()
}
// copy back to a local file
{
2021-07-29 17:17:37 +02:00
r, _ := fstore.OpenUUID(id)
2021-07-26 17:48:20 +02:00
w, _ := os.Create("glenda.png")
io.Copy(w, r)
r.Close()
w.Close()
}
}
func ExampleStoreFromHTTP() {
http.HandleFunc("/upload", func(w http.ResponseWriter, r *http.Request) {
2021-07-29 17:17:37 +02:00
if err := file.StoreFromHTTP(fstore, r, "file"); err != nil {
2021-07-26 17:48:20 +02:00
w.Header().Set("content-type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf(`{"message":"%v"}`, err)))
} else {
w.WriteHeader(http.StatusOK)
}
})
}