fix(bloomberg): avoid collection timeouts
This commit is contained in:
@ -0,0 +1,58 @@
|
||||
package bloomberg
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTicket35AbsentContentIsEmptyAndScraperErrorsAreSanitized(t *testing.T) {
|
||||
cases := []struct {
|
||||
name, body string
|
||||
status int
|
||||
err error
|
||||
want string
|
||||
}{
|
||||
{name: "success", status: 200, body: `{"articles":[{"title":"fixture title","url":"https://fixture.invalid/article"}]}`},
|
||||
{name: "http", status: 503, body: "PRIVATE_BODY https://private.invalid", want: "HTTP 503"},
|
||||
{name: "service", status: 200, body: `{"error":"PRIVATE_BODY https://private.invalid"}`, want: "scraper failure"},
|
||||
{name: "transport", err: &url.Error{Op: "POST", URL: "https://PRIVATE_URL.invalid", Err: errors.New("PRIVATE_BODY")}, want: "transport failure"},
|
||||
{name: "timeout", err: &url.Error{Op: "POST", URL: "https://PRIVATE_URL.invalid", Err: context.DeadlineExceeded}, want: "timeout"},
|
||||
{name: "canceled", err: &url.Error{Op: "POST", URL: "https://PRIVATE_URL.invalid", Err: context.Canceled}, want: "canceled"},
|
||||
{name: "json", status: 200, body: `{"PRIVATE_BODY":`, want: "invalid response"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
b := New("http://PRIVATE_URL.invalid")
|
||||
b.client = &http.Client{Transport: dateTransport(func(*http.Request) (*http.Response, error) {
|
||||
if tc.err != nil {
|
||||
return nil, tc.err
|
||||
}
|
||||
return &http.Response{StatusCode: tc.status, Body: io.NopCloser(strings.NewReader(tc.body)), Header: make(http.Header)}, nil
|
||||
})}
|
||||
articles, err := b.ScrapeWithCredentials(context.Background(), "PRIVATE_USER", "PRIVATE_SECRET", nil)
|
||||
if tc.want == "" {
|
||||
if err != nil || len(articles) != 1 || articles[0].Content != "" || articles[0].PublishedAt != nil {
|
||||
t.Fatal("invented content/date", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err == nil || !strings.Contains(err.Error(), tc.want) || articles != nil {
|
||||
t.Fatalf("unexpected result: %v", err)
|
||||
}
|
||||
if strings.Contains(err.Error(), "PRIVATE") || strings.Contains(err.Error(), "http://") || strings.Contains(err.Error(), "https://") {
|
||||
t.Fatal("raw scraper error leaked")
|
||||
}
|
||||
if tc.name == "timeout" && !errors.Is(err, context.DeadlineExceeded) {
|
||||
t.Fatal("timeout identity lost")
|
||||
}
|
||||
if tc.name == "canceled" && !errors.Is(err, context.Canceled) {
|
||||
t.Fatal("cancellation identity lost")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user