mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-17 04:32:15 +01:00
refactoring: now detecting file type using filename from form data. Also, only one accepted content type
This commit is contained in:
@@ -4,55 +4,28 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFindAuthorizedContentType(t *testing.T) {
|
||||
func TestCheckAuthorizedContentType(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodPost, "/", nil)
|
||||
|
||||
// case 1: uses a request without a content type entry in its header.
|
||||
if _, err := FindAuthorizedContentType(req.Header); err == nil {
|
||||
if err := CheckAuthorizedContentType(req.Header); err == nil {
|
||||
t.Error("It should not have been able to retrieve an authorized content type from header!")
|
||||
}
|
||||
|
||||
// case 2: uses a request with a content type entry in its header.
|
||||
req.Header.Set("Content-Type", string(MultipartFormDataContentType))
|
||||
if _, err := FindAuthorizedContentType(req.Header); err != nil {
|
||||
if err := CheckAuthorizedContentType(req.Header); err != nil {
|
||||
t.Error("It should have been able to retrieve an authorized content type from header!")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSniffContentType(t *testing.T) {
|
||||
// case 1: uses a file with a wrong content type.
|
||||
path, _ := filepath.Abs("../../_tests/configurations/gotenberg.yml")
|
||||
f, _ := os.Open(path)
|
||||
defer f.Close()
|
||||
if _, err := SniffContentType(f); err == nil {
|
||||
t.Error("It should not have been able to retrieve an authorized content type from an YAML file!")
|
||||
}
|
||||
|
||||
// case 2: uses a file with a correct content type.
|
||||
path, _ = filepath.Abs("../../_tests/file.pdf")
|
||||
f, _ = os.Open(path)
|
||||
defer f.Close()
|
||||
if _, err := SniffContentType(f); err != nil {
|
||||
t.Error("It should have been able to retrieve an authorized content type from a PDF file!")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotAuthorizedContentTypeError(t *testing.T) {
|
||||
err := ¬AuthorizedContentTypeError{}
|
||||
message := fmt.Sprintf("Accepted values for 'Content-Type': %s, %s", OctetStreamContentType, MultipartFormDataContentType)
|
||||
message := fmt.Sprintf("Accepted value for 'Content-Type': %s", MultipartFormDataContentType)
|
||||
if err.Error() != message {
|
||||
t.Errorf("Error returned a wrong message: got %s want %s", err.Error(), message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotAuthorizedFileContentTypeError(t *testing.T) {
|
||||
err := ¬AuthorizedFileContentTypeError{}
|
||||
if err.Error() != notAuthorizedFileContentTypeErrorMessage {
|
||||
t.Errorf("Error returned a wrong message: got %s want %s", err.Error(), notAuthorizedFileContentTypeErrorMessage)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user