1package log
2
3import (
4	"io/ioutil"
5	"testing"
6	"time"
7
8	log "github.com/sirupsen/logrus"
9	"github.com/stretchr/testify/assert"
10)
11
12func TestAccessLogFormatter_Format(t *testing.T) {
13	discardLogger := log.New()
14	discardLogger.Out = ioutil.Discard
15
16	tests := []struct {
17		name  string
18		entry *log.Entry
19		want  string
20	}{
21		{
22			"blank",
23			discardLogger.WithField("blank", ""),
24			"-  - - [2018/01/07:00:00:00 +0000] \"  \" 0 0 \"\" \"\" 0\n",
25		},
26		{
27			"full",
28			discardLogger.WithFields(log.Fields{
29				httpHostField:               "gitlab.com",
30				httpRemoteIPField:           "127.0.0.1",
31				httpRequestMethodField:      "GET",
32				httpURIField:                "/",
33				httpProtoField:              "HTTP/1.1",
34				httpResponseStatusCodeField: 200,
35				httpResponseSizeField:       100,
36				httpRequestReferrerField:    "http://localhost",
37				httpUserAgentField:          "Mozilla/1.0",
38				requestDurationField:        5,
39			}),
40			"gitlab.com 127.0.0.1 - - [2018/01/07:00:00:00 +0000] \"GET / HTTP/1.1\" 200 100 \"http://localhost\" \"Mozilla/1.0\" 5\n",
41		},
42	}
43
44	for _, tt := range tests {
45		t.Run(tt.name, func(t *testing.T) {
46			f := &combinedAccessLogFormatter{clock: &stubClock{time.Unix(1515283200, 0).UTC()}}
47
48			got, err := f.Format(tt.entry)
49			if err != nil {
50				t.Errorf("AccessLogFormatter.Format() error = %v", err)
51				return
52			}
53
54			assert.Equal(t, tt.want, string(got))
55		})
56	}
57}
58