refactor(netx): move construction logic outside package (#798)
For testability, replace most if-based construction logic with calls to well-tested factories living in other packages. While there, acknowledge that a bunch of types could now be private and make them private, modifying the code to call the public factories allowing to construct said types instead. Part of https://github.com/ooni/probe/issues/2121
This commit is contained in:
@@ -14,6 +14,20 @@ import (
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
||||
)
|
||||
|
||||
// MaybeWrapHTTPTransport wraps the HTTPTransport to save events if this Saver
|
||||
// is not nil and otherwise just returns the given HTTPTransport. The snapshotSize
|
||||
// argument is the maximum response body snapshot size to save per response.
|
||||
func (s *Saver) MaybeWrapHTTPTransport(txp model.HTTPTransport, snapshotSize int64) model.HTTPTransport {
|
||||
if s != nil {
|
||||
txp = &HTTPTransportSaver{
|
||||
HTTPTransport: txp,
|
||||
Saver: s,
|
||||
SnapshotSize: snapshotSize,
|
||||
}
|
||||
}
|
||||
return txp
|
||||
}
|
||||
|
||||
// httpCloneRequestHeaders returns a clone of the headers where we have
|
||||
// also set the host header, which normally is not set by
|
||||
// golang until it serializes the request itself.
|
||||
|
||||
@@ -15,6 +15,32 @@ import (
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite/filtering"
|
||||
)
|
||||
|
||||
func TestMaybeWrapHTTPTransport(t *testing.T) {
|
||||
const snapshotSize = 1024
|
||||
|
||||
t.Run("with non-nil saver", func(t *testing.T) {
|
||||
saver := &Saver{}
|
||||
underlying := &mocks.HTTPTransport{}
|
||||
txp := saver.MaybeWrapHTTPTransport(underlying, snapshotSize)
|
||||
realTxp := txp.(*HTTPTransportSaver)
|
||||
if realTxp.HTTPTransport != underlying {
|
||||
t.Fatal("unexpected result")
|
||||
}
|
||||
if realTxp.SnapshotSize != snapshotSize {
|
||||
t.Fatal("did not set snapshotSize correctly")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("with nil saver", func(t *testing.T) {
|
||||
var saver *Saver
|
||||
underlying := &mocks.HTTPTransport{}
|
||||
txp := saver.MaybeWrapHTTPTransport(underlying, snapshotSize)
|
||||
if txp != underlying {
|
||||
t.Fatal("unexpected result")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestHTTPTransportSaver(t *testing.T) {
|
||||
|
||||
t.Run("CloseIdleConnections", func(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user