ooni-probe-cli/internal/httpapi/sequence_test.go

359 lines
8.8 KiB
Go
Raw Normal View History

feat(webconnectivity): try all the available THs (#980) We introduce a fork of internal/httpx, named internal/httpapi, where there is a clear split between the concept of an API endpoint (such as https://0.th.ooni.org/) and of an API descriptor (such as using `GET` to access /api/v1/test-list/url). Additionally, httpapi allows to create a SequenceCaller that tries to call a given API descriptor using multiple API endpoints. The SequenceCaller will stop once an endpoint works or when all the available endpoints have been tried unsuccessfully. The definition of "success" is the following: we consider "failure" any error that occurs during the HTTP round trip or when reading the response body. We DO NOT consider "failure" errors (1) when parsing the input URL; (2) when the server returns >= 400; (3) when the server returns a string that does not parse as valid JSON. The idea of this classification of failures is that we ONLY want to retry when we see what looks like a network error that may be caused by (collateral or targeted) censorship. We take advantage of the availability of this new package and we refactor web_connectivity@v0.4 and web_connectivity@v0.5 to use a SequenceCaller for calling the web connectivity TH API. This means that we will now try all the available THs advertised by the backend rather than just selecting and using the first one provided by the backend. Because this diff is designed to be backported to the `release/3.16` branch, we have omitted additional changes to always use httpapi where we are currently using httpx. Yet, to remind ourselves about the need to do that, we have deprecated the httpx package. We will rewrite all the code currently using httpx to use httpapi as part of future work. It is also worth noting that httpapi will allow us to refactor the backend code such that (1) we remove code to select a backend URL endpoint at the beginning and (2) we try several endpoints. The design of the code is such that we can add to the mix some endpoints using as `http.Client` a special client using a tunnel. This will allow us to automatically fallback backend queries. Closes https://github.com/ooni/probe/issues/2353. Related to https://github.com/ooni/probe/issues/1519.
2022-11-21 16:28:53 +01:00
package httpapi
import (
"context"
"errors"
"io"
"net/http"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/model/mocks"
)
func TestSequenceCaller(t *testing.T) {
t.Run("Call", func(t *testing.T) {
t.Run("first success", func(t *testing.T) {
sc := NewSequenceCaller(
&Descriptor{
Logger: model.DiscardLogger,
Method: http.MethodGet,
URLPath: "/",
},
&Endpoint{
BaseURL: "https://a.example.com/",
HTTPClient: &mocks.HTTPClient{
MockDo: func(req *http.Request) (*http.Response, error) {
resp := &http.Response{
StatusCode: 200,
Body: io.NopCloser(strings.NewReader("deadbeef")),
}
return resp, nil
},
},
},
&Endpoint{
BaseURL: "https://b.example.com/",
HTTPClient: &mocks.HTTPClient{
MockDo: func(req *http.Request) (*http.Response, error) {
return nil, io.EOF
},
},
},
)
data, idx, err := sc.Call(context.Background())
if err != nil {
t.Fatal(err)
}
if idx != 0 {
t.Fatal("invalid idx")
}
if diff := cmp.Diff([]byte("deadbeef"), data); diff != "" {
t.Fatal(diff)
}
})
t.Run("first HTTP failure and we immediately stop", func(t *testing.T) {
sc := NewSequenceCaller(
&Descriptor{
Logger: model.DiscardLogger,
Method: http.MethodGet,
URLPath: "/",
},
&Endpoint{
BaseURL: "https://a.example.com/",
HTTPClient: &mocks.HTTPClient{
MockDo: func(req *http.Request) (*http.Response, error) {
resp := &http.Response{
StatusCode: 403, // should cause us to return early
Body: io.NopCloser(strings.NewReader("deadbeef")),
}
return resp, nil
},
},
},
&Endpoint{
BaseURL: "https://b.example.com/",
HTTPClient: &mocks.HTTPClient{
MockDo: func(req *http.Request) (*http.Response, error) {
return nil, io.EOF
},
},
},
)
data, idx, err := sc.Call(context.Background())
var failure *ErrHTTPRequestFailed
if !errors.As(err, &failure) || failure.StatusCode != 403 {
t.Fatal("unexpected err", err)
}
if idx != 0 {
t.Fatal("invalid idx")
}
if len(data) > 0 {
t.Fatal("expected to see no response body")
}
})
t.Run("first network failure, second success", func(t *testing.T) {
sc := NewSequenceCaller(
&Descriptor{
Logger: model.DiscardLogger,
Method: http.MethodGet,
URLPath: "/",
},
&Endpoint{
BaseURL: "https://a.example.com/",
HTTPClient: &mocks.HTTPClient{
MockDo: func(req *http.Request) (*http.Response, error) {
return nil, io.EOF // should cause us to cycle to the second entry
},
},
},
&Endpoint{
BaseURL: "https://b.example.com/",
HTTPClient: &mocks.HTTPClient{
MockDo: func(req *http.Request) (*http.Response, error) {
resp := &http.Response{
StatusCode: 200,
Body: io.NopCloser(strings.NewReader("abad1dea")),
}
return resp, nil
},
},
},
)
data, idx, err := sc.Call(context.Background())
if err != nil {
t.Fatal(err)
}
if idx != 1 {
t.Fatal("invalid idx")
}
if diff := cmp.Diff([]byte("abad1dea"), data); diff != "" {
t.Fatal(diff)
}
})
t.Run("all network failure", func(t *testing.T) {
sc := NewSequenceCaller(
&Descriptor{
Logger: model.DiscardLogger,
Method: http.MethodGet,
URLPath: "/",
},
&Endpoint{
BaseURL: "https://a.example.com/",
HTTPClient: &mocks.HTTPClient{
MockDo: func(req *http.Request) (*http.Response, error) {
return nil, io.EOF // should cause us to cycle to the next entry
},
},
},
&Endpoint{
BaseURL: "https://b.example.com/",
HTTPClient: &mocks.HTTPClient{
MockDo: func(req *http.Request) (*http.Response, error) {
return nil, io.EOF // should cause us to cycle to the next entry
},
},
},
)
data, idx, err := sc.Call(context.Background())
if !errors.Is(err, ErrAllEndpointsFailed) {
t.Fatal("unexpected err", err)
}
if idx != -1 {
t.Fatal("invalid idx")
}
if len(data) > 0 {
t.Fatal("expected zero-length data")
}
})
})
t.Run("CallWithJSONResponse", func(t *testing.T) {
type response struct {
Name string
Age int64
}
t.Run("first success", func(t *testing.T) {
sc := NewSequenceCaller(
&Descriptor{
Logger: model.DiscardLogger,
Method: http.MethodGet,
URLPath: "/",
},
&Endpoint{
BaseURL: "https://a.example.com/",
HTTPClient: &mocks.HTTPClient{
MockDo: func(req *http.Request) (*http.Response, error) {
resp := &http.Response{
StatusCode: 200,
Body: io.NopCloser(strings.NewReader(`{"Name":"sbs","Age":99}`)),
}
return resp, nil
},
},
},
&Endpoint{
BaseURL: "https://b.example.com/",
HTTPClient: &mocks.HTTPClient{
MockDo: func(req *http.Request) (*http.Response, error) {
resp := &http.Response{
StatusCode: 200,
Body: io.NopCloser(strings.NewReader(`{}`)), // different
}
return resp, nil
},
},
},
)
expect := response{
Name: "sbs",
Age: 99,
}
var got response
idx, err := sc.CallWithJSONResponse(context.Background(), &got)
if err != nil {
t.Fatal(err)
}
if idx != 0 {
t.Fatal("invalid idx")
}
if diff := cmp.Diff(expect, got); diff != "" {
t.Fatal(diff)
}
})
t.Run("first HTTP failure and we immediately stop", func(t *testing.T) {
sc := NewSequenceCaller(
&Descriptor{
Logger: model.DiscardLogger,
Method: http.MethodGet,
URLPath: "/",
},
&Endpoint{
BaseURL: "https://a.example.com/",
HTTPClient: &mocks.HTTPClient{
MockDo: func(req *http.Request) (*http.Response, error) {
resp := &http.Response{
StatusCode: 403, // should be enough to cause us fail immediately
Body: io.NopCloser(strings.NewReader(`{"Age": 155, "Name": "sbs"}`)),
}
return resp, nil
},
},
},
&Endpoint{
BaseURL: "https://b.example.com/",
HTTPClient: &mocks.HTTPClient{
MockDo: func(req *http.Request) (*http.Response, error) {
return nil, io.EOF
},
},
},
)
// even though there is a JSON body we don't care about reading it
// and so we expect to see in output the zero-value struct
expect := response{
Name: "",
Age: 0,
}
var got response
idx, err := sc.CallWithJSONResponse(context.Background(), &got)
var failure *ErrHTTPRequestFailed
if !errors.As(err, &failure) || failure.StatusCode != 403 {
t.Fatal("unexpected err", err)
}
if idx != 0 {
t.Fatal("invalid idx")
}
if diff := cmp.Diff(expect, got); diff != "" {
t.Fatal(diff)
}
})
t.Run("first network failure, second success", func(t *testing.T) {
sc := NewSequenceCaller(
&Descriptor{
Logger: model.DiscardLogger,
Method: http.MethodGet,
URLPath: "/",
},
&Endpoint{
BaseURL: "https://a.example.com/",
HTTPClient: &mocks.HTTPClient{
MockDo: func(req *http.Request) (*http.Response, error) {
return nil, io.EOF // should cause us to try the next entry
},
},
},
&Endpoint{
BaseURL: "https://b.example.com/",
HTTPClient: &mocks.HTTPClient{
MockDo: func(req *http.Request) (*http.Response, error) {
resp := &http.Response{
StatusCode: 200,
Body: io.NopCloser(strings.NewReader(`{"Age":155}`)),
}
return resp, nil
},
},
},
)
expect := response{
Name: "",
Age: 155,
}
var got response
idx, err := sc.CallWithJSONResponse(context.Background(), &got)
if err != nil {
t.Fatal(err)
}
if idx != 1 {
t.Fatal("invalid idx")
}
if diff := cmp.Diff(expect, got); diff != "" {
t.Fatal(diff)
}
})
t.Run("all network failure", func(t *testing.T) {
sc := NewSequenceCaller(
&Descriptor{
Logger: model.DiscardLogger,
Method: http.MethodGet,
URLPath: "/",
},
&Endpoint{
BaseURL: "https://a.example.com/",
HTTPClient: &mocks.HTTPClient{
MockDo: func(req *http.Request) (*http.Response, error) {
return nil, io.EOF // should cause us to try the next entry
},
},
},
&Endpoint{
BaseURL: "https://b.example.com/",
HTTPClient: &mocks.HTTPClient{
MockDo: func(req *http.Request) (*http.Response, error) {
return nil, io.EOF // should cause us to try the next entry
},
},
},
)
var got response
idx, err := sc.CallWithJSONResponse(context.Background(), &got)
if !errors.Is(err, ErrAllEndpointsFailed) {
t.Fatal("unexpected err", err)
}
if idx != -1 {
t.Fatal("invalid idx")
}
})
})
}