mirror of
https://github.com/openfaas/faas.git
synced 2025-06-08 16:26:47 +00:00
Add unit tests for MakeNotifierWrapper
- fixes issue where result was assigned to value rather than to pointer reference. Signed-off-by: Alex Ellis (VMware) <alexellis2@gmail.com>
This commit is contained in:
parent
f7cf7a6496
commit
67c9a71686
@ -13,32 +13,43 @@ func MakeNotifierWrapper(next http.HandlerFunc, notifiers []HTTPNotifier) http.H
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
then := time.Now()
|
||||
|
||||
writer := newCustomWriter(w)
|
||||
next(w, r)
|
||||
writer := newWriteInterceptor(w)
|
||||
next(&writer, r)
|
||||
|
||||
url := r.URL.String()
|
||||
for _, notifier := range notifiers {
|
||||
notifier.Notify(r.Method, url, url, writer.CapturedStatusCode, time.Since(then))
|
||||
notifier.Notify(r.Method, url, url, writer.Status(), time.Since(then))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func newCustomWriter(w http.ResponseWriter) customWriter {
|
||||
return customWriter{
|
||||
func newWriteInterceptor(w http.ResponseWriter) writeInterceptor {
|
||||
return writeInterceptor{
|
||||
w: w,
|
||||
}
|
||||
}
|
||||
|
||||
type customWriter struct {
|
||||
type writeInterceptor struct {
|
||||
CapturedStatusCode int
|
||||
w http.ResponseWriter
|
||||
}
|
||||
|
||||
func (c *customWriter) Write(data []byte) (int, error) {
|
||||
func (c *writeInterceptor) Status() int {
|
||||
if c.CapturedStatusCode == 0 {
|
||||
return http.StatusOK
|
||||
}
|
||||
return c.CapturedStatusCode
|
||||
}
|
||||
|
||||
func (c *writeInterceptor) Header() http.Header {
|
||||
return c.w.Header()
|
||||
}
|
||||
|
||||
func (c *writeInterceptor) Write(data []byte) (int, error) {
|
||||
return c.w.Write(data)
|
||||
}
|
||||
|
||||
func (c *customWriter) WriteHeader(code int) {
|
||||
func (c *writeInterceptor) WriteHeader(code int) {
|
||||
c.CapturedStatusCode = code
|
||||
c.w.WriteHeader(code)
|
||||
}
|
||||
|
89
gateway/handlers/notifier_wrapper_test.go
Normal file
89
gateway/handlers/notifier_wrapper_test.go
Normal file
@ -0,0 +1,89 @@
|
||||
// Copyright (c) OpenFaaS Author(s) 2018. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Test_MakeNotifierWrapper_ReceivesHttpStatusInNotifier(t *testing.T) {
|
||||
notifier := &testNotifier{}
|
||||
handlerVisited := false
|
||||
handlerWant := http.StatusAccepted
|
||||
|
||||
handler := MakeNotifierWrapper(func(w http.ResponseWriter, r *http.Request) {
|
||||
handlerVisited = true
|
||||
w.WriteHeader(handlerWant)
|
||||
}, []HTTPNotifier{notifier})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(rec, req)
|
||||
|
||||
if handlerVisited != true {
|
||||
t.Errorf("expected handler to have been visited")
|
||||
}
|
||||
|
||||
if notifier.StatusReceived == 0 {
|
||||
t.Errorf("notifier wanted a status, but got none")
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
if rec.Result().StatusCode != handlerWant {
|
||||
t.Errorf("recorder status want: %d, got %d", handlerWant, rec.Result().StatusCode)
|
||||
}
|
||||
|
||||
if notifier.StatusReceived != handlerWant {
|
||||
t.Errorf("notifier status want: %d, got %d", handlerWant, notifier.StatusReceived)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func Test_MakeNotifierWrapper_ReceivesDefaultHttpStatusWhenNotSet(t *testing.T) {
|
||||
notifier := &testNotifier{}
|
||||
handlerVisited := false
|
||||
handlerWant := http.StatusOK
|
||||
|
||||
handler := MakeNotifierWrapper(func(w http.ResponseWriter, r *http.Request) {
|
||||
handlerVisited = true
|
||||
}, []HTTPNotifier{notifier})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(rec, req)
|
||||
|
||||
if handlerVisited != true {
|
||||
t.Errorf("expected handler to have been visited")
|
||||
}
|
||||
|
||||
if notifier.StatusReceived == 0 {
|
||||
t.Errorf("notifier wanted a status, but got none")
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
if rec.Result().StatusCode != handlerWant {
|
||||
t.Errorf("recorder status want: %d, got %d", handlerWant, rec.Result().StatusCode)
|
||||
}
|
||||
|
||||
if notifier.StatusReceived != handlerWant {
|
||||
t.Errorf("notifier status want: %d, got %d", handlerWant, notifier.StatusReceived)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
type testNotifier struct {
|
||||
StatusReceived int
|
||||
}
|
||||
|
||||
// Notify about service metrics
|
||||
func (tf *testNotifier) Notify(method string, URL string, originalURL string, statusCode int, duration time.Duration) {
|
||||
tf.StatusReceived = statusCode
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user