15652: Eliminate a malloc when writing a one-segment block.
[arvados.git] / sdk / go / httpserver / logger_test.go
index bbcafa143957ae0a165840e58336f52336b8d919..3b2bc7758069b44345b3da522b8f80cc303c52fe 100644 (file)
@@ -6,14 +6,15 @@ package httpserver
 
 import (
        "bytes"
+       "context"
        "encoding/json"
        "net/http"
        "net/http/httptest"
-       "os"
        "testing"
        "time"
 
-       log "github.com/Sirupsen/logrus"
+       "git.curoverse.com/arvados.git/sdk/go/ctxlog"
+       "github.com/sirupsen/logrus"
        check "gopkg.in/check.v1"
 )
 
@@ -26,20 +27,25 @@ var _ = check.Suite(&Suite{})
 type Suite struct{}
 
 func (s *Suite) TestLogRequests(c *check.C) {
-       defer log.SetOutput(os.Stdout)
        captured := &bytes.Buffer{}
-       log.SetOutput(captured)
-       log.SetFormatter(&log.JSONFormatter{
+       log := logrus.New()
+       log.Out = captured
+       log.Formatter = &logrus.JSONFormatter{
                TimestampFormat: time.RFC3339Nano,
-       })
-       h := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
-               w.Write([]byte("hello world"))
-       })
+       }
+       ctx := ctxlog.Context(context.Background(), log)
+
+       h := AddRequestIDs(LogRequests(
+               http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
+                       w.Write([]byte("hello world"))
+               })))
+
        req, err := http.NewRequest("GET", "https://foo.example/bar", nil)
        req.Header.Set("X-Forwarded-For", "1.2.3.4:12345")
        c.Assert(err, check.IsNil)
        resp := httptest.NewRecorder()
-       AddRequestIDs(LogRequests(h)).ServeHTTP(resp, req)
+
+       HandlerWithContext(ctx, h).ServeHTTP(resp, req)
 
        dec := json.NewDecoder(captured)