oonimkall: add wrappers for /test-list/urls (#221)
* First wrapper implementation * Adding CountryCode parameter * requested changes * Update pkg/oonimkall/session.go Co-authored-by: Simone Basso <bassosimone@gmail.com> * Requested changes * Update pkg/oonimkall/session_integration_test.go Co-authored-by: Simone Basso <bassosimone@gmail.com> * Update pkg/oonimkall/session_integration_test.go Co-authored-by: Simone Basso <bassosimone@gmail.com> * Remove duplicated code * Apply suggestions from code review Co-authored-by: Simone Basso <bassosimone@gmail.com>
This commit is contained in:
parent
92ddbd5a5f
commit
772de83a06
|
@ -375,3 +375,70 @@ func (sess *Session) CheckIn(ctx *Context, config *CheckInConfig) (*CheckInInfo,
|
|||
WebConnectivity: newCheckInInfoWebConnectivity(result.WebConnectivity),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// URLListConfig contains configuration for fetching the URL list.
|
||||
type URLListConfig struct {
|
||||
Categories []string // Categories to query for (empty means all)
|
||||
CountryCode string // CountryCode is the optional country code
|
||||
Limit int64 // Max number of URLs (<= 0 means no limit)
|
||||
}
|
||||
|
||||
// URLListResult contains the URLs returned from the FetchURL API
|
||||
type URLListResult struct {
|
||||
Results []model.URLInfo
|
||||
}
|
||||
|
||||
// AddCategory adds category code to the array in URLListConfig
|
||||
func (ckw *URLListConfig) AddCategory(cat string) {
|
||||
ckw.Categories = append(ckw.Categories, cat)
|
||||
}
|
||||
|
||||
// At gets the URLInfo at position idx from CheckInInfoWebConnectivity.URLs. It returns
|
||||
// nil if you are using an outs of bound index.
|
||||
func (ckw *URLListResult) At(idx int64) *URLInfo {
|
||||
if idx < 0 || int(idx) >= len(ckw.Results) {
|
||||
return nil
|
||||
}
|
||||
w := ckw.Results[idx]
|
||||
return &URLInfo{
|
||||
CategoryCode: w.CategoryCode,
|
||||
CountryCode: w.CountryCode,
|
||||
URL: w.URL,
|
||||
}
|
||||
}
|
||||
|
||||
// Size returns the number of URLs.
|
||||
func (ckw *URLListResult) Size() int64 {
|
||||
return int64(len(ckw.Results))
|
||||
}
|
||||
|
||||
// FetchURLList fetches the list of URLs to test
|
||||
func (sess *Session) FetchURLList(ctx *Context, config *URLListConfig) (*URLListResult, error) {
|
||||
sess.mtx.Lock()
|
||||
defer sess.mtx.Unlock()
|
||||
psc, err := sess.sessp.NewProbeServicesClient(ctx.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if config.CountryCode == "" {
|
||||
config.CountryCode = "XX"
|
||||
info, err := sess.sessp.LookupLocationContext(ctx.ctx)
|
||||
if err == nil && info != nil {
|
||||
config.CountryCode = info.CountryCode
|
||||
}
|
||||
}
|
||||
|
||||
cfg := model.URLListConfig{
|
||||
Categories: config.Categories,
|
||||
CountryCode: config.CountryCode,
|
||||
Limit: config.Limit,
|
||||
}
|
||||
|
||||
result, err := psc.FetchURLList(ctx.ctx, cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &URLListResult{
|
||||
Results: result,
|
||||
}, nil
|
||||
}
|
||||
|
|
|
@ -419,6 +419,60 @@ func TestCheckInNoParams(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestFetchURLListSuccess(t *testing.T) {
|
||||
sess, err := NewSession()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx := sess.NewContext()
|
||||
config := oonimkall.URLListConfig{
|
||||
Limit: 10,
|
||||
}
|
||||
config.AddCategory("NEWS")
|
||||
config.AddCategory("CULTR")
|
||||
result, err := sess.FetchURLList(ctx, &config)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %+v", err)
|
||||
}
|
||||
if result == nil || result.Results == nil {
|
||||
t.Fatal("got nil result")
|
||||
}
|
||||
for _, entry := range result.Results {
|
||||
if entry.CategoryCode != "NEWS" && entry.CategoryCode != "CULTR" {
|
||||
t.Fatalf("unexpected category code: %+v", entry)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFetchURLListWithCC(t *testing.T) {
|
||||
sess, err := NewSession()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx := sess.NewContext()
|
||||
config := oonimkall.URLListConfig{
|
||||
CountryCode: "IT",
|
||||
}
|
||||
config.AddCategory("NEWS")
|
||||
config.AddCategory("CULTR")
|
||||
result, err := sess.FetchURLList(ctx, &config)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %+v", err)
|
||||
}
|
||||
if result == nil || result.Results == nil {
|
||||
t.Fatal("got nil result")
|
||||
}
|
||||
found := false
|
||||
for _, entry := range result.Results {
|
||||
if entry.CountryCode == "IT" {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("not found url for country code: IT")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
// Here we're basically testing whether eventually the finalizers
|
||||
// will run and the number of active sessions and cancels will become
|
||||
|
|
Loading…
Reference in New Issue
Block a user