This commit is contained in:
Julien Neuhart
2018-12-10 16:09:19 +01:00
committed by GitHub
parent c1f6100382
commit 495203c112
473 changed files with 4631 additions and 174161 deletions

BIN
test/testdata/html/font.woff vendored Executable file

Binary file not shown.

15
test/testdata/html/footer.html vendored Normal file
View File

@@ -0,0 +1,15 @@
<html>
<head>
<style>
body {
font-size: 8rem;
margin: 4rem auto;
}
</style>
</head>
<body>
<p>
<span class="pageNumber"></span> of <span class="totalPages"></span>
</p>
</body>
</html>

13
test/testdata/html/header.html vendored Normal file
View File

@@ -0,0 +1,13 @@
<html>
<head>
<style>
body {
font-size: 8rem;
margin: 4rem auto;
}
</style>
</head>
<body>
<span class="title"></span>
</body>
</html>

BIN
test/testdata/html/img.gif vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

38
test/testdata/html/index.html vendored Normal file
View File

@@ -0,0 +1,38 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="style.css">
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
<title>Gutenberg</title>
</head>
<body>
<div class="page-break-after">
<div class="center">
<h1>Gutenberg</h1>
<img src="img.gif">
</div>
<blockquote cite="https://sites.google.com/site/johanngutenbergper5/q">
<p>It is a press, certainly, but a press from which shall flow in inexhaustible streams...Through it, God will spread His Word. A spring of truth shall flow from it: like a new star it shall scatter the darkness of ignorance, and cause a light heretofore unknown to shine amongst men.</p>
<footer><a href="https://sites.google.com/site/johanngutenbergper5/q">Johannes Gutenberg</a></cite></footer>
</blockquote>
</div>
<div class="page-break-after">
<h2>This paragraph use the default font</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<h2>This paragraph use a Google font</h2>
<p class="google-font">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<h2>This paragraph use a local font</h2>
<p class="local-font">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<div class="center">
<h1>This image is loaded from a URL</h1>
<img src="https://gutendev.com/wp-content/uploads/2018/10/01_03-1.jpg">
</div>
</body>
</html>

28
test/testdata/html/style.css vendored Normal file
View File

@@ -0,0 +1,28 @@
body {
font-family: Arial, Helvetica, sans-serif;
}
.center {
text-align: center;
}
.google-font {
font-family: 'Montserrat', sans-serif;
}
@font-face {
font-family: 'Local';
src: url('font.woff') format('woff');
font-weight: normal;
font-style: normal;
}
.local-font {
font-family: 'Local'
}
@media print {
.page-break-after {
page-break-after: always;
}
}

BIN
test/testdata/markdown/font.woff vendored Executable file

Binary file not shown.

15
test/testdata/markdown/footer.html vendored Normal file
View File

@@ -0,0 +1,15 @@
<html>
<head>
<style>
body {
font-size: 8rem;
margin: 4rem auto;
}
</style>
</head>
<body>
<p>
<span class="pageNumber"></span> of <span class="totalPages"></span>
</p>
</body>
</html>

13
test/testdata/markdown/header.html vendored Normal file
View File

@@ -0,0 +1,13 @@
<html>
<head>
<style>
body {
font-size: 8rem;
margin: 4rem auto;
}
</style>
</head>
<body>
<span class="title"></span>
</body>
</html>

BIN
test/testdata/markdown/img.gif vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

34
test/testdata/markdown/index.html vendored Normal file
View File

@@ -0,0 +1,34 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="style.css">
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
<title>Gutenberg</title>
</head>
<body>
<div class="page-break-after">
<div class="center">
<h1>Gutenberg</h1>
<img src="img.gif">
</div>
<blockquote cite="https://sites.google.com/site/johanngutenbergper5/q">
<p>It is a press, certainly, but a press from which shall flow in inexhaustible streams...Through it, God will spread His Word. A spring of truth shall flow from it: like a new star it shall scatter the darkness of ignorance, and cause a light heretofore unknown to shine amongst men.</p>
<footer><a href="https://sites.google.com/site/johanngutenbergper5/q">Johannes Gutenberg</a></cite></footer>
</blockquote>
</div>
<div class="page-break-after">
{{ toHTML .DirPath "paragraph1.md" }}
<div class="google-font">
{{ toHTML .DirPath "paragraph2.md" }}
</div>
<div class="local-font">
{{ toHTML .DirPath "paragraph3.md" }}
</div>
</div>
</body>
</html>

3
test/testdata/markdown/paragraph1.md vendored Normal file
View File

@@ -0,0 +1,3 @@
## This paragraph use the default font and has been generated from a markdown file
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

3
test/testdata/markdown/paragraph2.md vendored Normal file
View File

@@ -0,0 +1,3 @@
## This paragraph use a Google font and has been generated from a markdown file
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

