mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-08 08:32:16 +01:00
fixing test in process package + tests for app package
This commit is contained in:
@@ -105,7 +105,7 @@ func TestRun(t *testing.T) {
|
||||
}
|
||||
|
||||
// case 2: uses a simple command but with an unsuitable timeout.
|
||||
if err := run("echo Hello world", 0); err == nil {
|
||||
if err := run("sleep 5", 0); err == nil {
|
||||
t.Error("Command should not have worked!")
|
||||
}
|
||||
|
||||
|
||||
@@ -128,6 +128,7 @@ func clearHandler(w http.ResponseWriter, r *http.Request) {
|
||||
c, err := context.GetConverter(r)
|
||||
if err != nil {
|
||||
logger.Warn(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if err := c.Clear(); err != nil {
|
||||
|
||||
@@ -1,14 +1,19 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/gulien/gotenberg/app/config"
|
||||
"github.com/gulien/gotenberg/app/context"
|
||||
"github.com/gulien/gotenberg/app/converter"
|
||||
"github.com/gulien/gotenberg/app/converter/process"
|
||||
ghttp "github.com/gulien/gotenberg/app/http"
|
||||
|
||||
"github.com/justinas/alice"
|
||||
@@ -18,15 +23,42 @@ func fakeSuccessHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func makeBody(filesPaths ...string) io.Reader {
|
||||
if len(filesPaths) == 1 {
|
||||
file, _ := os.Open(filesPaths[0])
|
||||
defer file.Close()
|
||||
return file
|
||||
func makeRequest(filesPaths ...string) *http.Request {
|
||||
if len(filesPaths) == 0 {
|
||||
req := httptest.NewRequest(http.MethodPost, "/", new(bytes.Buffer))
|
||||
req.Header.Set("Content-Type", string(ghttp.OctetStreamContentType))
|
||||
return req
|
||||
}
|
||||
|
||||
// TODO
|
||||
return nil
|
||||
if len(filesPaths) == 1 {
|
||||
file, _ := os.Open(filesPaths[0])
|
||||
req := httptest.NewRequest(http.MethodPost, "/", file)
|
||||
req.Header.Set("Content-Type", string(ghttp.OctetStreamContentType))
|
||||
return req
|
||||
}
|
||||
|
||||
r, w := io.Pipe()
|
||||
mpw := multipart.NewWriter(w)
|
||||
|
||||
go func() {
|
||||
var part io.Writer
|
||||
defer w.Close()
|
||||
|
||||
for _, filePath := range filesPaths {
|
||||
file, _ := os.Open(filePath)
|
||||
defer file.Close()
|
||||
|
||||
fileInfo, _ := file.Stat()
|
||||
part, _ = mpw.CreateFormFile("files", fileInfo.Name())
|
||||
io.Copy(part, file)
|
||||
}
|
||||
|
||||
mpw.Close()
|
||||
}()
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/", r)
|
||||
req.Header.Set("Content-Type", mpw.FormDataContentType())
|
||||
return req
|
||||
}
|
||||
|
||||
func TestEnforceContentLengthHandler(t *testing.T) {
|
||||
@@ -36,17 +68,14 @@ func TestEnforceContentLengthHandler(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodPost, "/", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
h.ServeHTTP(rr, req)
|
||||
|
||||
if status := rr.Code; status != http.StatusBadRequest {
|
||||
t.Errorf("Handler returned a wrong status code: got %v want %v", status, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
// case 2: sends a body.
|
||||
path, _ := filepath.Abs("../../_tests/file.docx")
|
||||
req = httptest.NewRequest(http.MethodPost, "/", makeBody(path))
|
||||
path, _ := filepath.Abs("../_tests/file.docx")
|
||||
rr = httptest.NewRecorder()
|
||||
h.ServeHTTP(rr, req)
|
||||
|
||||
h.ServeHTTP(rr, makeRequest(path))
|
||||
if status := rr.Code; status != http.StatusOK {
|
||||
t.Errorf("Handler returned a wrong status code: got %v want %v", status, http.StatusOK)
|
||||
}
|
||||
@@ -60,7 +89,6 @@ func TestEnforceContentTypeHandler(t *testing.T) {
|
||||
req.Header.Set("Content-Type", string(ghttp.PDFContentType))
|
||||
rr := httptest.NewRecorder()
|
||||
h.ServeHTTP(rr, req)
|
||||
|
||||
if status := rr.Code; status != http.StatusUnsupportedMediaType {
|
||||
t.Errorf("Handler returned a wrong status code: got %v want %v", status, http.StatusUnsupportedMediaType)
|
||||
}
|
||||
@@ -70,7 +98,6 @@ func TestEnforceContentTypeHandler(t *testing.T) {
|
||||
req.Header.Set("Content-Type", string(ghttp.OctetStreamContentType))
|
||||
rr = httptest.NewRecorder()
|
||||
h.ServeHTTP(rr, req)
|
||||
|
||||
if status := rr.Code; status != http.StatusOK {
|
||||
t.Errorf("Handler returned wrong a status code: got %v want %v", status, http.StatusOK)
|
||||
}
|
||||
@@ -83,26 +110,84 @@ func TestConvertHandler(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodPost, "/", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
h.ServeHTTP(rr, req)
|
||||
|
||||
if status := rr.Code; status != http.StatusInternalServerError {
|
||||
t.Errorf("Handler returned a wrong status code: got %v want %v", status, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
// case 2: sends a request as without body.
|
||||
// case 2: sends a request without body.
|
||||
req = context.WithContentType(httptest.NewRequest(http.MethodPost, "/", nil), ghttp.OctetStreamContentType)
|
||||
rr = httptest.NewRecorder()
|
||||
h.ServeHTTP(rr, req)
|
||||
|
||||
if status := rr.Code; status != http.StatusInternalServerError {
|
||||
t.Errorf("Handler returned a wrong status code: got %v want %v", status, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
// case 3: sends a request as "multipart/form-data" without "files" key.
|
||||
/*req = context.WithContentType(httptest.NewRequest(http.MethodPost, "/", nil), ghttp.MultipartFormDataContentType)
|
||||
// case 3: sends a request with two files and using an unsuitable timeout for merge commande.
|
||||
path, _ := filepath.Abs("../_tests/configurations/merge-timeout-gotenberg.yml")
|
||||
appConfig, _ := config.NewAppConfig(path)
|
||||
process.Load(appConfig.CommandsConfig)
|
||||
|
||||
oPath, _ := filepath.Abs("../_tests/file.docx")
|
||||
path, _ = filepath.Abs("../_tests/configurations/gotenberg.yml")
|
||||
req = context.WithContentType(makeRequest(oPath, path), ghttp.MultipartFormDataContentType)
|
||||
rr = httptest.NewRecorder()
|
||||
h.ServeHTTP(rr, req)
|
||||
if status := rr.Code; status != http.StatusInternalServerError {
|
||||
t.Errorf("Handler returned a wrong status code: got %v want %v", status, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if status := rr.Code; status != http.StatusBadRequest {
|
||||
t.Errorf("Handler returned wrong status code: got %v want %v", status, http.StatusBadRequest)
|
||||
}*/
|
||||
// case 4: sends a request with two files.
|
||||
path, _ = filepath.Abs("../_tests/configurations/gotenberg.yml")
|
||||
appConfig, _ = config.NewAppConfig(path)
|
||||
process.Load(appConfig.CommandsConfig)
|
||||
|
||||
oPath, _ = filepath.Abs("../_tests/file.docx")
|
||||
path, _ = filepath.Abs("../_tests/file.pdf")
|
||||
req = context.WithContentType(makeRequest(oPath, path), ghttp.MultipartFormDataContentType)
|
||||
rr = httptest.NewRecorder()
|
||||
h.ServeHTTP(rr, req)
|
||||
if status := rr.Code; status != http.StatusOK {
|
||||
t.Errorf("Handler returned a wrong status code: got %v want %v", status, http.StatusOK)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServeHandler(t *testing.T) {
|
||||
h := alice.New(serveHandler).ThenFunc(fakeSuccessHandler)
|
||||
|
||||
// case 1: sends a request without a result file path entry in its context.
|
||||
req := httptest.NewRequest(http.MethodPost, "/", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
h.ServeHTTP(rr, req)
|
||||
if status := rr.Code; status != http.StatusInternalServerError {
|
||||
t.Errorf("Handler returned a wrong status code: got %v want %v", status, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
// case 2: sends a request with a wrong result file path entry in its context.
|
||||
req = context.WithResultFilePath(httptest.NewRequest(http.MethodPost, "/", nil), "file")
|
||||
rr = httptest.NewRecorder()
|
||||
h.ServeHTTP(rr, req)
|
||||
if status := rr.Code; status != http.StatusInternalServerError {
|
||||
t.Errorf("Handler returned a wrong status code: got %v want %v", status, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
// case 3: sends a request with a correct result file path entry in its context.
|
||||
path, _ := filepath.Abs("../_tests/file.pdf")
|
||||
req = context.WithResultFilePath(httptest.NewRequest(http.MethodPost, "/", nil), path)
|
||||
rr = httptest.NewRecorder()
|
||||
h.ServeHTTP(rr, req)
|
||||
if status := rr.Code; status != http.StatusOK {
|
||||
t.Errorf("Handler returned a wrong status code: got %v want %v", status, http.StatusOK)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClearHandler(t *testing.T) {
|
||||
// case 1: sends a request without a converter entry in its context.
|
||||
req := httptest.NewRequest(http.MethodPost, "/", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
clearHandler(rr, req)
|
||||
|
||||
// case 2: sends with a wrong converter entry in its context.
|
||||
req = context.WithConverter(httptest.NewRequest(http.MethodPost, "/", nil), &converter.Converter{})
|
||||
rr = httptest.NewRecorder()
|
||||
clearHandler(rr, req)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user