From 6a23efd1752d9ba90dd6d4ce691e94a0cd407a05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Musab=20G=C3=BCltekin?= Date: Sat, 17 Apr 2021 11:12:22 +0300 Subject: [PATCH] JoinURL now returns *url.URL and error --- client/response.go | 6 +++--- client/response_test.go | 4 +++- geziyor_test.go | 9 ++++++--- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/client/response.go b/client/response.go index 3a91a4b..a7c57a9 100644 --- a/client/response.go +++ b/client/response.go @@ -22,14 +22,14 @@ type Response struct { } // JoinURL joins base response URL and provided relative URL. -func (r *Response) JoinURL(relativeURL string) string { +func (r *Response) JoinURL(relativeURL string) (*url.URL, error) { parsedRelativeURL, err := url.Parse(relativeURL) if err != nil { - return "" + return nil, err } joinedURL := r.Request.URL.ResolveReference(parsedRelativeURL) - return joinedURL.String() + return joinedURL, nil } // IsHTML checks if response content is HTML by looking content-type header diff --git a/client/response_test.go b/client/response_test.go index 19b9cb5..cf9accc 100644 --- a/client/response_test.go +++ b/client/response_test.go @@ -9,5 +9,7 @@ func TestResponse_JoinURL(t *testing.T) { req, _ := NewRequest("GET", "https://localhost.com/test/a.html", nil) resp := Response{Request: req} - assert.Equal(t, "https://localhost.com/source", resp.JoinURL("/source")) + joinedURL, err := resp.JoinURL("/source") + assert.NoError(t, err) + assert.Equal(t, "https://localhost.com/source", joinedURL.String()) } diff --git a/geziyor_test.go b/geziyor_test.go index 392ff26..9057931 100644 --- a/geziyor_test.go +++ b/geziyor_test.go @@ -78,7 +78,8 @@ func quotesParse(g *geziyor.Geziyor, r *client.Response) { // Next Page if href, ok := r.HTMLDoc.Find("li.next > a").Attr("href"); ok { - g.Get(r.JoinURL(href), quotesParse) + absoluteURL, _ := r.JoinURL(href) + g.Get(absoluteURL.String(), quotesParse) } } @@ -94,7 +95,8 @@ func TestAllLinks(t *testing.T) { g.Exports <- []string{r.Request.URL.String()} r.HTMLDoc.Find("a").Each(func(i int, s *goquery.Selection) { if href, ok := s.Attr("href"); ok { - g.Get(r.JoinURL(href), g.Opt.ParseFunc) + absoluteURL, _ := r.JoinURL(href) + g.Get(absoluteURL.String(), g.Opt.ParseFunc) } }) }, @@ -269,7 +271,8 @@ func BenchmarkWhole(b *testing.B) { g.Exports <- []string{r.Request.URL.String()} r.HTMLDoc.Find("a").Each(func(i int, s *goquery.Selection) { if href, ok := s.Attr("href"); ok { - g.Get(r.JoinURL(href), g.Opt.ParseFunc) + absoluteURL, _ := r.JoinURL(href) + g.Get(absoluteURL.String(), g.Opt.ParseFunc) } }) },