From 089cd332b88b1632c8751389ddb7879f06c7d130 Mon Sep 17 00:00:00 2001 From: Lucas Roesler Date: Fri, 19 Feb 2021 21:19:33 +0100 Subject: [PATCH] Add explicit test for the logging notifier middleware **What** - Add unit test that verifies the behavior of the logging middleware in various reponse cases Signed-off-by: Lucas Roesler --- gateway/handlers/notifier_wrapper_test.go | 124 ++++++++++++++++++++++ 1 file changed, 124 insertions(+) diff --git a/gateway/handlers/notifier_wrapper_test.go b/gateway/handlers/notifier_wrapper_test.go index fee77e8bc..bb8bea65d 100644 --- a/gateway/handlers/notifier_wrapper_test.go +++ b/gateway/handlers/notifier_wrapper_test.go @@ -4,8 +4,12 @@ package handlers import ( + "bytes" + "fmt" + "log" "net/http" "net/http/httptest" + "strings" "testing" "time" ) @@ -87,3 +91,123 @@ type testNotifier struct { func (tf *testNotifier) Notify(method string, URL string, originalURL string, statusCode int, event string, duration time.Duration) { tf.StatusReceived = statusCode } + +func TestLoggingMiddleware(t *testing.T) { + + logger := LoggingNotifier{} + cases := []struct { + name string + status int + method string + path string + }{ + { + name: "logs successful GET request", + status: http.StatusOK, + method: http.MethodGet, + path: "/a/b/c", + }, + { + name: "logs successful POST request", + status: http.StatusOK, + method: http.MethodPost, + path: "/a/b/c", + }, + { + name: "logs successful PATCH request", + status: http.StatusOK, + method: http.MethodPatch, + path: "/a/b/c", + }, + { + name: "logs successful PUT request", + status: http.StatusOK, + method: http.MethodPut, + path: "/a/b/c", + }, + { + name: "logs successful DELETE request", + status: http.StatusOK, + method: http.MethodDelete, + path: "/a/b/c", + }, + { + name: "logs successful OPTIONS request", + status: http.StatusOK, + method: http.MethodOptions, + path: "/a/b/c", + }, + { + name: "logs 201 success", + status: http.StatusCreated, + method: http.MethodGet, + path: "/a/b/c", + }, + { + name: "logs 204 success", + status: http.StatusNoContent, + method: http.MethodGet, + path: "/a/b/c", + }, + { + name: "logs 400 failure", + status: http.StatusBadRequest, + method: http.MethodGet, + path: "/a/b/c", + }, + { + name: "logs 401 failure", + status: http.StatusUnauthorized, + method: http.MethodGet, + path: "/a/b/c", + }, + { + name: "logs 403 failure", + status: http.StatusForbidden, + method: http.MethodGet, + path: "/a/b/c", + }, + { + name: "logs 500 failure", + status: http.StatusInternalServerError, + method: http.MethodGet, + path: "/a/b/c", + }, + { + name: "logs 301 redirect", + status: http.StatusMovedPermanently, + method: http.MethodGet, + path: "/a/b/c", + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + var b bytes.Buffer + log.SetOutput(&b) + log.SetFlags(0) + log.SetPrefix("") + + handler := MakeNotifierWrapper(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(tc.status) + }, []HTTPNotifier{logger}) + + req := httptest.NewRequest(tc.method, tc.path, nil) + rec := httptest.NewRecorder() + + handler.ServeHTTP(rec, req) + + if rec.Code != tc.status { + t.Fatalf("unexpected status code, expected %d, got %d", tc.status, rec.Code) + } + + logs := b.String() + + prefix := fmt.Sprintf("Forwarded [%s] to %s - [%d] -", tc.method, tc.path, tc.status) + if !strings.HasPrefix(logs, prefix) { + t.Fatalf("expected log to start with: %q\ngot: %q", prefix, logs) + } + }) + } + +}