Skip to content

Commit

Permalink
Coverage improvement in one package (#284)
Browse files Browse the repository at this point in the history
* Make sure the Handler handles things correctly
  • Loading branch information
pboothe committed Apr 21, 2020
1 parent 4a9b7ab commit c434c37
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
5 changes: 3 additions & 2 deletions logging/logging.go
Expand Up @@ -3,6 +3,7 @@
package logging

import (
golog "log"
"net/http"
"os"

Expand All @@ -17,7 +18,7 @@ import (
// when dockerising an Apache or Nginx instance.
var Logger = log.Logger{
Handler: json.New(os.Stderr),
Level: log.DebugLevel,
Level: log.DebugLevel,
}

// MakeAccessLogHandler wraps |handler| with another handler that logs
Expand All @@ -26,5 +27,5 @@ var Logger = log.Logger{
// access logs, because access logs are a fairly standard format that
// has been around for a long time now, so better to follow such standard.
func MakeAccessLogHandler(handler http.Handler) http.Handler {
return handlers.LoggingHandler(os.Stdout, handler)
return handlers.LoggingHandler(golog.Writer(), handler)
}
40 changes: 40 additions & 0 deletions logging/logging_test.go
@@ -0,0 +1,40 @@
package logging

import (
"bytes"
"log"
"net/http"
"testing"

"github.com/m-lab/go/httpx"
"github.com/m-lab/go/rtx"
)

type fakeHandler struct{}

func (s *fakeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
}

func TestMakeAccessLogHandler(t *testing.T) {
buff := &bytes.Buffer{}
old := log.Writer()
defer func() {
log.SetOutput(old)
}()
log.SetOutput(buff)
f := MakeAccessLogHandler(&fakeHandler{})
log.SetOutput(old)
srv := http.Server{
Addr: ":0",
Handler: f,
}
rtx.Must(httpx.ListenAndServeAsync(&srv), "Could not start server")
defer srv.Close()
_, err := http.Get("http://" + srv.Addr + "/")
rtx.Must(err, "Could not get")
s, err := buff.ReadString('\n')
if s == "" {
t.Error("We should not have had an empty string")
}
}

0 comments on commit c434c37

Please sign in to comment.