feat(ooapi): add toplevel client and simplify API (#248)

* feat(ooapi): add toplevel client and simplify API

This diff should simplify using ooapi from other packages by
adding more abstraction that wraps the existing code.

Part of https://github.com/ooni/probe/issues/1355.

* fix(ooapi): use correct comment for cloners

See https://github.com/ooni/probe-cli/pull/248#discussion_r590663843

* fix(ooapi): make sure the documentation is current

See https://github.com/ooni/probe-cli/pull/248#discussion_r590665773

* fix(ooapi): automate copying APIs

See https://github.com/ooni/probe-cli/pull/248#discussion_r590665837

* feat(ooapi): add unit tests for clientcall.go

See https://github.com/ooni/probe-cli/pull/248#discussion_r590666297

* fix(ooapi): rewrite integration tests to use toplevel API

See https://github.com/ooni/probe-cli/pull/248#discussion_r590665084
This commit is contained in:
Simone Basso 2021-03-19 09:30:42 +01:00 committed by GitHub
commit 28ce79eff1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
33 changed files with 2090 additions and 795 deletions

View file

@ -1,28 +1,13 @@
package ooapi
package ooapi_test
import (
"context"
"net/http"
"testing"
"github.com/ooni/probe-cli/v3/internal/engine/ooapi"
"github.com/ooni/probe-cli/v3/internal/engine/ooapi/apimodel"
)
type VerboseHTTPClient struct {
t *testing.T
}
func (c *VerboseHTTPClient) Do(req *http.Request) (*http.Response, error) {
c.t.Logf("> %s %s", req.Method, req.URL.String())
resp, err := http.DefaultClient.Do(req)
if err != nil {
c.t.Logf("< %s", err.Error())
return nil, err
}
c.t.Logf("< %d", resp.StatusCode)
return resp, nil
}
func TestWithRealServerDoCheckIn(t *testing.T) {
if testing.Short() {
t.Skip("skip test in short mode")
@ -40,12 +25,10 @@ func TestWithRealServerDoCheckIn(t *testing.T) {
CategoryCodes: []string{"NEWS", "CULTR"},
},
}
httpClnt := &VerboseHTTPClient{t: t}
api := &CheckInAPI{
HTTPClient: httpClnt,
}
httpClnt := &ooapi.VerboseHTTPClient{T: t}
clnt := &ooapi.Client{HTTPClient: httpClnt, KVStore: &ooapi.MemKVStore{}}
ctx := context.Background()
resp, err := api.Call(ctx, req)
resp, err := clnt.CheckIn(ctx, req)
if err != nil {
t.Fatal(err)
}
@ -67,9 +50,9 @@ func TestWithRealServerDoCheckReportID(t *testing.T) {
req := &apimodel.CheckReportIDRequest{
ReportID: "20210223T093606Z_ndt_JO_8376_n1_kDYToqrugDY54Soy",
}
api := &CheckReportIDAPI{}
clnt := &ooapi.Client{KVStore: &ooapi.MemKVStore{}}
ctx := context.Background()
resp, err := api.Call(ctx, req)
resp, err := clnt.CheckReportID(ctx, req)
if err != nil {
t.Fatal(err)
}
@ -86,9 +69,9 @@ func TestWithRealServerDoMeasurementMeta(t *testing.T) {
req := &apimodel.MeasurementMetaRequest{
ReportID: "20210223T093606Z_ndt_JO_8376_n1_kDYToqrugDY54Soy",
}
api := &MeasurementMetaAPI{}
clnt := &ooapi.Client{KVStore: &ooapi.MemKVStore{}}
ctx := context.Background()
resp, err := api.Call(ctx, req)
resp, err := clnt.MeasurementMeta(ctx, req)
if err != nil {
t.Fatal(err)
}
@ -113,9 +96,9 @@ func TestWithRealServerDoOpenReport(t *testing.T) {
TestStartTime: "2018-11-01 15:33:20",
TestVersion: "0.1.0",
}
api := &OpenReportAPI{}
clnt := &ooapi.Client{KVStore: &ooapi.MemKVStore{}}
ctx := context.Background()
resp, err := api.Call(ctx, req)
resp, err := clnt.OpenReport(ctx, req)
if err != nil {
t.Fatal(err)
}
@ -130,21 +113,10 @@ func TestWithRealServerDoPsiphonConfig(t *testing.T) {
t.Skip("skip test in short mode")
}
req := &apimodel.PsiphonConfigRequest{}
httpClnt := &VerboseHTTPClient{t: t}
api := &PsiphonConfigAPIWithLogin{
API: &PsiphonConfigAPI{
HTTPClient: httpClnt,
},
KVStore: &memkvstore{},
RegisterAPI: &RegisterAPI{
HTTPClient: httpClnt,
},
LoginAPI: &LoginAPI{
HTTPClient: httpClnt,
},
}
httpClnt := &ooapi.VerboseHTTPClient{T: t}
clnt := &ooapi.Client{HTTPClient: httpClnt, KVStore: &ooapi.MemKVStore{}}
ctx := context.Background()
resp, err := api.Call(ctx, req)
resp, err := clnt.PsiphonConfig(ctx, req)
if err != nil {
t.Fatal(err)
}
@ -159,21 +131,10 @@ func TestWithRealServerDoTorTargets(t *testing.T) {
t.Skip("skip test in short mode")
}
req := &apimodel.TorTargetsRequest{}
httpClnt := &VerboseHTTPClient{t: t}
api := &TorTargetsAPIWithLogin{
API: &TorTargetsAPI{
HTTPClient: httpClnt,
},
KVStore: &memkvstore{},
RegisterAPI: &RegisterAPI{
HTTPClient: httpClnt,
},
LoginAPI: &LoginAPI{
HTTPClient: httpClnt,
},
}
httpClnt := &ooapi.VerboseHTTPClient{T: t}
clnt := &ooapi.Client{HTTPClient: httpClnt, KVStore: &ooapi.MemKVStore{}}
ctx := context.Background()
resp, err := api.Call(ctx, req)
resp, err := clnt.TorTargets(ctx, req)
if err != nil {
t.Fatal(err)
}
@ -191,9 +152,9 @@ func TestWithRealServerDoURLs(t *testing.T) {
CountryCode: "IT",
Limit: 3,
}
api := &URLsAPI{}
clnt := &ooapi.Client{KVStore: &ooapi.MemKVStore{}}
ctx := context.Background()
resp, err := api.Call(ctx, req)
resp, err := clnt.URLs(ctx, req)
if err != nil {
t.Fatal(err)
}