refactor: move httptransport w/ logging to netxlite (#411)
Part of https://github.com/ooni/probe/issues/1505
This commit is contained in:
parent
046dd4545d
commit
527e1a0707
10 changed files with 236 additions and 131 deletions
19
internal/netxmocks/http.go
Normal file
19
internal/netxmocks/http.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package netxmocks
|
||||
|
||||
import "net/http"
|
||||
|
||||
// HTTPTransport mocks netxlite.HTTPTransport.
|
||||
type HTTPTransport struct {
|
||||
MockRoundTrip func(req *http.Request) (*http.Response, error)
|
||||
MockCloseIdleConnections func()
|
||||
}
|
||||
|
||||
// RoundTrip calls MockRoundTrip.
|
||||
func (txp *HTTPTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
return txp.MockRoundTrip(req)
|
||||
}
|
||||
|
||||
// CloseIdleConnections calls MockCloseIdleConnections.
|
||||
func (txp *HTTPTransport) CloseIdleConnections() {
|
||||
txp.MockCloseIdleConnections()
|
||||
}
|
||||
38
internal/netxmocks/http_test.go
Normal file
38
internal/netxmocks/http_test.go
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
package netxmocks
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/atomicx"
|
||||
)
|
||||
|
||||
func TestHTTPTransportRoundTrip(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
txp := &HTTPTransport{
|
||||
MockRoundTrip: func(req *http.Request) (*http.Response, error) {
|
||||
return nil, expected
|
||||
},
|
||||
}
|
||||
resp, err := txp.RoundTrip(&http.Request{})
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if resp != nil {
|
||||
t.Fatal("expected nil response here")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHTTPTransportCloseIdleConnections(t *testing.T) {
|
||||
called := &atomicx.Int64{}
|
||||
txp := &HTTPTransport{
|
||||
MockCloseIdleConnections: func() {
|
||||
called.Add(1)
|
||||
},
|
||||
}
|
||||
txp.CloseIdleConnections()
|
||||
if called.Load() != 1 {
|
||||
t.Fatal("not called")
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue