mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-18 21:22:15 +01:00
refactoring handlers package tests
This commit is contained in:
@@ -17,10 +17,6 @@ import (
|
|||||||
"github.com/justinas/alice"
|
"github.com/justinas/alice"
|
||||||
)
|
)
|
||||||
|
|
||||||
func fakeSuccessHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
}
|
|
||||||
|
|
||||||
func makeRequest(filesPaths ...string) *http.Request {
|
func makeRequest(filesPaths ...string) *http.Request {
|
||||||
r, w := io.Pipe()
|
r, w := io.Pipe()
|
||||||
mpw := multipart.NewWriter(w)
|
mpw := multipart.NewWriter(w)
|
||||||
@@ -29,6 +25,10 @@ func makeRequest(filesPaths ...string) *http.Request {
|
|||||||
var part io.Writer
|
var part io.Writer
|
||||||
defer w.Close()
|
defer w.Close()
|
||||||
|
|
||||||
|
if len(filesPaths) == 0 {
|
||||||
|
part, _ = mpw.CreateFormField("foo")
|
||||||
|
part.Write([]byte("bar"))
|
||||||
|
} else {
|
||||||
for _, filePath := range filesPaths {
|
for _, filePath := range filesPaths {
|
||||||
file, _ := os.Open(filePath)
|
file, _ := os.Open(filePath)
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
@@ -37,6 +37,7 @@ func makeRequest(filesPaths ...string) *http.Request {
|
|||||||
part, _ = mpw.CreateFormFile("files", fileInfo.Name())
|
part, _ = mpw.CreateFormFile("files", fileInfo.Name())
|
||||||
io.Copy(part, file)
|
io.Copy(part, file)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
mpw.Close()
|
mpw.Close()
|
||||||
}()
|
}()
|
||||||
@@ -46,15 +47,30 @@ func makeRequest(filesPaths ...string) *http.Request {
|
|||||||
return req
|
return req
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func loadCommandConfigs(configurationFilePath string) {
|
||||||
|
path, _ := filepath.Abs(configurationFilePath)
|
||||||
|
c, _ := config.NewAppConfig(path)
|
||||||
|
process.Load(c.CommandsConfig)
|
||||||
|
}
|
||||||
|
|
||||||
|
func fakeSuccessHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
func TestEnforceContentLengthHandler(t *testing.T) {
|
func TestEnforceContentLengthHandler(t *testing.T) {
|
||||||
|
var (
|
||||||
|
req *http.Request
|
||||||
|
rr *httptest.ResponseRecorder
|
||||||
|
)
|
||||||
|
|
||||||
h := alice.New(enforceContentLengthHandler).ThenFunc(fakeSuccessHandler)
|
h := alice.New(enforceContentLengthHandler).ThenFunc(fakeSuccessHandler)
|
||||||
|
|
||||||
// case 1: sends an empty request.
|
// case 1: sends an empty request.
|
||||||
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.
|
||||||
@@ -62,20 +78,25 @@ func TestEnforceContentLengthHandler(t *testing.T) {
|
|||||||
rr = httptest.NewRecorder()
|
rr = httptest.NewRecorder()
|
||||||
h.ServeHTTP(rr, makeRequest(path))
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEnforceContentTypeHandler(t *testing.T) {
|
func TestEnforceContentTypeHandler(t *testing.T) {
|
||||||
|
var (
|
||||||
|
req *http.Request
|
||||||
|
rr *httptest.ResponseRecorder
|
||||||
|
)
|
||||||
|
|
||||||
h := alice.New(enforceContentTypeHandler).ThenFunc(fakeSuccessHandler)
|
h := alice.New(enforceContentTypeHandler).ThenFunc(fakeSuccessHandler)
|
||||||
|
|
||||||
// case 1: sends a wrong content type.
|
// case 1: sends a wrong content type.
|
||||||
req := httptest.NewRequest(http.MethodPost, "/", nil)
|
req = httptest.NewRequest(http.MethodPost, "/", nil)
|
||||||
req.Header.Set("Content-Type", "application/pdf")
|
req.Header.Set("Content-Type", "application/pdf")
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
// case 2: sends a good content type.
|
// case 2: sends a good content type.
|
||||||
@@ -84,62 +105,78 @@ func TestEnforceContentTypeHandler(t *testing.T) {
|
|||||||
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConvertHandler(t *testing.T) {
|
func TestConvertHandler(t *testing.T) {
|
||||||
|
var (
|
||||||
|
req *http.Request
|
||||||
|
rr *httptest.ResponseRecorder
|
||||||
|
path string
|
||||||
|
oPath string
|
||||||
|
)
|
||||||
|
|
||||||
h := alice.New(convertHandler).ThenFunc(fakeSuccessHandler)
|
h := alice.New(convertHandler).ThenFunc(fakeSuccessHandler)
|
||||||
|
|
||||||
// case 1: sends a request without body.
|
// case 1: sends a request without body.
|
||||||
req := httptest.NewRequest(http.MethodPost, "/", nil)
|
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 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 = makeRequest(oPath, path)
|
|
||||||
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 two requests (almost) simultany.
|
// case 2: sends a request with no file.
|
||||||
path, _ = filepath.Abs("../_tests/configurations/gotenberg.yml")
|
req = makeRequest()
|
||||||
appConfig, _ = config.NewAppConfig(path)
|
rr = httptest.NewRecorder()
|
||||||
process.Load(appConfig.CommandsConfig)
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
// TODO
|
loadCommandConfigs("../_tests/configurations/merge-timeout-gotenberg.yml")
|
||||||
|
|
||||||
|
// case 3: sends a request with two files and using an unsuitable timeout for merge commande.
|
||||||
|
path, _ = filepath.Abs("../_tests/configurations/gotenberg.yml")
|
||||||
|
oPath, _ = filepath.Abs("../_tests/file.docx")
|
||||||
|
req = makeRequest(path, oPath)
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
loadCommandConfigs("../_tests/configurations/gotenberg.yml")
|
||||||
|
|
||||||
// case 4: sends a request with two files.
|
// case 4: sends a request with two files.
|
||||||
oPath, _ = filepath.Abs("../_tests/file.docx")
|
|
||||||
path, _ = filepath.Abs("../_tests/file.pdf")
|
path, _ = filepath.Abs("../_tests/file.pdf")
|
||||||
req = makeRequest(oPath, path)
|
oPath, _ = filepath.Abs("../_tests/file.docx")
|
||||||
|
req = makeRequest(path, oPath)
|
||||||
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 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// case 5: sends two requests (almost) simultany.
|
||||||
|
// TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestServeHandler(t *testing.T) {
|
func TestServeHandler(t *testing.T) {
|
||||||
|
var (
|
||||||
|
req *http.Request
|
||||||
|
rr *httptest.ResponseRecorder
|
||||||
|
)
|
||||||
|
|
||||||
h := alice.New().ThenFunc(serveHandler)
|
h := alice.New().ThenFunc(serveHandler)
|
||||||
|
|
||||||
// case 1: sends a request without a result file path entry in its context.
|
// case 1: sends a request without a result file path entry in its context.
|
||||||
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 with a wrong result file path entry in its context.
|
// case 2: sends a request with a wrong result file path entry in its context.
|
||||||
@@ -147,7 +184,7 @@ func TestServeHandler(t *testing.T) {
|
|||||||
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 with a correct result file path entry in its context.
|
// case 3: sends a request with a correct result file path entry in its context.
|
||||||
@@ -156,6 +193,6 @@ func TestServeHandler(t *testing.T) {
|
|||||||
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 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user