mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-15 03:42:15 +01:00
refactoring tests util methods + adding context tests
This commit is contained in:
@@ -2,11 +2,11 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/thecodingmachine/gotenberg/internal/pkg/pm2"
|
||||
"github.com/thecodingmachine/gotenberg/test/internalpkg/xlogtest"
|
||||
"github.com/thecodingmachine/gotenberg/test"
|
||||
)
|
||||
|
||||
func main() {
|
||||
logger := xlogtest.DebugLogger()
|
||||
logger := test.DebugLogger()
|
||||
process := pm2.NewChromeProcess(logger)
|
||||
if err := process.Start(); err != nil {
|
||||
panic(err)
|
||||
|
||||
29
test/context.go
Normal file
29
test/context.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// DummyEchoContext creates a
|
||||
// echo.Context without anything.
|
||||
func DummyEchoContext() echo.Context {
|
||||
e := echo.New()
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
return e.NewContext(req, rec)
|
||||
}
|
||||
|
||||
// EchoContextMultipart creates a
|
||||
// echo.Context with form files.
|
||||
func EchoContextMultipart(t *testing.T) echo.Context {
|
||||
e := echo.New()
|
||||
body, contentType := MergeMultipartForm(t, nil)
|
||||
req := httptest.NewRequest(http.MethodPost, "/", body)
|
||||
req.Header.Set(echo.HeaderContentType, contentType)
|
||||
rec := httptest.NewRecorder()
|
||||
return e.NewContext(req, rec)
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
/*
|
||||
Package printertest contains useful
|
||||
functions for tests related
|
||||
to printer package.
|
||||
*/
|
||||
package printertest
|
||||
@@ -1,6 +0,0 @@
|
||||
/*
|
||||
Package xerrortest contains useful
|
||||
functions for tests related
|
||||
to xerror package.
|
||||
*/
|
||||
package xerrortest
|
||||
@@ -1,6 +0,0 @@
|
||||
/*
|
||||
Package xlogtest contains useful
|
||||
functions for tests related
|
||||
to xlog package.
|
||||
*/
|
||||
package xlogtest
|
||||
80
test/multipartform.go
Normal file
80
test/multipartform.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// MergeMultipartForm returns the body
|
||||
// for a multipart/form-data request with all
|
||||
// files under "pdf" folder.
|
||||
func MergeMultipartForm(t *testing.T, formValues map[string]string) (*bytes.Buffer, string) {
|
||||
fpaths := MergeFpaths(t)
|
||||
return multipartForm(t, "pdf", formValues, fpaths)
|
||||
}
|
||||
|
||||
// HTMLMultipartForm returns the body
|
||||
// for a multipart/form-data request with all
|
||||
// files under "html" folder.
|
||||
func HTMLMultipartForm(t *testing.T, formValues map[string]string) (*bytes.Buffer, string) {
|
||||
// TODO
|
||||
return multipartForm(t, "html", formValues, []string{})
|
||||
}
|
||||
|
||||
// URLMultipartForm returns the body
|
||||
// for a multipart/form-data request with all
|
||||
// files under "url" folder.
|
||||
func URLMultipartForm(t *testing.T, formValues map[string]string) (*bytes.Buffer, string) {
|
||||
// TODO
|
||||
return multipartForm(t, "url", formValues, []string{})
|
||||
}
|
||||
|
||||
// MarkdownMultipartForm returns the body
|
||||
// for a multipart/form-data request with all
|
||||
// files under "markdown" folder.
|
||||
func MarkdownMultipartForm(t *testing.T, formValues map[string]string) (*bytes.Buffer, string) {
|
||||
// TODO
|
||||
return multipartForm(t, "markdown", formValues, []string{})
|
||||
}
|
||||
|
||||
// OfficeMultipartForm returns the body
|
||||
// for a multipart/form-data request with all
|
||||
// files under "office" folder.
|
||||
func OfficeMultipartForm(t *testing.T, formValues map[string]string) (*bytes.Buffer, string) {
|
||||
fpaths := OfficeFpaths(t)
|
||||
return multipartForm(t, "office", formValues, fpaths)
|
||||
}
|
||||
|
||||
func multipartForm(
|
||||
t *testing.T,
|
||||
kind string,
|
||||
formValues map[string]string,
|
||||
formFilePaths []string,
|
||||
) (*bytes.Buffer, string) {
|
||||
body := &bytes.Buffer{}
|
||||
writer := multipart.NewWriter(body)
|
||||
defer writer.Close()
|
||||
for _, fpath := range formFilePaths {
|
||||
file, err := os.Open(fpath)
|
||||
require.Nil(t, err)
|
||||
part, err := writer.CreateFormFile("foo", filepath.Base(fpath))
|
||||
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()
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package printertest
|
||||
package test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package xerrortest
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
@@ -1,4 +1,4 @@
|
||||
package xlogtest
|
||||
package test
|
||||
|
||||
import (
|
||||
"github.com/thecodingmachine/gotenberg/internal/pkg/xlog"
|
||||
Reference in New Issue
Block a user