Skip to content

Commit

Permalink
Add explicit test for the logging notifier middleware
Browse files Browse the repository at this point in the history
**What**
- Add unit test that verifies the behavior of the logging middleware in
  various reponse cases

Signed-off-by: Lucas Roesler <[email protected]>
  • Loading branch information
LucasRoesler authored and alexellis committed Feb 27, 2021
1 parent 048a90a commit 089cd33
Showing 1 changed file with 124 additions and 0 deletions.
124 changes: 124 additions & 0 deletions gateway/handlers/notifier_wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
package handlers

import (
"bytes"
"fmt"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -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)
}
})
}

}

0 comments on commit 089cd33

Please sign in to comment.