Repository: fsouza/fake-gcs-server
Pull request #155 (2019-12-28 13:32:19)

fakestorage: add logging to server
It helps debugging requests to server easier.

cuonglm: Hey @fsouza, a gentle ping 😄 

Patch:
diff --git a/fakestorage/server.go b/fakestorage/server.go
index 5fd9aac..2676cbd 100644
--- a/fakestorage/server.go
+++ b/fakestorage/server.go
@@ -8,6 +8,7 @@ import (
 	"context"
 	"crypto/tls"
 	"fmt"
+	"io"
 	"net"
 	"net/http"
 	"net/http/httptest"
@@ -15,6 +16,7 @@ import (
 
 	"cloud.google.com/go/storage"
 	"github.com/fsouza/fake-gcs-server/internal/backend"
+	"github.com/gorilla/handlers"
 	"github.com/gorilla/mux"
 	"google.golang.org/api/option"
 )
@@ -77,6 +79,9 @@ type Options struct {
 	// https://<bucket>.storage.gcs.127.0.0.1.nip.io:4443>/<bucket>/<object>
 	// If unset, the default is "storage.googleapis.com", the XML API
 	PublicHost string
+
+	// Destination for writing log.
+	Writer io.Writer
 }
 
 // NewServerWithOptions creates a new server with custom options
@@ -90,7 +95,12 @@ func NewServerWithOptions(options Options) (*Server, error) {
 		return s, nil
 	}
 
-	s.ts = httptest.NewUnstartedServer(s.mux)
+	var handler http.Handler = s.mux
+	if options.Writer != nil {
+		handler = handlers.LoggingHandler(options.Writer, s.mux)
+	}
+
+	s.ts = httptest.NewUnstartedServer(handler)
 	startFunc := s.ts.StartTLS
 	if options.Scheme == "http" {
 		startFunc = s.ts.Start
diff --git a/go.mod b/go.mod
index dfcc96a..7171872 100644
--- a/go.mod
+++ b/go.mod
@@ -3,6 +3,7 @@ module github.com/fsouza/fake-gcs-server
 require (
 	cloud.google.com/go/storage v1.5.0
 	github.com/google/go-cmp v0.4.0
+	github.com/gorilla/handlers v1.4.2
 	github.com/gorilla/mux v1.7.3
 	github.com/sirupsen/logrus v1.4.2
 	google.golang.org/api v0.15.0
diff --git a/go.sum b/go.sum
index e247d49..b16feb9 100644
--- a/go.sum
+++ b/go.sum
@@ -69,6 +69,8 @@ github.com/googleapis/gax-go/v2 v2.0.4 h1:hU4mGcQI4DaAYW+IbTun+2qEZVFxK0ySjQLTbS
 github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
 github.com/googleapis/gax-go/v2 v2.0.5 h1:sjZBwGj9Jlw33ImPtvFviGYvseOtDM7hkSKB7+Tv3SM=
 github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
+github.com/gorilla/handlers v1.4.2 h1:0QniY0USkHQ1RGCLfKxeNHK9bkDHGRYGNDFBCS+YARg=
+github.com/gorilla/handlers v1.4.2/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/OAirnOIQ=
 github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
 github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
 github.com/hashicorp/golang-lru v0.5.0 h1:CL2msUPvZTLb5O648aiLNJw3hnBxN2+1Jq8rCOH9wdo=


Test patch:
diff --git a/fakestorage/server_test.go b/fakestorage/server_test.go
index e5c1ed8..a052b85 100644
--- a/fakestorage/server_test.go
+++ b/fakestorage/server_test.go
@@ -5,6 +5,8 @@
 package fakestorage
 
 import (
+	"bytes"
+	"io"
 	"io/ioutil"
 	"net/http"
 	"testing"
@@ -51,6 +53,31 @@ func TestNewServerExternalHost(t *testing.T) {
 	}
 }
 
+func TestNewServerLogging(t *testing.T) {
+	t.Parallel()
+	buf := new(bytes.Buffer)
+	server, err := NewServerWithOptions(Options{Writer: buf})
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	defer server.Stop()
+	client := server.HTTPClient()
+	req, err := http.NewRequest(http.MethodGet, "https://storage.googleapis.com/", nil)
+	if err != nil {
+		t.Fatal(err)
+	}
+	res, err := client.Do(req)
+	if err != nil {
+		t.Fatal(err)
+	}
+	defer res.Body.Close()
+	io.Copy(ioutil.Discard, res.Body)
+	if buf.Len() == 0 {
+		t.Error("Log was not written to buffer.")
+	}
+}
+
 func TestDownloadObject(t *testing.T) {
 	objs := []Object{
 		{BucketName: "some-bucket", Name: "files/txt/text-01.txt", Content: []byte("something")},