2021-02-02 12:05:47 +01:00
|
|
|
package httptransport_test
|
|
|
|
|
|
|
|
import (
|
2021-06-15 11:57:40 +02:00
|
|
|
"context"
|
2021-02-02 12:05:47 +01:00
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/apex/log"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/httptransport"
|
2021-06-15 11:57:40 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/iox"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLoggingFailure(t *testing.T) {
|
|
|
|
txp := httptransport.LoggingTransport{
|
|
|
|
Logger: log.Log,
|
|
|
|
RoundTripper: httptransport.FakeTransport{
|
|
|
|
Err: io.EOF,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
client := &http.Client{Transport: txp}
|
|
|
|
resp, err := client.Get("https://www.google.com")
|
|
|
|
if !errors.Is(err, io.EOF) {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatal("expected nil response here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoggingFailureWithNoHostHeader(t *testing.T) {
|
|
|
|
txp := httptransport.LoggingTransport{
|
|
|
|
Logger: log.Log,
|
|
|
|
RoundTripper: httptransport.FakeTransport{
|
|
|
|
Err: io.EOF,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
req := &http.Request{
|
|
|
|
Header: http.Header{},
|
|
|
|
URL: &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "www.google.com",
|
|
|
|
Path: "/",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := txp.RoundTrip(req)
|
|
|
|
if !errors.Is(err, io.EOF) {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatal("expected nil response here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoggingSuccess(t *testing.T) {
|
|
|
|
txp := httptransport.LoggingTransport{
|
|
|
|
Logger: log.Log,
|
|
|
|
RoundTripper: httptransport.FakeTransport{
|
|
|
|
Resp: &http.Response{
|
2021-06-15 14:01:45 +02:00
|
|
|
Body: io.NopCloser(strings.NewReader("")),
|
2021-02-02 12:05:47 +01:00
|
|
|
Header: http.Header{
|
|
|
|
"Server": []string{"antani/0.1.0"},
|
|
|
|
},
|
|
|
|
StatusCode: 200,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
client := &http.Client{Transport: txp}
|
|
|
|
resp, err := client.Get("https://www.google.com")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-06-15 11:57:40 +02:00
|
|
|
iox.ReadAllContext(context.Background(), resp.Body)
|
2021-02-02 12:05:47 +01:00
|
|
|
resp.Body.Close()
|
|
|
|
}
|