From dea527591e53e74d8540856c6af2247d0f6f1055 Mon Sep 17 00:00:00 2001 From: Timo Volkmann Date: Wed, 8 Sep 2021 17:46:30 +0200 Subject: [PATCH] Tests: Add httpclient tests #98 --- internal/httpclient/httpclient.go | 37 +++----------------------- internal/httpclient/httpclient_test.go | 28 +++++++++++++++++++ 2 files changed, 31 insertions(+), 34 deletions(-) create mode 100644 internal/httpclient/httpclient_test.go diff --git a/internal/httpclient/httpclient.go b/internal/httpclient/httpclient.go index dadbd1987..15d1ae0af 100644 --- a/internal/httpclient/httpclient.go +++ b/internal/httpclient/httpclient.go @@ -10,20 +10,13 @@ import ( var log = event.Log -var httpclient *http.Client - func Client(debug bool) *http.Client { - if httpclient != nil { - return httpclient - } if debug { - httpclient = &http.Client{ + return &http.Client{ Transport: LoggingRoundTripper{http.DefaultTransport}, } - } else { - httpclient = http.DefaultClient } - return httpclient + return http.DefaultClient } // This type implements the http.RoundTripper interface @@ -44,21 +37,7 @@ func (lrt LoggingRoundTripper) RoundTrip(req *http.Request) (res *http.Response, } else { log.Debugf("Received %v response\n", res.Status) - //buf := bytes.NewBuffer(make([]byte, 0, 1024)) - //r := io.TeeReader(res.Body, buf) - //all, err := io.ReadAll(r) - //log.Debugf("Body: %s\nError: %s\n", all, err) - //res.Body = io.NopCloser(buf) - - //pipeReader, pipeWriter := io.Pipe() - //r := io.TeeReader(res.Body, pipeWriter) - //go func() { - // all, err := io.ReadAll(r) - // pipeWriter.CloseWithError(err) - // log.Debugf("Body\n%s\n%s", all, err) - //}() - //res.Body = pipeReader - + // Copy body into buffer for logging buf := new(bytes.Buffer) _, err := io.Copy(buf, res.Body) if err != nil { @@ -66,16 +45,6 @@ func (lrt LoggingRoundTripper) RoundTrip(req *http.Request) (res *http.Response, } log.Debugf("Reponse Body: %s\n", buf.String()) res.Body = io.NopCloser(buf) - - //all, err := io.ReadAll(io.NopCloser(res.Body)) - // - //if err != nil { - // log.Errorf("Error: %v", err) - //} - ////io.Teereader, io.MultiReader() - //log.Debugf("Body\n%s\n", all) - // - //res.Body = io.NopCloser(bytes.NewReader(all)) } return } diff --git a/internal/httpclient/httpclient_test.go b/internal/httpclient/httpclient_test.go new file mode 100644 index 000000000..530cb82a2 --- /dev/null +++ b/internal/httpclient/httpclient_test.go @@ -0,0 +1,28 @@ +package httpclient + +import ( + "github.com/stretchr/testify/assert" + "net/http" + "testing" +) + +func TestClient(t *testing.T) { + t.Run("default client", func(t *testing.T) { + client := Client(false) + + assert.IsType(t, http.DefaultClient, client) + assert.IsType(t, nil, client.Transport) + }) + t.Run("logging proxy client", func(t *testing.T) { + client := Client(true) + + assert.IsType(t, LoggingRoundTripper{}, client.Transport) + }) + t.Run("RoundTripper working", func(t *testing.T) { + req, err := http.NewRequest("GET", "https://photoprism.app", nil) + assert.Nil(t, err) + rt := LoggingRoundTripper{http.DefaultTransport} + _, err = rt.RoundTrip(req) + assert.Nil(t, err) + }) +}