2021-02-02 12:05:47 +01:00
|
|
|
package oldhttptransport
|
|
|
|
|
|
|
|
import (
|
2021-06-15 11:57:40 +02:00
|
|
|
"context"
|
2021-02-02 12:05:47 +01:00
|
|
|
"net/http"
|
|
|
|
"testing"
|
2021-06-15 11:57:40 +02:00
|
|
|
|
2021-09-05 14:49:38 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite/iox"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
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()
|
2021-06-15 11:57:40 +02:00
|
|
|
_, err = iox.ReadAllContext(context.Background(), resp.Body)
|
2021-02-02 12:05:47 +01:00
|
|
|
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()
|
|
|
|
}
|