3
test/testdata/markdown/paragraph3.md vendored Normal file
View File

@@ -0,0 +1,3 @@
## This paragraph use a local font and has been generated from a markdown file
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

28
test/testdata/markdown/style.css vendored Normal file
View File

@@ -0,0 +1,28 @@
body {
font-family: Arial, Helvetica, sans-serif;
}
.center {
text-align: center;
}
.google-font {
font-family: 'Montserrat', sans-serif;
}
@font-face {
font-family: 'Local';
src: url('font.woff') format('woff');
font-weight: normal;
font-style: normal;
}
.local-font {
font-family: 'Local'
}
@media print {
.page-break-after {
page-break-after: always;
}
}

BIN
test/testdata/office/document.docx vendored Normal file

Binary file not shown.

BIN
test/testdata/pdf/gotenberg.pdf vendored Normal file

Binary file not shown.

159
test/testfunc.go Normal file
View File

@@ -0,0 +1,159 @@
// Package test contains useful functions used across tests.
package test
import (
"bytes"
"fmt"
"io"
"mime/multipart"
"os"
"path"
"path/filepath"
"runtime"
"testing"
"github.com/stretchr/testify/require"
"github.com/thecodingmachine/gotenberg/internal/pkg/rand"
)
// HTMLMultipartForm returns the body
// for a multipate/form-data request with all
// files under "html" folder.
func HTMLMultipartForm(t *testing.T) (*bytes.Buffer, string) {
return multipartForm(t, "html")
}
// MarkdownMultipartForm returns the body
// for a multipate/form-data request with all
// files under "markdown" folder.
func MarkdownMultipartForm(t *testing.T) (*bytes.Buffer, string) {
return multipartForm(t, "markdown")
}
// OfficeMultipartForm returns the body
// for a multipate/form-data request with all
// files under "office" folder.
func OfficeMultipartForm(t *testing.T) (*bytes.Buffer, string) {
return multipartForm(t, "office")
}
// PDFMultipartForm returns the body
// for a multipate/form-data request with all
// files under "pdf" folder.
func PDFMultipartForm(t *testing.T) (*bytes.Buffer, string) {
return multipartForm(t, "pdf")
}
func multipartForm(t *testing.T, kind string) (*bytes.Buffer, string) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
defer writer.Close()
dirPath := abs(t, kind, "")
fpaths := make(map[string]string)
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
})
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)
}
return body, writer.FormDataContentType()
}
// HTMLTestDirPath creates a copy
// of "html" folder in test/testdata.
func HTMLTestDirPath(t *testing.T) string {
return copyDir(t, "html")
}
// MarkdownTestDirPath creates a copy
// of "markdown" folder in test/testdata.
func MarkdownTestDirPath(t *testing.T) string {
return copyDir(t, "markdown")
}
// OfficeTestDirPath creates a copy
// of "office" folder in test/testdata.
func OfficeTestDirPath(t *testing.T) string {
return copyDir(t, "office")
}
// PDFTestDirPath creates a copy
// of "pdf" folder in test/testdata.
func PDFTestDirPath(t *testing.T) string {
return copyDir(t, "pdf")
}
func copyDir(t *testing.T, kind string) string {
tmpDirPath, err := rand.Get()
require.Nil(t, err)
err = os.MkdirAll(tmpDirPath, 0755)
require.Nil(t, err)
dirPath := abs(t, kind, "")
filepath.Walk(dirPath, func(path string, info os.FileInfo, _ error) error {
if info.IsDir() {
return nil
}
fpath := abs(t, kind, info.Name())
in, err := os.Open(fpath)
require.Nil(t, err)
defer in.Close()
tmpFpath := fmt.Sprintf("%s/%s", tmpDirPath, info.Name())
out, err := os.Create(tmpFpath)
require.Nil(t, err)
defer out.Close()
err = out.Chmod(0644)
require.Nil(t, err)
_, err = io.Copy(out, in)
require.Nil(t, err)
_, err = out.Seek(0, 0)
require.Nil(t, err)
return nil
})
return tmpDirPath
}
// HTMLTestFilePath returns the absolute file path
// of a file in "html" folder in test/testdata.
func HTMLTestFilePath(t *testing.T, filename string) string {
return abs(t, "html", filename)
}
// MarkdownTestFilePath returns the absolute file path
// of a file in "markdown" folder in test/testdata.
func MarkdownTestFilePath(t *testing.T, filename string) string {
return abs(t, "markdown", filename)
}
// OfficeTestFilePath returns the absolute file path
// of a file in "office" folder in test/testdata.
func OfficeTestFilePath(t *testing.T, filename string) string {
return abs(t, "office", filename)
}
// PDFTestFilePath returns the absolute file path
// of a file in "pdf" folder in test/testdata.
func PDFTestFilePath(t *testing.T, filename string) string {
return abs(t, "pdf", filename)
}
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
}