From e9d79ae783000f449b39023836023a93a8edc7b4 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 3 Nov 2024 13:07:49 +0200 Subject: simplify test --- internal/platforms/linkedin/oauth2/oauth2.go | 4 ++-- internal/platforms/linkedin/preview.go | 12 +++++++++--- internal/platforms/linkedin/preview_test.go | 26 +++++++++++++++++--------- 3 files changed, 28 insertions(+), 14 deletions(-) (limited to 'internal/platforms') diff --git a/internal/platforms/linkedin/oauth2/oauth2.go b/internal/platforms/linkedin/oauth2/oauth2.go index 8c03761..9a6c84d 100644 --- a/internal/platforms/linkedin/oauth2/oauth2.go +++ b/internal/platforms/linkedin/oauth2/oauth2.go @@ -117,7 +117,7 @@ func LinkedInCreds(ctx context.Context, args config.Args) (string, string, error } }() - if err := waitUntilURLIsReachable("http://localhost:8080/up"); err != nil { + if err := WaitUntilURLIsReachable("http://localhost:8080/up"); err != nil { return "", "", err } @@ -154,7 +154,7 @@ func openURLInFirefox(browser, url string) error { } } -func waitUntilURLIsReachable(url string) error { +func WaitUntilURLIsReachable(url string) error { var counter int for counter < 10 { counter++ diff --git a/internal/platforms/linkedin/preview.go b/internal/platforms/linkedin/preview.go index 03bf7e6..87a8d3e 100644 --- a/internal/platforms/linkedin/preview.go +++ b/internal/platforms/linkedin/preview.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "io" "log" "net/http" "net/url" @@ -24,7 +25,7 @@ func NewPreview(ctx context.Context, urls []string) (preview, error) { if len(urls) == 0 { return preview{}, nil } - title, imageURL, err := fetchHTMLTitleAndFirstImage(ctx, urls[0]) + title, imageURL, err := extractFromURL(ctx, urls[0]) if errors.Is(err, errNoTitleElementFound) || (err == nil && title == "") { log.Println("Setting title to", urls[0]) title = urls[0] @@ -98,7 +99,7 @@ func resolveURL(baseURL, rawURL string) (string, error) { return base.ResolveReference(u).String(), nil } -func fetchHTMLTitleAndFirstImage(ctx context.Context, url string) (string, string, error) { +func extractFromURL(ctx context.Context, url string) (string, string, error) { req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { return "", "", fmt.Errorf("failed to create request: %w", err) @@ -112,7 +113,12 @@ func fetchHTMLTitleAndFirstImage(ctx context.Context, url string) (string, strin if resp.StatusCode != http.StatusOK { return "", "", fmt.Errorf("failed to get a successful response: %v", resp.StatusCode) } - doc, err := html.Parse(resp.Body) + + return extract(url, resp.Body) +} + +func extract(url string, htmlBody io.Reader) (string, string, error) { + doc, err := html.Parse(htmlBody) if err != nil { return "", "", fmt.Errorf("failed to parse HTML: %w", err) } diff --git a/internal/platforms/linkedin/preview_test.go b/internal/platforms/linkedin/preview_test.go index ea47d08..c782764 100644 --- a/internal/platforms/linkedin/preview_test.go +++ b/internal/platforms/linkedin/preview_test.go @@ -1,19 +1,27 @@ package linkedin import ( - "context" + "strings" "testing" ) -// TODO: Mock the http request, and serve a local HTML page! -func TestFetchHTMLTitleAndFirstImage(t *testing.T) { - var ( - url = "https://foo.zone/about/" - expectedTitle = "About" - expectedImageURL = "https://foo.zone/about/paul.jpg" - ) +func TestPreviewExtract(t *testing.T) { - title, imageURL, err := fetchHTMLTitleAndFirstImage(context.Background(), url) + expectedTitle := "Baz" + expectedImageURL := "https://free.beer:666/bar/foo.jpg" + mockHTML := strings.NewReader(` + + + + Baz + + + Foo + + +`) + + title, imageURL, err := extract("https://free.beer:666/bar/", mockHTML) if err != nil { t.Error(err) } -- cgit v1.2.3