ooni-probe-cli/internal/engine/netx/http_test.go
Simone Basso 64bffbd941
refactor(netx): reorganize by topic (#800)
Before finishing the ongoing refactoring and leaving whatever
is left of netx in tree, I would like to restructure it so that
we'll have an easy time next time we need to modify it.

Currently, every functionality lives into the `netx.go` file and
we have a support file called `httptransport.go`.

I would like to reorganize by topic, instead. This would allow
future me to more easily perform topic-specific changes.

While there, improve `netx`'s documentation and duplicate some of
this documentation inside `internal/README.md` to provide pointers
to previous documentation, historical context, and some help to
understand the logic architecture of network extensions (aka `netx`).

Part of https://github.com/ooni/probe-cli/pull/396
2022-06-06 14:27:25 +02:00

51 lines
1.1 KiB
Go

package netx
import (
"context"
"errors"
"net"
"net/http"
"testing"
"github.com/ooni/probe-cli/v3/internal/model/mocks"
"github.com/ooni/probe-cli/v3/internal/tracex"
)
func TestNewHTTPTransportWithDialer(t *testing.T) {
expected := errors.New("mocked error")
dialer := &mocks.Dialer{
MockDialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
return nil, expected
},
}
txp := NewHTTPTransport(Config{
Dialer: dialer,
})
client := &http.Client{Transport: txp}
resp, err := client.Get("http://www.google.com")
if !errors.Is(err, expected) {
t.Fatal("not the error we expected")
}
if resp != nil {
t.Fatal("not the response we expected")
}
}
func TestNewHTTPTransportWithSaver(t *testing.T) {
saver := new(tracex.Saver)
txp := NewHTTPTransport(Config{
Saver: saver,
})
stxptxp, ok := txp.(*tracex.HTTPTransportSaver)
if !ok {
t.Fatal("not the transport we expected")
}
if stxptxp.Saver != saver {
t.Fatal("not the logger we expected")
}
if stxptxp.Saver != saver {
t.Fatal("not the logger we expected")
}
// We are going to trust the underlying type returned by netxlite
}