github.com/prebid/prebid-server/v2@v2.18.0/config/structlog_test.go (about)

     1  package config
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"reflect"
     7  	"testing"
     8  )
     9  
    10  type testStruct struct {
    11  	Myint    int    `mapstructure:"this_int"`
    12  	Mystring string `mapstructure:"mystring"`
    13  	Flag     bool
    14  	Sub      innerStruct `mapstructure:"sub"`
    15  	Caps     map[string]string
    16  }
    17  
    18  type innerStruct struct {
    19  	int1     int    `mapstructure:"int1"`
    20  	password string `mapstructure:"password"`
    21  }
    22  
    23  var expected string = `this_int: 5
    24  mystring: foobar
    25  ((Flag)): false
    26  sub.int1: 3
    27  sub.password: <REDACTED>
    28  ((Caps))[Alabama]: Montgomery
    29  `
    30  
    31  func TestBasic(t *testing.T) {
    32  	var buf bytes.Buffer
    33  
    34  	mylogger := func(msg string, args ...interface{}) {
    35  		buf.WriteString(fmt.Sprintf(fmt.Sprintln(msg), args...))
    36  	}
    37  
    38  	testCfg := testStruct{
    39  		Myint:    5,
    40  		Mystring: "foobar",
    41  		Sub: innerStruct{
    42  			int1:     3,
    43  			password: "secret",
    44  		},
    45  		// Can't do more than one entry as order is not guaranteed.
    46  		Caps: map[string]string{
    47  			"Alabama": "Montgomery",
    48  		},
    49  	}
    50  
    51  	logStructWithLogger(reflect.ValueOf(testCfg), "", mylogger)
    52  
    53  	result := buf.String()
    54  
    55  	if expected != result {
    56  		t.Errorf("Did not log properly.\ndesired:%s\nfound:%s\nsource: %v", expected, result, testCfg)
    57  	}
    58  }