gosimplenpm/internal/handler/testutils_test.go

232 lines
4.4 KiB
Go

package handler
import (
"bytes"
"encoding/base64"
"fmt"
"gosimplenpm/internal/config"
"gosimplenpm/internal/storage"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"github.com/sirupsen/logrus"
)
func newTestApp(t *testing.T, cfg config.Config) *Application {
return &Application{
Conf: cfg,
Logger: &logrus.Logger{
Out: io.Discard,
// Level: logrus.DebugLevel,
// Formatter: &logrus.TextFormatter{
// FullTimestamp: true,
// TimestampFormat: "2009-01-02 15:15:15",
// },
},
FSStorage: &storage.FSStorage{},
}
}
type testServer struct {
*httptest.Server
}
func newTestServer(t *testing.T, h http.Handler) *testServer {
ts := httptest.NewServer(h)
return &testServer{ts}
}
func (ts *testServer) get(t *testing.T, urlPath string) (int, http.Header, []byte) {
rs, err := ts.Client().Get(ts.URL + urlPath)
if err != nil {
t.Fatal(err)
}
defer rs.Body.Close()
body, err := io.ReadAll(rs.Body)
if err != nil {
t.Fatal(err)
}
body = bytes.TrimSpace(body)
return rs.StatusCode, rs.Header, body
}
func (ts *testServer) put(t *testing.T, urlPath string, token string, data string) (int, http.Header, []byte) {
req, err := http.NewRequest(http.MethodPut, ts.URL+urlPath, strings.NewReader(data))
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
if err != nil {
t.Fatal(err)
}
rs, err := ts.Client().Do(req)
if err != nil {
t.Fatal(err)
}
defer rs.Body.Close()
body, err := io.ReadAll(rs.Body)
if err != nil {
t.Fatal(err)
}
body = bytes.TrimSpace(body)
return rs.StatusCode, rs.Header, body
}
func (ts *testServer) delete(t *testing.T, urlPath string, token string) (int, http.Header, []byte) {
req, err := http.NewRequest(http.MethodDelete, ts.URL+urlPath, nil)
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
if err != nil {
t.Fatal(err)
}
rs, err := ts.Client().Do(req)
if err != nil {
t.Fatal(err)
}
defer rs.Body.Close()
body, err := io.ReadAll(rs.Body)
if err != nil {
t.Fatal(err)
}
body = bytes.TrimSpace(body)
return rs.StatusCode, rs.Header, body
}
func cpFile(t *testing.T, src string, dst string) {
var srcfd *os.File
var dstfd *os.File
var err error
var srcinfo os.FileInfo
srcfd, err = os.Open(src)
if err != nil {
t.Fatal(err)
}
defer srcfd.Close()
dstfd, err = os.Create(dst)
if err != nil {
t.Fatal(err)
}
defer dstfd.Close()
_, err = io.Copy(dstfd, srcfd)
if err != nil {
t.Fatal(err)
}
srcinfo, err = os.Stat(src)
if err != nil {
t.Fatal(err)
}
err = os.Chmod(dst, srcinfo.Mode())
if err != nil {
t.Fatal(err)
}
}
// cpFolders - Copy folders recursively
// func cpFolders(t *testing.T, src string, dst string) {
// var err error
// var fds []fs.DirEntry
// var srcinfo os.FileInfo
// srcinfo, err = os.Stat(src)
// if err != nil {
// t.Fatal(err)
// }
// err = os.MkdirAll(dst, srcinfo.Mode())
// if err != nil {
// t.Fatal(err)
// }
// fds, err = os.ReadDir(src)
// if err != nil {
// t.Fatal(err)
// }
// for _, fd := range fds {
// srcfp := path.Join(src, fd.Name())
// dstfp := path.Join(dst, fd.Name())
// if fd.IsDir() {
// cpFolders(t, srcfp, dstfp)
// } else {
// cpFile(t, srcfp, dstfp)
// }
// }
// }
func mkDir(t *testing.T, fp string) {
err := os.MkdirAll(fp, os.ModePerm)
if err != nil {
t.Fatal(err)
}
}
func readTestFile(t *testing.T, fp string) []byte {
f, err := os.ReadFile(fp)
if err != nil {
t.Fatal(err)
}
return f
}
func readTestFileAsBase64(t *testing.T, fp string) string {
f, err := os.ReadFile(fp)
if err != nil {
t.Fatal(err)
}
return base64.StdEncoding.EncodeToString(f)
}
func listDir(t *testing.T, fp string, list bool) []string {
var filePaths []string
err := filepath.Walk(fp, func(path string, info os.FileInfo, err error) error {
if list {
t.Logf("File (Directory: %t)=> %s", info.IsDir(), path)
} else {
if !info.IsDir() {
filePaths = append(filePaths, path)
}
}
return nil
})
if err != nil {
t.Fatal(err)
}
return filePaths
}
func IsDirEmpty(t *testing.T, name string) bool {
f, err := os.Open(name)
if err != nil {
t.Fatal(err)
}
defer f.Close()
// read in ONLY one file
_, err = f.Readdir(1)
// and if the file is EOF... well, the dir is empty.
return err == io.EOF
}
func containsSub(s []string, str string) bool {
for _, v := range s {
if strings.Contains(v, str) {
return true
}
}
return false
}