Fixed remote address logging and added appropriate unit tests.

This commit is contained in:
Jeromy Streets 2019-04-08 12:23:29 -07:00
parent 95b3a1c7d3
commit 7793fde0ca
2 changed files with 41 additions and 16 deletions

View File

@ -49,15 +49,29 @@ func WithReferrers(serveFile FileServerFunc, referrers []string) FileServerFunc
// to serving the requested file. // to serving the requested file.
func WithLogging(serveFile FileServerFunc) FileServerFunc { func WithLogging(serveFile FileServerFunc) FileServerFunc {
return func(w http.ResponseWriter, r *http.Request, name string) { return func(w http.ResponseWriter, r *http.Request, name string) {
log.Printf( referer := r.Referer()
"REQ from '%s': %s %s %s%s -> %s\n", if 0 == len(referer) {
r.Referer(), log.Printf(
r.Method, "REQ from '%s': %s %s %s%s -> %s\n",
r.Proto, r.RemoteAddr,
r.Host, r.Method,
r.URL.Path, r.Proto,
name, r.Host,
) r.URL.Path,
name,
)
} else {
log.Printf(
"REQ from '%s' (REFERER: '%s'): %s %s %s%s -> %s\n",
r.RemoteAddr,
referer,
r.Method,
r.Proto,
r.Host,
r.URL.Path,
name,
)
}
serveFile(w, r, name) serveFile(w, r, name)
} }
} }

View File

@ -152,19 +152,29 @@ func TestWithReferrers(t *testing.T) {
} }
func TestBasicWithAndWithoutLogging(t *testing.T) { func TestBasicWithAndWithoutLogging(t *testing.T) {
referer := "http://localhost"
noReferer := ""
testCases := []struct { testCases := []struct {
name string name string
path string path string
code int code int
refer string
contents string contents string
}{ }{
{"Good base dir", "", ok, tmpIndex}, {"Good base dir", "", ok, referer, tmpIndex},
{"Good base index", tmpIndexName, redirect, nothing}, {"Good base index", tmpIndexName, redirect, referer, nothing},
{"Good base file", tmpFileName, ok, tmpFile}, {"Good base file", tmpFileName, ok, referer, tmpFile},
{"Bad base file", tmpBadName, missing, notFound}, {"Bad base file", tmpBadName, missing, referer, notFound},
{"Good subdir dir", subDir, ok, tmpSubIndex}, {"Good subdir dir", subDir, ok, referer, tmpSubIndex},
{"Good subdir index", tmpSubIndexName, redirect, nothing}, {"Good subdir index", tmpSubIndexName, redirect, referer, nothing},
{"Good subdir file", tmpSubFileName, ok, tmpSubFile}, {"Good subdir file", tmpSubFileName, ok, referer, tmpSubFile},
{"Good base dir", "", ok, noReferer, tmpIndex},
{"Good base index", tmpIndexName, redirect, noReferer, nothing},
{"Good base file", tmpFileName, ok, noReferer, tmpFile},
{"Bad base file", tmpBadName, missing, noReferer, notFound},
{"Good subdir dir", subDir, ok, noReferer, tmpSubIndex},
{"Good subdir index", tmpSubIndexName, redirect, noReferer, nothing},
{"Good subdir file", tmpSubFileName, ok, noReferer, tmpSubFile},
} }
for _, serveFile := range serveFileFuncs { for _, serveFile := range serveFileFuncs {
@ -173,6 +183,7 @@ func TestBasicWithAndWithoutLogging(t *testing.T) {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
fullpath := "http://localhost/" + tc.path fullpath := "http://localhost/" + tc.path
req := httptest.NewRequest("GET", fullpath, nil) req := httptest.NewRequest("GET", fullpath, nil)
req.Header.Add("Referer", tc.refer)
w := httptest.NewRecorder() w := httptest.NewRecorder()
handler(w, req) handler(w, req)