github.com/thiagoyeds/go-cloud@v0.26.0/server/requestlog/ncsa_test.go (about)

     1  // Copyright 2018 The Go Cloud Development Kit Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package requestlog
    16  
    17  import (
    18  	"bytes"
    19  	"testing"
    20  	"time"
    21  )
    22  
    23  var _ Logger = (*NCSALogger)(nil)
    24  
    25  func TestNCSALog(t *testing.T) {
    26  	const (
    27  		startTime      = 1507914000
    28  		startTimeNanos = 512
    29  
    30  		latencySec   = 5
    31  		latencyNanos = 123456789
    32  
    33  		endTime      = startTime + latencySec
    34  		endTimeNanos = startTimeNanos + latencyNanos
    35  	)
    36  	tests := []struct {
    37  		name string
    38  		ent  Entry
    39  		want string
    40  	}{
    41  		{
    42  			name: "AllFields",
    43  			ent: Entry{
    44  				ReceivedTime:       time.Unix(startTime, startTimeNanos).UTC(),
    45  				RequestMethod:      "POST",
    46  				RequestURL:         "/foo/bar",
    47  				RequestHeaderSize:  456,
    48  				RequestBodySize:    123000,
    49  				UserAgent:          "Chrome proxied through Firefox and Edge",
    50  				Referer:            "http://www.example.com/",
    51  				Proto:              "HTTP/1.1",
    52  				RemoteIP:           "12.34.56.78",
    53  				ServerIP:           "127.0.0.1",
    54  				Status:             404,
    55  				ResponseHeaderSize: 555,
    56  				ResponseBodySize:   789000,
    57  				Latency:            latencySec*time.Second + latencyNanos*time.Nanosecond,
    58  			},
    59  			want: `12.34.56.78 - - [13/Oct/2017:17:00:00 +0000] "POST /foo/bar HTTP/1.1" 404 789000 "http://www.example.com/" "Chrome proxied through Firefox and Edge"` + "\n",
    60  		},
    61  		{
    62  			name: "OnlyRequiredFields",
    63  			ent: Entry{
    64  				ReceivedTime:  time.Unix(startTime, startTimeNanos).UTC(),
    65  				RequestMethod: "POST",
    66  				RequestURL:    "/foo/bar",
    67  				Proto:         "HTTP/1.1",
    68  				Status:        404,
    69  			},
    70  			want: `- - - [13/Oct/2017:17:00:00 +0000] "POST /foo/bar HTTP/1.1" 404 0 "" ""` + "\n",
    71  		},
    72  		{
    73  			name: "OnlyRequiredFieldsAndUserAgent",
    74  			ent: Entry{
    75  				ReceivedTime:  time.Unix(startTime, startTimeNanos).UTC(),
    76  				RequestMethod: "POST",
    77  				RequestURL:    "/foo/bar",
    78  				Proto:         "HTTP/1.1",
    79  				Status:        404,
    80  				UserAgent:     "Chrome proxied through Firefox and Edge",
    81  			},
    82  			want: `- - - [13/Oct/2017:17:00:00 +0000] "POST /foo/bar HTTP/1.1" 404 0 "" "Chrome proxied through Firefox and Edge"` + "\n",
    83  		},
    84  		{
    85  			name: "DoubleQuotesInUserAgent",
    86  			ent: Entry{
    87  				ReceivedTime:  time.Unix(startTime, startTimeNanos).UTC(),
    88  				RequestMethod: "POST",
    89  				RequestURL:    "/foo/bar",
    90  				Proto:         "HTTP/1.1",
    91  				Status:        404,
    92  				UserAgent:     "Chrome \"proxied\" through Firefox and Edge",
    93  			},
    94  			want: `- - - [13/Oct/2017:17:00:00 +0000] "POST /foo/bar HTTP/1.1" 404 0 "" "Chrome \"proxied\" through Firefox and Edge"` + "\n",
    95  		},
    96  	}
    97  	for _, test := range tests {
    98  		t.Run(test.name, func(t *testing.T) {
    99  			buf := new(bytes.Buffer)
   100  			var logErr error
   101  			l := NewNCSALogger(buf, func(e error) { logErr = e })
   102  			l.Log(&test.ent)
   103  			if logErr != nil {
   104  				t.Error("Logger called error callback:", logErr)
   105  			}
   106  			got := buf.String()
   107  			if got != test.want {
   108  				t.Errorf("Log(...) wrote %q; want %q", got, test.want)
   109  			}
   110  		})
   111  	}
   112  }