6d3a4f1db8
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
42 lines
939 B
Go
42 lines
939 B
Go
package oldhttptransport
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
|
)
|
|
|
|
func TestBodyTracerSuccess(t *testing.T) {
|
|
client := &http.Client{
|
|
Transport: NewBodyTracer(http.DefaultTransport),
|
|
}
|
|
resp, err := client.Get("https://www.google.com")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer resp.Body.Close()
|
|
_, err = netxlite.ReadAllContext(context.Background(), resp.Body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
client.CloseIdleConnections()
|
|
}
|
|
|
|
func TestBodyTracerFailure(t *testing.T) {
|
|
client := &http.Client{
|
|
Transport: NewBodyTracer(http.DefaultTransport),
|
|
}
|
|
// This fails the request because we attempt to speak cleartext HTTP with
|
|
// a server that instead is expecting TLS.
|
|
resp, err := client.Get("http://www.google.com:443")
|
|
if err == nil {
|
|
t.Fatal("expected an error here")
|
|
}
|
|
if resp != nil {
|
|
t.Fatal("expected a nil response here")
|
|
}
|
|
client.CloseIdleConnections()
|
|
}
|