...

Source file src/github.com/go-kivik/kivik/v4/x/kivikd/logger/logger_test.go

Documentation: github.com/go-kivik/kivik/v4/x/kivikd/logger

     1  // Licensed under the Apache License, Version 2.0 (the "License"); you may not
     2  // use this file except in compliance with the License. You may obtain a copy of
     3  // the License at
     4  //
     5  //  http://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     9  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    10  // License for the specific language governing permissions and limitations under
    11  // the License.
    12  
    13  package logger
    14  
    15  import (
    16  	"bytes"
    17  	"net/http"
    18  	"testing"
    19  	"time"
    20  
    21  	"gitlab.com/flimzy/testy"
    22  )
    23  
    24  func TestFields(t *testing.T) {
    25  	now := time.Now()
    26  	f := Fields{
    27  		"exists": "exists",
    28  		"dur":    time.Second,
    29  		"time":   now,
    30  		"int":    123,
    31  	}
    32  	t.Run("Exists", func(t *testing.T) {
    33  		if !f.Exists("exists") {
    34  			t.Errorf("Should exist")
    35  		}
    36  	})
    37  	t.Run("NotExists", func(t *testing.T) {
    38  		if f.Exists("notExists") {
    39  			t.Errorf("Should not exist")
    40  		}
    41  	})
    42  	t.Run("Get", func(t *testing.T) {
    43  		v, ok := f.Get("exists").(string)
    44  		if !ok {
    45  			t.Errorf("Should have returned a string")
    46  		}
    47  		if v != "exists" {
    48  			t.Errorf("Should have returned expected value")
    49  		}
    50  	})
    51  	t.Run("GetString", func(t *testing.T) {
    52  		if f.GetString("exists") != "exists" {
    53  			t.Errorf("Should have returned expected value")
    54  		}
    55  	})
    56  	t.Run("GetStringNotExist", func(t *testing.T) {
    57  		if f.GetString("notExists") != "-" {
    58  			t.Errorf("Should have returned placeholder value")
    59  		}
    60  	})
    61  	t.Run("GetDuration", func(t *testing.T) {
    62  		if f.GetDuration("dur") != time.Second {
    63  			t.Errorf("Should have returned expected value")
    64  		}
    65  	})
    66  	t.Run("GetDurationNotExist", func(t *testing.T) {
    67  		if f.GetDuration("notExists") != time.Duration(0) {
    68  			t.Errorf("Should have returned zero value")
    69  		}
    70  	})
    71  	t.Run("GetTime", func(t *testing.T) {
    72  		if !f.GetTime("time").Equal(now) {
    73  			t.Errorf("Should have returned expected value")
    74  		}
    75  	})
    76  	t.Run("GetTimeNotExist", func(t *testing.T) {
    77  		if !f.GetTime("notExist").IsZero() {
    78  			t.Errorf("Should have returned zero time")
    79  		}
    80  	})
    81  	t.Run("GetInt", func(t *testing.T) {
    82  		if f.GetInt("int") != 123 {
    83  			t.Errorf("Should have returned expected value")
    84  		}
    85  	})
    86  	t.Run("GetIntNotExist", func(t *testing.T) {
    87  		if f.GetInt("notExist") != 0 {
    88  			t.Errorf("Should have returned 0")
    89  		}
    90  	})
    91  }
    92  
    93  func TestLogger(t *testing.T) {
    94  	ts, _ := time.Parse(time.RFC3339, "2006-01-02T15:04:05+07:00")
    95  	type logTest struct {
    96  		Name     string
    97  		Func     func(RequestLogger)
    98  		Expected string
    99  	}
   100  	tests := []logTest{
   101  		{
   102  			Name: "EmptyishRequest",
   103  			Func: func(l RequestLogger) {
   104  				r, _ := http.NewRequest("GET", "/foo", nil)
   105  				r.RemoteAddr = "127.0.0.1:123"
   106  				l.Log(r, 200, nil)
   107  			},
   108  			Expected: `127.0.0.1 - [0001-01-01 00:00:00Z] (0s) "GET /foo HTTP/1.1" 200 0 "" ""` + "\n",
   109  		},
   110  		{
   111  			Name: "Fields",
   112  			Func: func(l RequestLogger) {
   113  				r, _ := http.NewRequest("GET", "/foo", nil)
   114  				r.RemoteAddr = "127.0.0.1:123"
   115  				r.Header.Add("User-Agent", "Bog's special browser version 1.23")
   116  				r.Header.Add("Referer", "http://somesite.com/")
   117  				l.Log(r, 200, Fields{
   118  					FieldUsername:     "bob",
   119  					FieldTimestamp:    ts,
   120  					FieldElapsedTime:  time.Duration(1234),
   121  					FieldResponseSize: 56789,
   122  				})
   123  			},
   124  			Expected: `127.0.0.1 bob [2006-01-02 15:04:05+07:00] (1.234µs) "GET /foo HTTP/1.1" 200 56789 "http://somesite.com/" "Bog's special browser version 1.23"` + "\n",
   125  		},
   126  	}
   127  	for _, test := range tests {
   128  		func(test logTest) {
   129  			t.Run(test.Name, func(t *testing.T) {
   130  				buf := &bytes.Buffer{}
   131  				l := New(buf)
   132  				test.Func(l)
   133  				if d := testy.DiffText(test.Expected, buf.String()); d != nil {
   134  					t.Error(d)
   135  				}
   136  			})
   137  		}(test)
   138  	}
   139  }
   140  

View as plain text