Skip to content

Commit

Permalink
Fix scrape service test (#44)
Browse files Browse the repository at this point in the history
This fixes the test in two ways. It fixes the syntax of
newPagespeedScrapeService having two return values, and
it allows authentication against Google to actually call
the Pagespeed API for the non-short version of the test.
  • Loading branch information
penmanglewood authored Jun 2, 2022
1 parent 1467563 commit 539158c
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion collector/scrape_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,42 @@
package collector

import (
"os"
"testing"
"time"

"google.golang.org/api/option"
)

const (
envvarAPIKey = "PAGESPEED_API_KEY"
envvarCredentialsFile = "PAGESPEED_CREDENTIALS_FILE"
)

func Test_PagespeedScrapeService(t *testing.T) {
if testing.Short() {
t.Skip("skipping testing in short mode")
}

service := newPagespeedScrapeService(30 * time.Second)
var options []option.ClientOption

if apiKey := os.Getenv(envvarAPIKey); apiKey != "" {
options = append(options, option.WithAPIKey(apiKey))
}

if cf := os.Getenv(envvarCredentialsFile); cf != "" {
options = append(options, option.WithCredentialsFile(cf))
}

if len(options) == 0 {
t.Skip("skipping testing unless API key or credentials file is set")
}

service, err := newPagespeedScrapeService(30*time.Second, options...)
if err != nil {
t.Fatalf("newPagespeedScrapeService should not throw an error: %v", err)
}

scrapes, err := service.Scrape(false, CalculateScrapeRequests("http://example.com/"))
if err != nil {
t.Fatal("scrape should not throw an error")
Expand Down

0 comments on commit 539158c

Please sign in to comment.