Tests: Add httpclient tests #98
This commit is contained in:
parent
0cc9dcb2b9
commit
dea527591e
2 changed files with 31 additions and 34 deletions
|
@ -10,20 +10,13 @@ import (
|
||||||
|
|
||||||
var log = event.Log
|
var log = event.Log
|
||||||
|
|
||||||
var httpclient *http.Client
|
|
||||||
|
|
||||||
func Client(debug bool) *http.Client {
|
func Client(debug bool) *http.Client {
|
||||||
if httpclient != nil {
|
|
||||||
return httpclient
|
|
||||||
}
|
|
||||||
if debug {
|
if debug {
|
||||||
httpclient = &http.Client{
|
return &http.Client{
|
||||||
Transport: LoggingRoundTripper{http.DefaultTransport},
|
Transport: LoggingRoundTripper{http.DefaultTransport},
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
httpclient = http.DefaultClient
|
|
||||||
}
|
}
|
||||||
return httpclient
|
return http.DefaultClient
|
||||||
}
|
}
|
||||||
|
|
||||||
// This type implements the http.RoundTripper interface
|
// This type implements the http.RoundTripper interface
|
||||||
|
@ -44,21 +37,7 @@ func (lrt LoggingRoundTripper) RoundTrip(req *http.Request) (res *http.Response,
|
||||||
} else {
|
} else {
|
||||||
log.Debugf("Received %v response\n", res.Status)
|
log.Debugf("Received %v response\n", res.Status)
|
||||||
|
|
||||||
//buf := bytes.NewBuffer(make([]byte, 0, 1024))
|
// Copy body into buffer for logging
|
||||||
//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
|
|
||||||
|
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
_, err := io.Copy(buf, res.Body)
|
_, err := io.Copy(buf, res.Body)
|
||||||
if err != nil {
|
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())
|
log.Debugf("Reponse Body: %s\n", buf.String())
|
||||||
res.Body = io.NopCloser(buf)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
28
internal/httpclient/httpclient_test.go
Normal file
28
internal/httpclient/httpclient_test.go
Normal file
|
@ -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)
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in a new issue