mirror of
https://github.com/openfaas/faas.git
synced 2025-06-08 16:26:47 +00:00
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 <roesler.lucas@gmail.com>
This commit is contained in:
parent
048a90a150
commit
089cd332b8
@ -4,8 +4,12 @@
|
|||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"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) {
|
func (tf *testNotifier) Notify(method string, URL string, originalURL string, statusCode int, event string, duration time.Duration) {
|
||||||
tf.StatusReceived = statusCode
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user