Skip to content

Commit

Permalink
minor, moving things and t.Fatal
Browse files Browse the repository at this point in the history
  • Loading branch information
azimut committed Sep 8, 2021
1 parent eddee31 commit 586bc13
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 26 deletions.
21 changes: 12 additions & 9 deletions internal/hackernews/comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,26 @@ import (
)

func TestMakeComments(t *testing.T) {
file, err := ioutil.ReadFile("/home/sendai/testfield/hn.html")
file, err := ioutil.ReadFile("testdata/hn.html")
if err != nil {
t.Error(err)
t.Fatal(err)
}
r := bytes.NewReader(file)
doc, err := goquery.NewDocumentFromReader(r)
if err != nil {
t.Error(err)
t.Fatal(err)
}
comments := NewComments(doc)
if len(comments) != 19 {
t.Errorf("expected 19 root comments got %d", len(comments))
result := len(comments)
if result != 19 {
t.Errorf("expected 19 root comments got %d", result)
}
if len(comments[0].Childs) != 4 {
t.Errorf("expected 4 child comments for [0] got %d", len(comments[0].Childs))
result = len(comments[0].Childs)
if result != 4 {
t.Errorf("expected 4 child comments for [0].Childs got %d", result)
}
if len(comments[0].Childs[0].Childs) != 1 {
t.Errorf("expected 1 child comment for .Childs[0].Childs got %d", len(comments[0].Childs[0].Childs))
result = len(comments[0].Childs[0].Childs)
if result != 1 {
t.Errorf("expected 1 child comments for [0].Childs[0].Childs got %d", result)
}
}
15 changes: 1 addition & 14 deletions internal/hackernews/fetch.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
package hackernews

import (
"errors"
"net/url"
"strings"
"time"

"github.com/PuerkitoBio/goquery"
"github.com/azimut/cli-view/internal/fetch"
)

func EffectiveUrl(rawUrl string) (string, error) {
uri, err := url.Parse(rawUrl)
if err != nil {
return "", err
}
if uri.Host != "news.ycombinator.com" {
return "", errors.New("invalid hostname")
}
return uri.String(), nil
}

func Fetch(url, ua string, timeout time.Duration) (doc *goquery.Document, err error) {
url, err = EffectiveUrl(url)
url, err = effectiveUrl(url)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions internal/hackernews/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
)

func TestNewHeader(t *testing.T) {
file, err := ioutil.ReadFile("/home/sendai/testfield/hn.html")
file, err := ioutil.ReadFile("testdata/hn.html")
if err != nil {
t.Error(err)
t.Fatal(err)
}
r := bytes.NewReader(file)
doc, err := goquery.NewDocumentFromReader(r)
if err != nil {
t.Error(err)
t.Fatal(err)
}
header := NewHeader(doc)
if header.url != "https://www.newyorker.com/news/news-desk/the-red-warning-light-on-richard-bransons-space-flight" {
Expand Down
17 changes: 17 additions & 0 deletions internal/hackernews/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package hackernews

import (
"errors"
"net/url"
)

func effectiveUrl(rawUrl string) (string, error) {
uri, err := url.Parse(rawUrl)
if err != nil {
return "", err
}
if uri.Host != "news.ycombinator.com" {
return "", errors.New("invalid hostname")
}
return uri.String(), nil
}

0 comments on commit 586bc13

Please sign in to comment.