...

Source file src/edge-infra.dev/pkg/lib/fog/middleware_test.go

Documentation: edge-infra.dev/pkg/lib/fog

     1  package fog
     2  
     3  import (
     4  	"bytes"
     5  	"net/http"
     6  	"net/url"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/go-logr/logr"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  type testLogSink struct {
    15  	fnInit       func(ri logr.RuntimeInfo)
    16  	fnEnabled    func(lvl int) bool
    17  	fnInfo       func(lvl int, msg string, kv ...interface{})
    18  	fnError      func(err error, msg string, kv ...interface{})
    19  	fnWithValues func(kv ...interface{})
    20  	fnWithName   func(name string)
    21  }
    22  
    23  var _ logr.LogSink = &testLogSink{}
    24  
    25  func (l *testLogSink) Init(ri logr.RuntimeInfo) {
    26  	if l.fnInit != nil {
    27  		l.fnInit(ri)
    28  	}
    29  }
    30  
    31  func (l *testLogSink) Enabled(lvl int) bool {
    32  	if l.fnEnabled != nil {
    33  		return l.fnEnabled(lvl)
    34  	}
    35  	return false
    36  }
    37  
    38  func (l *testLogSink) Info(lvl int, msg string, kv ...interface{}) {
    39  	if l.fnInfo != nil {
    40  		l.fnInfo(lvl, msg, kv...)
    41  	}
    42  }
    43  
    44  func (l *testLogSink) Error(err error, msg string, kv ...interface{}) {
    45  	if l.fnError != nil {
    46  		l.fnError(err, msg, kv...)
    47  	}
    48  }
    49  
    50  func (l *testLogSink) WithValues(kv ...interface{}) logr.LogSink {
    51  	if l.fnWithValues != nil {
    52  		l.fnWithValues(kv...)
    53  	}
    54  	out := *l
    55  	return &out
    56  }
    57  
    58  func (l *testLogSink) WithName(name string) logr.LogSink {
    59  	if l.fnWithName != nil {
    60  		l.fnWithName(name)
    61  	}
    62  	out := *l
    63  	return &out
    64  }
    65  
    66  func TestMiddlewareLoggerError(t *testing.T) {
    67  	calledError := 0
    68  	errInput := "Bad Gateway"
    69  	msgInput := "POST / 502 Bad Gateway"
    70  
    71  	sink := &testLogSink{}
    72  	sink.fnError = func(err error, msg string, _ ...interface{}) {
    73  		calledError++
    74  		if err.Error() != errInput {
    75  			t.Errorf("unexpected err input, got %v", err)
    76  		}
    77  		if msg != msgInput {
    78  			t.Errorf("unexpected msg input, got %q", msg)
    79  		}
    80  	}
    81  	logger := logr.New(sink)
    82  	MiddlewareLogger(logger, &MiddlewareContext{
    83  		Request: &http.Request{
    84  			Method: http.MethodPost,
    85  			URL: &url.URL{
    86  				Scheme: "http",
    87  				Host:   "ncr.com",
    88  				Path:   "/",
    89  			},
    90  		},
    91  		Status: http.StatusBadGateway,
    92  		Size:   120,
    93  		Body:   bytes.NewBufferString("hello test"),
    94  		Time:   5 * time.Second,
    95  	})
    96  	if calledError != 1 {
    97  		t.Errorf("expected Error() to be called once, got %d", calledError)
    98  	}
    99  }
   100  
   101  func TestMiddlewareLoggerWithValues(t *testing.T) {
   102  	calledWithValues := 0
   103  	printSink := make(map[string]bool)
   104  	sink := &testLogSink{}
   105  	sink.fnWithValues = func(kv ...interface{}) {
   106  		calledWithValues++
   107  		for _, res := range kv {
   108  			val, ok := res.(string)
   109  			if ok {
   110  				printSink[val] = true
   111  			}
   112  		}
   113  	}
   114  	logger := logr.New(sink)
   115  	MiddlewareLogger(logger, &MiddlewareContext{
   116  		Request: &http.Request{
   117  			Method: http.MethodPost,
   118  			URL: &url.URL{
   119  				Scheme: "http",
   120  				Host:   "ncr.com",
   121  				Path:   "/",
   122  			},
   123  		},
   124  		Status: http.StatusUnauthorized,
   125  		Size:   120,
   126  		Body:   bytes.NewBufferString("hello test"),
   127  		Time:   5 * time.Second,
   128  	})
   129  	_, exists := printSink["httpRequest"]
   130  	assert.True(t, exists)
   131  	_, exists = printSink["httpResponse"]
   132  	assert.True(t, exists)
   133  	if calledWithValues != 2 {
   134  		t.Errorf("expected WithValues() to be called twice, got %d", calledWithValues)
   135  	}
   136  }
   137  

View as plain text