refactoring tests util methods + adding context tests

This commit is contained in:
Julien Neuhart
2019-07-24 14:56:12 +02:00
parent 2be9ae7868
commit 554474776c
23 changed files with 295 additions and 234 deletions

View File

@@ -1,21 +1,13 @@
package test
import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
"os"
"path"
"path/filepath"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/thecodingmachine/gotenberg/internal/pkg/xerror"
"golang.org/x/sync/errgroup"
)
@@ -51,94 +43,3 @@ func AssertConcurrent(t *testing.T, fn func() error, amount int) {
err := eg.Wait()
assert.NoError(t, err)
}
// AssertStandardError validates that given error
// is of an instance of xerror.Error.
// If so, returns the instance of xerror.Error.
func AssertStandardError(t *testing.T, err error) *xerror.Error {
assert.NotNil(t, err)
standardized, ok := err.(*xerror.Error)
assert.Equal(t, true, ok)
return standardized
}
// HTMLTestMultipartForm returns the body
// for a multipate/form-data request with all
// files under "html" folder.
func HTMLTestMultipartForm(t *testing.T, formValues map[string]string) (*bytes.Buffer, string) {
return multipartForm(t, "html", formValues)
}
// URLTestMultipartForm returns the body
// for a multipate/form-data request with all
// files under "url" folder.
func URLTestMultipartForm(t *testing.T, formValues map[string]string) (*bytes.Buffer, string) {
return multipartForm(t, "url", formValues)
}
// MarkdownTestMultipartForm returns the body
// for a multipate/form-data request with all
// files under "markdown" folder.
func MarkdownTestMultipartForm(t *testing.T, formValues map[string]string) (*bytes.Buffer, string) {
return multipartForm(t, "markdown", formValues)
}
// OfficeTestMultipartForm returns the body
// for a multipate/form-data request with all
// files under "office" folder.
func OfficeTestMultipartForm(t *testing.T, formValues map[string]string) (*bytes.Buffer, string) {
return multipartForm(t, "office", formValues)
}
// PDFTestMultipartForm returns the body
// for a multipate/form-data request with all
// files under "pdf" folder.
func PDFTestMultipartForm(t *testing.T, formValues map[string]string) (*bytes.Buffer, string) {
return multipartForm(t, "pdf", formValues)
}
func multipartForm(t *testing.T, kind string, formValues map[string]string) (*bytes.Buffer, string) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
defer writer.Close()
dirPath := abs(t, kind, "")
fpaths := make(map[string]string)
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, _ error) error {
if info.IsDir() {
return nil
}
fpaths[info.Name()] = abs(t, kind, info.Name())
return nil
})
require.Nil(t, err)
for filename, fpath := range fpaths {
file, err := os.Open(fpath)
require.Nil(t, err)
part, err := writer.CreateFormFile("foo", filename)
require.Nil(t, err)
_, err = io.Copy(part, file)
require.Nil(t, err)
}
if kind == "url" {
err := writer.WriteField("remoteURL", "http://google.com")
require.Nil(t, err)
}
for k, v := range formValues {
err := writer.WriteField(k, v)
require.Nil(t, err)
}
return body, writer.FormDataContentType()
}
func abs(t *testing.T, kind, filename string) string {
_, gofilename, _, ok := runtime.Caller(0)
require.Equal(t, ok, true, "got no caller information")
if filename == "" {
path, err := filepath.Abs(fmt.Sprintf("%s/testdata/%s", path.Dir(gofilename), kind))
require.Nil(t, err, `getting the absolute path of "%s"`, kind)
return path
}
path, err := filepath.Abs(fmt.Sprintf("%s/testdata/%s/%s", path.Dir(gofilename), kind, filename))
require.Nil(t, err, `getting the absolute path of "%s"`, filename)
return path
}