ooni-probe-cli/internal/engine/legacy/netxlogger/netxlogger_test.go
Simone Basso 6d3a4f1db8
refactor: merge dnsx and errorsx into netxlite (#517)
When preparing a tutorial for netxlite, I figured it is easier
to tell people "hey, this is the package you should use for all
low-level networking stuff" rather than introducing people to
a set of packages working together where some piece of functionality
is here and some other piece is there.

Part of https://github.com/ooni/probe/issues/1591
2021-09-28 12:42:01 +02:00

42 lines
1023 B
Go

package netxlogger
import (
"context"
"net/http"
"testing"
"time"
"github.com/apex/log"
"github.com/apex/log/handlers/discard"
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx"
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/modelx"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
func TestGood(t *testing.T) {
log.SetHandler(discard.Default)
client := netx.NewHTTPClient()
client.ConfigureDNS("udp", "dns.google.com:53")
req, err := http.NewRequest("GET", "http://www.facebook.com", nil)
if err != nil {
t.Fatal(err)
}
req = req.WithContext(modelx.WithMeasurementRoot(req.Context(), &modelx.MeasurementRoot{
Beginning: time.Now(),
Handler: NewHandler(log.Log),
}))
resp, err := client.HTTPClient.Do(req)
if err != nil {
t.Fatal(err)
}
if resp == nil {
t.Fatal("expected non-nil resp here")
}
defer resp.Body.Close()
_, err = netxlite.ReadAllContext(context.Background(), resp.Body)
if err != nil {
t.Fatal(err)
}
client.HTTPClient.CloseIdleConnections()
}