2022-01-05 12:48:32 +01:00
|
|
|
package httpx
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2022-01-05 12:48:32 +01:00
|
|
|
"net/http/httptest"
|
2021-02-02 12:05:47 +01:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/apex/log"
|
2022-01-05 14:49:31 +01:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/fakefill"
|
2022-01-05 14:15:42 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const userAgent = "miniooni/0.1.0-dev"
|
|
|
|
|
2022-01-05 14:15:42 +01:00
|
|
|
func TestAPIClientTemplate(t *testing.T) {
|
|
|
|
t.Run("normal constructor", func(t *testing.T) {
|
2022-01-05 14:49:31 +01:00
|
|
|
// Implementation note: the fakefiller will ignore the
|
|
|
|
// fields it does not know how to fill, so we are filling
|
|
|
|
// those fields with plausible values in advance
|
2022-01-05 14:15:42 +01:00
|
|
|
tmpl := &APIClientTemplate{
|
2022-01-05 14:49:31 +01:00
|
|
|
HTTPClient: http.DefaultClient,
|
|
|
|
Logger: model.DiscardLogger,
|
2022-01-05 14:15:42 +01:00
|
|
|
}
|
2022-01-05 14:49:31 +01:00
|
|
|
ff := &fakefill.Filler{}
|
|
|
|
ff.Fill(tmpl)
|
2022-01-05 14:15:42 +01:00
|
|
|
ac := tmpl.Build()
|
2022-01-05 14:49:31 +01:00
|
|
|
orig := apiClient(*tmpl)
|
|
|
|
if diff := cmp.Diff(&orig, ac); diff != "" {
|
|
|
|
t.Fatal(diff)
|
2022-01-05 14:15:42 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("constructor with authorization", func(t *testing.T) {
|
2022-01-05 14:49:31 +01:00
|
|
|
// Implementation note: the fakefiller will ignore the
|
|
|
|
// fields it does not know how to fill, so we are filling
|
|
|
|
// those fields with plausible values in advance
|
2022-01-05 14:15:42 +01:00
|
|
|
tmpl := &APIClientTemplate{
|
2022-01-05 14:49:31 +01:00
|
|
|
HTTPClient: http.DefaultClient,
|
|
|
|
Logger: model.DiscardLogger,
|
2022-01-05 14:15:42 +01:00
|
|
|
}
|
2022-01-05 14:49:31 +01:00
|
|
|
ff := &fakefill.Filler{}
|
|
|
|
ff.Fill(tmpl)
|
|
|
|
tok := ""
|
|
|
|
ff.Fill(&tok)
|
|
|
|
ac := tmpl.BuildWithAuthorization(tok)
|
|
|
|
// the authorization should be different now
|
|
|
|
if tmpl.Authorization == ac.(*apiClient).Authorization {
|
|
|
|
t.Fatal("we expect Authorization to be different")
|
2022-01-05 14:15:42 +01:00
|
|
|
}
|
2022-01-05 14:49:31 +01:00
|
|
|
// clear authorization for the comparison
|
|
|
|
tmpl.Authorization = ""
|
|
|
|
ac.(*apiClient).Authorization = ""
|
|
|
|
orig := apiClient(*tmpl)
|
|
|
|
if diff := cmp.Diff(&orig, ac); diff != "" {
|
|
|
|
t.Fatal(diff)
|
2022-01-05 14:15:42 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func newClient() *apiClient {
|
|
|
|
return &apiClient{
|
2021-02-02 12:05:47 +01:00
|
|
|
BaseURL: "https://httpbin.org",
|
|
|
|
HTTPClient: http.DefaultClient,
|
|
|
|
Logger: log.Log,
|
|
|
|
UserAgent: userAgent,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewRequestWithJSONBodyJSONMarshalFailure(t *testing.T) {
|
|
|
|
client := newClient()
|
2022-01-05 12:48:32 +01:00
|
|
|
req, err := client.newRequestWithJSONBody(
|
2021-02-02 12:05:47 +01:00
|
|
|
context.Background(), "GET", "/", nil, make(chan interface{}),
|
|
|
|
)
|
|
|
|
if err == nil || !strings.HasPrefix(err.Error(), "json: unsupported type") {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if req != nil {
|
|
|
|
t.Fatal("expected nil request here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewRequestWithJSONBodyNewRequestFailure(t *testing.T) {
|
|
|
|
client := newClient()
|
|
|
|
client.BaseURL = "\t\t\t" // cause URL parse error
|
2022-01-05 12:48:32 +01:00
|
|
|
req, err := client.newRequestWithJSONBody(
|
2021-02-02 12:05:47 +01:00
|
|
|
context.Background(), "GET", "/", nil, nil,
|
|
|
|
)
|
|
|
|
if err == nil || !strings.HasSuffix(err.Error(), "invalid control character in URL") {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if req != nil {
|
|
|
|
t.Fatal("expected nil request here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewRequestWithQuery(t *testing.T) {
|
|
|
|
client := newClient()
|
|
|
|
q := url.Values{}
|
|
|
|
q.Add("antani", "mascetti")
|
|
|
|
q.Add("melandri", "conte")
|
2022-01-05 12:48:32 +01:00
|
|
|
req, err := client.newRequest(
|
2021-02-02 12:05:47 +01:00
|
|
|
context.Background(), "GET", "/", q, nil,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if req.URL.Query().Get("antani") != "mascetti" {
|
|
|
|
t.Fatal("expected different query string here")
|
|
|
|
}
|
|
|
|
if req.URL.Query().Get("melandri") != "conte" {
|
|
|
|
t.Fatal("expected different query string here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewRequestNewRequestFailure(t *testing.T) {
|
|
|
|
client := newClient()
|
2022-01-05 12:48:32 +01:00
|
|
|
req, err := client.newRequest(
|
2021-02-02 12:05:47 +01:00
|
|
|
context.Background(), "\t\t\t", "/", nil, nil,
|
|
|
|
)
|
|
|
|
if err == nil || !strings.HasPrefix(err.Error(), "net/http: invalid method") {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if req != nil {
|
|
|
|
t.Fatal("expected nil request here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewRequestCloudfronting(t *testing.T) {
|
|
|
|
client := newClient()
|
|
|
|
client.Host = "www.x.org"
|
2022-01-05 12:48:32 +01:00
|
|
|
req, err := client.newRequest(
|
2021-02-02 12:05:47 +01:00
|
|
|
context.Background(), "GET", "/", nil, nil,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if req.Host != client.Host {
|
|
|
|
t.Fatal("expected different req.Host here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewRequestAcceptIsSet(t *testing.T) {
|
|
|
|
client := newClient()
|
|
|
|
client.Accept = "application/xml"
|
2022-01-05 12:48:32 +01:00
|
|
|
req, err := client.newRequestWithJSONBody(
|
2021-02-02 12:05:47 +01:00
|
|
|
context.Background(), "GET", "/", nil, []string{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if req.Header.Get("Accept") != "application/xml" {
|
|
|
|
t.Fatal("expected different Accept here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewRequestContentTypeIsSet(t *testing.T) {
|
|
|
|
client := newClient()
|
2022-01-05 12:48:32 +01:00
|
|
|
req, err := client.newRequestWithJSONBody(
|
2021-02-02 12:05:47 +01:00
|
|
|
context.Background(), "GET", "/", nil, []string{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if req.Header.Get("Content-Type") != "application/json" {
|
|
|
|
t.Fatal("expected different Content-Type here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewRequestAuthorizationHeader(t *testing.T) {
|
|
|
|
client := newClient()
|
|
|
|
client.Authorization = "deadbeef"
|
2022-01-05 12:48:32 +01:00
|
|
|
req, err := client.newRequest(
|
2021-02-02 12:05:47 +01:00
|
|
|
context.Background(), "GET", "/", nil, nil,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if req.Header.Get("Authorization") != client.Authorization {
|
|
|
|
t.Fatal("expected different Authorization here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewRequestUserAgentIsSet(t *testing.T) {
|
|
|
|
client := newClient()
|
2022-01-05 12:48:32 +01:00
|
|
|
req, err := client.newRequest(
|
2021-02-02 12:05:47 +01:00
|
|
|
context.Background(), "GET", "/", nil, nil,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if req.Header.Get("User-Agent") != userAgent {
|
|
|
|
t.Fatal("expected different User-Agent here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientDoJSONClientDoFailure(t *testing.T) {
|
|
|
|
expected := errors.New("mocked error")
|
|
|
|
client := newClient()
|
2022-01-05 12:48:32 +01:00
|
|
|
client.HTTPClient = &http.Client{Transport: FakeTransport{
|
2021-02-02 12:05:47 +01:00
|
|
|
Err: expected,
|
|
|
|
}}
|
2022-01-05 12:48:32 +01:00
|
|
|
err := client.doJSON(&http.Request{URL: &url.URL{Scheme: "https", Host: "x.org"}}, nil)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientDoJSONResponseNotSuccessful(t *testing.T) {
|
|
|
|
client := newClient()
|
2022-01-05 12:48:32 +01:00
|
|
|
client.HTTPClient = &http.Client{Transport: FakeTransport{
|
2021-02-02 12:05:47 +01:00
|
|
|
Resp: &http.Response{
|
|
|
|
StatusCode: 401,
|
2022-01-05 12:48:32 +01:00
|
|
|
Body: FakeBody{},
|
2021-02-02 12:05:47 +01:00
|
|
|
},
|
|
|
|
}}
|
2022-01-05 12:48:32 +01:00
|
|
|
err := client.doJSON(&http.Request{URL: &url.URL{Scheme: "https", Host: "x.org"}}, nil)
|
2021-02-02 12:05:47 +01:00
|
|
|
if err == nil || !strings.HasPrefix(err.Error(), "httpx: request failed") {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientDoJSONResponseReadingBodyError(t *testing.T) {
|
|
|
|
expected := errors.New("mocked error")
|
|
|
|
client := newClient()
|
2022-01-05 12:48:32 +01:00
|
|
|
client.HTTPClient = &http.Client{Transport: FakeTransport{
|
2021-02-02 12:05:47 +01:00
|
|
|
Resp: &http.Response{
|
|
|
|
StatusCode: 200,
|
2022-01-05 12:48:32 +01:00
|
|
|
Body: FakeBody{
|
2021-02-02 12:05:47 +01:00
|
|
|
Err: expected,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}}
|
2022-01-05 12:48:32 +01:00
|
|
|
err := client.doJSON(&http.Request{URL: &url.URL{Scheme: "https", Host: "x.org"}}, nil)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientDoJSONResponseIsNotJSON(t *testing.T) {
|
|
|
|
client := newClient()
|
2022-01-05 12:48:32 +01:00
|
|
|
client.HTTPClient = &http.Client{Transport: FakeTransport{
|
2021-02-02 12:05:47 +01:00
|
|
|
Resp: &http.Response{
|
|
|
|
StatusCode: 200,
|
2022-01-05 12:48:32 +01:00
|
|
|
Body: FakeBody{
|
2021-02-02 12:05:47 +01:00
|
|
|
Err: io.EOF,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}}
|
2022-01-05 12:48:32 +01:00
|
|
|
err := client.doJSON(&http.Request{URL: &url.URL{Scheme: "https", Host: "x.org"}}, nil)
|
2021-02-02 12:05:47 +01:00
|
|
|
if err == nil || err.Error() != "unexpected end of JSON input" {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type httpbinheaders struct {
|
|
|
|
Headers map[string]string `json:"headers"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadJSONSuccess(t *testing.T) {
|
|
|
|
var headers httpbinheaders
|
|
|
|
err := newClient().GetJSON(context.Background(), "/headers", &headers)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if headers.Headers["Host"] != "httpbin.org" {
|
|
|
|
t.Fatal("unexpected Host header")
|
|
|
|
}
|
|
|
|
if headers.Headers["User-Agent"] != "miniooni/0.1.0-dev" {
|
|
|
|
t.Fatal("unexpected Host header")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type httpbinpost struct {
|
|
|
|
Data string `json:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateJSONSuccess(t *testing.T) {
|
|
|
|
headers := httpbinheaders{
|
|
|
|
Headers: map[string]string{
|
|
|
|
"Foo": "bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var response httpbinpost
|
|
|
|
err := newClient().PostJSON(context.Background(), "/post", &headers, &response)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if response.Data != `{"headers":{"Foo":"bar"}}` {
|
|
|
|
t.Fatal(response.Data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadJSONFailure(t *testing.T) {
|
|
|
|
var headers httpbinheaders
|
|
|
|
client := newClient()
|
|
|
|
client.BaseURL = "\t\t\t\t"
|
|
|
|
err := client.GetJSON(context.Background(), "/headers", &headers)
|
|
|
|
if err == nil || !strings.HasSuffix(err.Error(), "invalid control character in URL") {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateJSONFailure(t *testing.T) {
|
|
|
|
var headers httpbinheaders
|
|
|
|
client := newClient()
|
|
|
|
client.BaseURL = "\t\t\t\t"
|
|
|
|
err := client.PostJSON(context.Background(), "/headers", &headers, &headers)
|
|
|
|
if err == nil || !strings.HasSuffix(err.Error(), "invalid control character in URL") {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 12:48:32 +01:00
|
|
|
func TestFetchResourceIntegration(t *testing.T) {
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
ctx := context.Background()
|
2022-01-05 14:15:42 +01:00
|
|
|
data, err := (&apiClient{
|
2022-01-05 12:48:32 +01:00
|
|
|
BaseURL: "http://facebook.com/",
|
|
|
|
HTTPClient: http.DefaultClient,
|
|
|
|
Logger: log.Log,
|
|
|
|
UserAgent: "ooniprobe-engine/0.1.0",
|
|
|
|
}).FetchResource(ctx, "/robots.txt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(data) <= 0 {
|
|
|
|
t.Fatal("Did not expect an empty resource")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFetchResourceExpiredContext(t *testing.T) {
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel()
|
2022-01-05 14:15:42 +01:00
|
|
|
data, err := (&apiClient{
|
2022-01-05 12:48:32 +01:00
|
|
|
BaseURL: "http://facebook.com/",
|
|
|
|
HTTPClient: http.DefaultClient,
|
|
|
|
Logger: log.Log,
|
|
|
|
UserAgent: "ooniprobe-engine/0.1.0",
|
|
|
|
}).FetchResource(ctx, "/robots.txt")
|
|
|
|
if !errors.Is(err, context.Canceled) {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if len(data) != 0 {
|
|
|
|
t.Fatal("expected an empty resource")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFetchResourceInvalidURL(t *testing.T) {
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
ctx := context.Background()
|
2022-01-05 14:15:42 +01:00
|
|
|
data, err := (&apiClient{
|
2022-01-05 12:48:32 +01:00
|
|
|
BaseURL: "http://\t/",
|
|
|
|
HTTPClient: http.DefaultClient,
|
|
|
|
Logger: log.Log,
|
|
|
|
UserAgent: "ooniprobe-engine/0.1.0",
|
|
|
|
}).FetchResource(ctx, "/robots.txt")
|
2021-02-02 12:05:47 +01:00
|
|
|
if err == nil || !strings.HasSuffix(err.Error(), "invalid control character in URL") {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
2022-01-05 12:48:32 +01:00
|
|
|
if len(data) != 0 {
|
|
|
|
t.Fatal("expected an empty resource")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFetchResource400(t *testing.T) {
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(400)
|
|
|
|
},
|
|
|
|
))
|
|
|
|
defer server.Close()
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
ctx := context.Background()
|
2022-01-05 14:15:42 +01:00
|
|
|
data, err := (&apiClient{
|
2022-01-05 12:48:32 +01:00
|
|
|
Authorization: "foobar",
|
|
|
|
BaseURL: server.URL,
|
|
|
|
HTTPClient: http.DefaultClient,
|
|
|
|
Logger: log.Log,
|
|
|
|
UserAgent: "ooniprobe-engine/0.1.0",
|
|
|
|
}).FetchResource(ctx, "")
|
|
|
|
if err == nil || !strings.HasSuffix(err.Error(), "400 Bad Request") {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if len(data) != 0 {
|
|
|
|
t.Fatal("expected an empty resource")
|
|
|
|
}
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|