func TestSetValues(t *testing.T) { buf := &bytes.Buffer{} handler := weblogs.HandlerWithOptions( &handler{Field1: "one", Field2: "two"}, &weblogs.Options{Writer: buf, Logger: setLogger{}}) handler.ServeHTTP( kNilResponseWriter, newRequest("192.168.5.1", "GET", "/foo/bar?query=tall")) expected := "field1=one field2=two\n" verifyLogs(t, expected, buf.String()) }
func TestNormalLogs(t *testing.T) { buf := &bytes.Buffer{} clock := &clock{Time: kTime} handler := weblogs.HandlerWithOptions( &handler{Status: 321, Clock: clock, ElapsedMillis: 387}, &weblogs.Options{Writer: buf, Now: clock.Now()}) handler.ServeHTTP( kNilResponseWriter, newRequest("192.168.5.1:3333", "GET", "/foo/bar?query=tall")) expected := "03/23/2013 13:14:15 192.168.5.1 GET /foo/bar?query=tall 321 387\n" verifyLogs(t, expected, buf.String()) }
func TestAppendedLogs(t *testing.T) { buf := &bytes.Buffer{} clock := &clock{Time: kTime} handler := weblogs.HandlerWithOptions( &handler{Status: 321, LogExtra: "behere"}, &weblogs.Options{Writer: buf, Now: clock.Now()}) handler.ServeHTTP( kNilResponseWriter, newRequest("192.168.5.1", "GET", "/foo/bar?query=tall")) expected := "03/23/2013 13:14:15 192.168.5.1 GET /foo/bar?query=tall 321 0 behere\n" verifyLogs(t, expected, buf.String()) }
func TestSend500OnNoOutput(t *testing.T) { buf := &bytes.Buffer{} clock := &clock{Time: kTime} handler := weblogs.HandlerWithOptions( &handler{LogExtra: "behere", Clock: clock, ElapsedMillis: 23}, &weblogs.Options{Writer: buf, Now: clock.Now()}) w := &spyResponseWriter{} handler.ServeHTTP( w, newRequest("192.168.5.1:3333", "GET", "/foo/bar?query=tall")) expected := "03/23/2013 13:14:15 192.168.5.1 GET /foo/bar?query=tall 500 23 behere\n" verifyLogs(t, expected, buf.String()) if w.Status != 500 { t.Errorf("Expected 500 error to be sent, but %d was sent.", w.Status) } }
func TestLogStatus200(t *testing.T) { buf := &bytes.Buffer{} clock := &clock{Time: kTime} handler := weblogs.HandlerWithOptions( &handler{Message: "1234567"}, &weblogs.Options{ Writer: buf, Logger: weblogs.ApacheCommonLogger(), Now: clock.Now()}) request := newRequest("192.168.5.1:3333", "GET", "/foo/bar?query=tall") request.URL.User = url.User("fred") handler.ServeHTTP( kNilResponseWriter, request) expected := "192.168.5.1 - fred [23/Mar/2013:13:14:15 +0000] \"GET /foo/bar?query=tall HTTP/1.0\" 200 7\n" verifyLogs(t, expected, buf.String()) }
func TestRaces(t *testing.T) { buf := &bytes.Buffer{} clock := &clock{Time: kTime} handler := weblogs.HandlerWithOptions( &handler{Status: 200}, &weblogs.Options{ Writer: buf, Logger: verySimpleLogger{}, Now: clock.Now()}) var wg sync.WaitGroup wg.Add(20) for i := 0; i < 20; i++ { go func() { handler.ServeHTTP( kNilResponseWriter, newRequest("192.168.5.1:3333", "GET", "/foo/bar?query=tall")) wg.Done() }() } wg.Wait() }
func TestCombinedLogs(t *testing.T) { buf := &bytes.Buffer{} clock := &clock{Time: kTime} handler := weblogs.HandlerWithOptions( &handler{Status: 321, Message: "1234567"}, &weblogs.Options{ Writer: buf, Logger: weblogs.ApacheCombinedLogger(), Now: clock.Now()}) request := newRequest("192.168.5.1:3333", "GET", "/foo/bar?query=tall") request.URL.User = url.User("fred") request.Header = make(http.Header) request.Header.Set("Referer", "referer") request.Header.Set("User-Agent", "useragent") handler.ServeHTTP( kNilResponseWriter, request) expected := "192.168.5.1 - fred [23/Mar/2013:13:14:15 +0000] \"GET /foo/bar?query=tall HTTP/1.0\" 321 7 \"referer\" \"useragent\"\n" verifyLogs(t, expected, buf.String()) }