fixing test in process package + tests for app package

This commit is contained in:
Julien Neuhart
2018-04-05 17:57:15 +02:00
parent acea482456
commit bf28499e18
3 changed files with 109 additions and 23 deletions

View File

@@ -105,7 +105,7 @@ func TestRun(t *testing.T) {
} }
// case 2: uses a simple command but with an unsuitable timeout. // 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!") t.Error("Command should not have worked!")
} }

View File

@@ -128,6 +128,7 @@ func clearHandler(w http.ResponseWriter, r *http.Request) {
c, err := context.GetConverter(r) c, err := context.GetConverter(r)
if err != nil { if err != nil {
logger.Warn(err.Error()) logger.Warn(err.Error())
return
} }
if err := c.Clear(); err != nil { if err := c.Clear(); err != nil {

View File

@@ -1,14 +1,19 @@
package app package app
import ( import (
"bytes"
"io" "io"
"mime/multipart"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/gulien/gotenberg/app/config"
"github.com/gulien/gotenberg/app/context" "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" ghttp "github.com/gulien/gotenberg/app/http"
"github.com/justinas/alice" "github.com/justinas/alice"
@@ -18,15 +23,42 @@ func fakeSuccessHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
func makeBody(filesPaths ...string) io.Reader { func makeRequest(filesPaths ...string) *http.Request {
if len(filesPaths) == 1 { if len(filesPaths) == 0 {
file, _ := os.Open(filesPaths[0]) req := httptest.NewRequest(http.MethodPost, "/", new(bytes.Buffer))
defer file.Close() req.Header.Set("Content-Type", string(ghttp.OctetStreamContentType))
return file return req
} }
// TODO if len(filesPaths) == 1 {
return nil 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) { func TestEnforceContentLengthHandler(t *testing.T) {
@@ -36,17 +68,14 @@ func TestEnforceContentLengthHandler(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/", nil) req := httptest.NewRequest(http.MethodPost, "/", nil)
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
h.ServeHTTP(rr, req) h.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusBadRequest { if status := rr.Code; status != http.StatusBadRequest {
t.Errorf("Handler returned a wrong status code: got %v want %v", status, http.StatusBadRequest) t.Errorf("Handler returned a wrong status code: got %v want %v", status, http.StatusBadRequest)
} }
// case 2: sends a body. // case 2: sends a body.
path, _ := filepath.Abs("../../_tests/file.docx") path, _ := filepath.Abs("../_tests/file.docx")
req = httptest.NewRequest(http.MethodPost, "/", makeBody(path))
rr = httptest.NewRecorder() rr = httptest.NewRecorder()
h.ServeHTTP(rr, req) h.ServeHTTP(rr, makeRequest(path))
if status := rr.Code; status != http.StatusOK { if status := rr.Code; status != http.StatusOK {
t.Errorf("Handler returned a wrong status code: got %v want %v", 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)) req.Header.Set("Content-Type", string(ghttp.PDFContentType))
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
h.ServeHTTP(rr, req) h.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusUnsupportedMediaType { if status := rr.Code; status != http.StatusUnsupportedMediaType {
t.Errorf("Handler returned a wrong status code: got %v want %v", 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)) req.Header.Set("Content-Type", string(ghttp.OctetStreamContentType))
rr = httptest.NewRecorder() rr = httptest.NewRecorder()
h.ServeHTTP(rr, req) h.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK { if status := rr.Code; status != http.StatusOK {
t.Errorf("Handler returned wrong a status code: got %v want %v", 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) req := httptest.NewRequest(http.MethodPost, "/", nil)
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
h.ServeHTTP(rr, req) h.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusInternalServerError { if status := rr.Code; status != http.StatusInternalServerError {
t.Errorf("Handler returned a wrong status code: got %v want %v", 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) req = context.WithContentType(httptest.NewRequest(http.MethodPost, "/", nil), ghttp.OctetStreamContentType)
rr = httptest.NewRecorder() rr = httptest.NewRecorder()
h.ServeHTTP(rr, req) h.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusInternalServerError { if status := rr.Code; status != http.StatusInternalServerError {
t.Errorf("Handler returned a wrong status code: got %v want %v", 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. // case 3: sends a request with two files and using an unsuitable timeout for merge commande.
/*req = context.WithContentType(httptest.NewRequest(http.MethodPost, "/", nil), ghttp.MultipartFormDataContentType) 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() rr = httptest.NewRecorder()
h.ServeHTTP(rr, req) 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 { // case 4: sends a request with two files.
t.Errorf("Handler returned wrong status code: got %v want %v", status, http.StatusBadRequest) 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)
} }