mirror of
https://github.com/openfaas/faas.git
synced 2025-06-18 12:06:37 +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:
committed by
Alex Ellis
parent
f7cf7a6496
commit
67c9a71686
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
|
||||
}
|
Reference in New Issue
Block a user