feat(httpx): improve testing using the fakefiller (#649)

This diff extracts the fakefiller inside of internal/ooapi (a
currently unused package) into its own package.

The fakefiller knows how to fill many fields that are typically
shared as data structures across processes.

It is not perfect in that it cannot fill logger or http client
fields, but still helps with better filling and testing.

So, here we're using the fakefiller to improve testing of httpx
and, nicely enough, we've already catched a bug in the way in
which APIClientTemplate.Build misses to forward Authorization from
the original template. Yay!

Work part of https://github.com/ooni/probe/issues/1951
This commit is contained in:
Simone Basso
2022-01-05 14:49:31 +01:00
committed by GitHub
parent eed51978ca
commit 93f084598e
24 changed files with 433 additions and 337 deletions
+1 -1
View File
@@ -42,7 +42,7 @@ type APIClientTemplate struct {
// Build creates an APIClient from the APIClientTemplate.
func (tmpl *APIClientTemplate) Build() APIClient {
return tmpl.BuildWithAuthorization("")
return tmpl.BuildWithAuthorization(tmpl.Authorization)
}
// BuildWithAuthorization creates an APIClient from the
+31 -23
View File
@@ -11,6 +11,8 @@ import (
"testing"
"github.com/apex/log"
"github.com/google/go-cmp/cmp"
"github.com/ooni/probe-cli/v3/internal/fakefill"
"github.com/ooni/probe-cli/v3/internal/model"
)
@@ -18,39 +20,45 @@ const userAgent = "miniooni/0.1.0-dev"
func TestAPIClientTemplate(t *testing.T) {
t.Run("normal constructor", func(t *testing.T) {
// TODO(bassosimone): we need to use fakeFiller here
// 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
tmpl := &APIClientTemplate{
Accept: "application/json",
Authorization: "ORIG-TOKEN",
BaseURL: "https://ams-pg.ooni.org/",
HTTPClient: http.DefaultClient,
Host: "ams-pg.ooni.org",
Logger: model.DiscardLogger,
UserAgent: userAgent,
HTTPClient: http.DefaultClient,
Logger: model.DiscardLogger,
}
ff := &fakefill.Filler{}
ff.Fill(tmpl)
ac := tmpl.Build()
if ac == nil {
t.Fatal("expected non-nil Client here")
orig := apiClient(*tmpl)
if diff := cmp.Diff(&orig, ac); diff != "" {
t.Fatal(diff)
}
})
t.Run("constructor with authorization", func(t *testing.T) {
// TODO(bassosimone): we need to use fakeFiller here
// 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
tmpl := &APIClientTemplate{
Accept: "application/json",
Authorization: "ORIG-TOKEN",
BaseURL: "https://ams-pg.ooni.org/",
HTTPClient: http.DefaultClient,
Host: "ams-pg.ooni.org",
Logger: model.DiscardLogger,
UserAgent: userAgent,
HTTPClient: http.DefaultClient,
Logger: model.DiscardLogger,
}
ac := tmpl.BuildWithAuthorization("AUTH-TOKEN")
if tmpl.Authorization != "ORIG-TOKEN" {
t.Fatal("invalid template Authorization")
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")
}
if ac.(*apiClient).Authorization != "AUTH-TOKEN" {
t.Fatal("invalid client Authorization")
// clear authorization for the comparison
tmpl.Authorization = ""
ac.(*apiClient).Authorization = ""
orig := apiClient(*tmpl)
if diff := cmp.Diff(&orig, ac); diff != "" {
t.Fatal(diff)
}
})
}