refactoring: now detecting file type using filename from form data. Also, only one accepted content type

This commit is contained in:
Julien Neuhart
2018-04-08 21:49:02 +02:00
parent cdeef33ff7
commit bca02f6198
14 changed files with 119 additions and 400 deletions

View File

@@ -7,46 +7,15 @@ import (
"net/http"
"github.com/thecodingmachine/gotenberg/app/converter"
ghttp "github.com/thecodingmachine/gotenberg/app/http"
)
type key uint32
const (
contentTypeKey key = iota
converterKey
converterKey key = iota
resultFilePathKey
)
// WithContentType populates a request's context with the given content type
// and returns the updated request.
func WithContentType(r *http.Request, contentType ghttp.ContentType) *http.Request {
ctx := r.Context()
ctx = context.WithValue(ctx, contentTypeKey, contentType)
r = r.WithContext(ctx)
return r
}
type contentTypeNotFoundError struct{}
const contentTypeNotFoundErrorMessage = "The 'Content-Type' was not found in request context"
func (e *contentTypeNotFoundError) Error() string {
return contentTypeNotFoundErrorMessage
}
// GetContentType returns the content type if found in
// the request's context. Otherwise throws an error.
func GetContentType(r *http.Request) (ghttp.ContentType, error) {
ct, ok := r.Context().Value(contentTypeKey).(ghttp.ContentType)
if !ok {
return "", &contentTypeNotFoundError{}
}
return ct, nil
}
// WithConverter populates a request's context with the given converter
// and returns the updated request.
func WithConverter(r *http.Request, converter *converter.Converter) *http.Request {

View File

@@ -6,31 +6,8 @@ import (
"testing"
"github.com/thecodingmachine/gotenberg/app/converter"
ghttp "github.com/thecodingmachine/gotenberg/app/http"
)
func TestWithContentType(t *testing.T) {
req := WithContentType(httptest.NewRequest(http.MethodPost, "/", nil), ghttp.MultipartFormDataContentType)
if ct, _ := req.Context().Value(contentTypeKey).(ghttp.ContentType); ct != ghttp.MultipartFormDataContentType {
t.Errorf("Context returned a wrong content type: got %v want %v", ct, ghttp.MultipartFormDataContentType)
}
}
func TestGetContentType(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/", nil)
// case 1: uses a request without a content type entry in its context.
if _, err := GetContentType(req); err == nil {
t.Error("Context should not have a content type entry!")
}
// case 2: uses a request with a content type entry in its context.
req = WithContentType(req, ghttp.MultipartFormDataContentType)
if _, err := GetContentType(req); err != nil {
t.Error("Context should have a content type entry!")
}
}
func TestWithConverter(t *testing.T) {
req := WithConverter(httptest.NewRequest(http.MethodPost, "/", nil), &converter.Converter{})
if c, _ := req.Context().Value(converterKey).(*converter.Converter); c == nil {
@@ -53,14 +30,6 @@ func TestGetConverter(t *testing.T) {
}
}
func TestWithResultFilePath(t *testing.T) {
filePath := "file.pdf"
req := WithResultFilePath(httptest.NewRequest(http.MethodPost, "/", nil), filePath)
if path, _ := req.Context().Value(resultFilePathKey).(string); path != filePath {
t.Errorf("Context returned a wrong converter: got %s want %s", path, filePath)
}
}
func TestGetResultFilePath(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/", nil)
@@ -76,10 +45,11 @@ func TestGetResultFilePath(t *testing.T) {
}
}
func TestContentTypeNotFoundError(t *testing.T) {
err := &contentTypeNotFoundError{}
if err.Error() != contentTypeNotFoundErrorMessage {
t.Errorf("Error returned a wrong message: got %s want %s", err.Error(), contentTypeNotFoundErrorMessage)
func TestWithResultFilePath(t *testing.T) {
filePath := "file.pdf"
req := WithResultFilePath(httptest.NewRequest(http.MethodPost, "/", nil), filePath)
if path, _ := req.Context().Value(resultFilePathKey).(string); path != filePath {
t.Errorf("Context returned a wrong converter: got %s want %s", path, filePath)
}
}