2022-07-06 23:19:05 +02:00
|
|
|
package rest_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"mime/multipart"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2022-10-30 10:28:14 +02:00
|
|
|
"regexp"
|
2022-07-06 23:19:05 +02:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pocketbase/pocketbase/tools/rest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFindUploadedFiles(t *testing.T) {
|
2022-10-30 10:28:14 +02:00
|
|
|
scenarios := []struct {
|
|
|
|
filename string
|
|
|
|
expectedPattern string
|
|
|
|
}{
|
|
|
|
{"ab.png", `^ab_\w{10}_\w{10}\.png$`},
|
|
|
|
{"test", `^test_\w{10}\.txt$`},
|
|
|
|
{"a b c d!@$.j!@$pg", `^a_b_c_d_\w{10}\.jpg$`},
|
|
|
|
{strings.Repeat("a", 150), `^a{100}_\w{10}\.txt$`},
|
2022-07-06 23:19:05 +02:00
|
|
|
}
|
|
|
|
|
2022-10-30 10:28:14 +02:00
|
|
|
for i, s := range scenarios {
|
|
|
|
// create multipart form file body
|
|
|
|
body := new(bytes.Buffer)
|
|
|
|
mp := multipart.NewWriter(body)
|
|
|
|
w, err := mp.CreateFormFile("test", s.filename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
w.Write([]byte("test"))
|
|
|
|
mp.Close()
|
|
|
|
// ---
|
2022-07-06 23:19:05 +02:00
|
|
|
|
2022-10-30 10:28:14 +02:00
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", body)
|
|
|
|
req.Header.Add("Content-Type", mp.FormDataContentType())
|
2022-07-06 23:19:05 +02:00
|
|
|
|
2022-10-30 10:28:14 +02:00
|
|
|
result, err := rest.FindUploadedFiles(req, "test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-07-06 23:19:05 +02:00
|
|
|
|
2022-10-30 10:28:14 +02:00
|
|
|
if len(result) != 1 {
|
|
|
|
t.Errorf("[%d] Expected 1 file, got %d", i, len(result))
|
|
|
|
}
|
2022-08-18 19:44:29 +02:00
|
|
|
|
2022-10-30 10:28:14 +02:00
|
|
|
if result[0].Header().Size != 4 {
|
|
|
|
t.Errorf("[%d] Expected the file size to be 4 bytes, got %d", i, result[0].Header().Size)
|
|
|
|
}
|
2022-07-06 23:19:05 +02:00
|
|
|
|
2022-10-30 10:28:14 +02:00
|
|
|
pattern, err := regexp.Compile(s.expectedPattern)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] Invalid filename pattern %q: %v", i, s.expectedPattern, err)
|
|
|
|
}
|
|
|
|
if !pattern.MatchString(result[0].Name()) {
|
|
|
|
t.Fatalf("Expected filename to match %s, got filename %s", s.expectedPattern, result[0].Name())
|
|
|
|
}
|
2022-07-06 23:19:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFindUploadedFilesMissing(t *testing.T) {
|
|
|
|
body := new(bytes.Buffer)
|
|
|
|
mp := multipart.NewWriter(body)
|
|
|
|
mp.Close()
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", body)
|
|
|
|
req.Header.Add("Content-Type", mp.FormDataContentType())
|
|
|
|
|
|
|
|
result, err := rest.FindUploadedFiles(req, "test")
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Expected error, got nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if result != nil {
|
|
|
|
t.Errorf("Expected result to be nil, got %v", result)
|
|
|
|
}
|
|
|
|
}
|