From 3d8247ec509301d3e56563319c8dce47ce2504b9 Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Wed, 4 Apr 2018 17:59:45 +0200 Subject: [PATCH] tests for context package + typos --- app/context/context.go | 1 - app/context/context_test.go | 77 +++++++++++++++++++++++++++++++++++++ app/handlers_test.go | 14 +++---- 3 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 app/context/context_test.go diff --git a/app/context/context.go b/app/context/context.go index ff34bc33..eabb1275 100644 --- a/app/context/context.go +++ b/app/context/context.go @@ -7,7 +7,6 @@ import ( "net/http" "github.com/gulien/gotenberg/app/converter" - ghttp "github.com/gulien/gotenberg/app/http" ) diff --git a/app/context/context_test.go b/app/context/context_test.go new file mode 100644 index 00000000..15010eed --- /dev/null +++ b/app/context/context_test.go @@ -0,0 +1,77 @@ +package context + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/gulien/gotenberg/app/converter" + ghttp "github.com/gulien/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 { + t.Errorf("Context returned a wrong converter: got %v want not nil", c) + } +} + +func TestGetConverter(t *testing.T) { + req := httptest.NewRequest(http.MethodPost, "/", nil) + + // case 1: uses a request without a converter entry in its context. + if _, err := GetConverter(req); err == nil { + t.Error("Context should not have a converter entry!") + } + + // case 2: uses a request with a converter entry in its context. + req = WithConverter(req, &converter.Converter{}) + if _, err := GetConverter(req); err != nil { + t.Error("Context should have a converter entry!") + } +} + +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) + + // case 1: uses a request without a result file path entry in its context. + if _, err := GetResultFilePath(req); err == nil { + t.Error("Context should not have a result file path entry!") + } + + // case 2: uses a request with a result file path entry in its context. + req = WithResultFilePath(req, "file.pdf") + if _, err := GetResultFilePath(req); err != nil { + t.Error("Context should have a converter entry!") + } +} diff --git a/app/handlers_test.go b/app/handlers_test.go index 8c937302..95ca5bce 100644 --- a/app/handlers_test.go +++ b/app/handlers_test.go @@ -38,7 +38,7 @@ func TestEnforceContentLengthHandler(t *testing.T) { h.ServeHTTP(rr, req) if status := rr.Code; status != http.StatusBadRequest { - t.Errorf("Handler returned 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. @@ -48,7 +48,7 @@ func TestEnforceContentLengthHandler(t *testing.T) { h.ServeHTTP(rr, req) if status := rr.Code; status != http.StatusOK { - t.Errorf("Handler returned 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) } } @@ -62,7 +62,7 @@ func TestEnforceContentTypeHandler(t *testing.T) { h.ServeHTTP(rr, req) if status := rr.Code; status != http.StatusUnsupportedMediaType { - t.Errorf("Handler returned 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. @@ -72,20 +72,20 @@ func TestEnforceContentTypeHandler(t *testing.T) { h.ServeHTTP(rr, req) if status := rr.Code; status != http.StatusOK { - t.Errorf("Handler returned wrong 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) { h := alice.New(convertHandler).ThenFunc(fakeSuccessHandler) - // case 1: sends a request without a content type in its context. + // case 1: sends a request without a content type 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 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. @@ -94,7 +94,7 @@ func TestConvertHandler(t *testing.T) { h.ServeHTTP(rr, req) if status := rr.Code; status != http.StatusInternalServerError { - t.Errorf("Handler returned 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.