Skip to content

Commit

Permalink
Test request logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Seiffert authored and talam committed Dec 4, 2017
1 parent 69550cb commit 1cefc96
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions logging_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
)

func TestLoggingHandler_ServeHTTP(t *testing.T) {
ts := time.Now()

tests := []struct {
Format,
ExpectedLogMessage string
}{
{defaultRequestLoggingFormat, fmt.Sprintf("127.0.0.1 - - [%s] test-server GET - \"/foo/bar\" HTTP/1.1 \"\" 200 4 0.000\n", ts.Format("02/Jan/2006:15:04:05 -0700"))},
{"{{.RequestMethod}}", "GET\n"},
}

for _, test := range tests {
buf := bytes.NewBuffer(nil)
handler := func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("test"))
}

h := LoggingHandler(buf, http.HandlerFunc(handler), true, test.Format)

r, _ := http.NewRequest("GET", "/foo/bar", nil)
r.RemoteAddr = "127.0.0.1"
r.Host = "test-server"

h.ServeHTTP(httptest.NewRecorder(), r)

actual := buf.String()
if actual != test.ExpectedLogMessage {
t.Errorf("Log message was\n%s\ninstead of expected \n%s", actual, test.ExpectedLogMessage)
}
}
}

0 comments on commit 1cefc96

Please sign in to comment.