Browse Source

s/Url/URL/g

renovate/configure
Yota Toyama 8 years ago
parent
commit
7cc1d2e72c
  1. 8
      main.go
  2. 14
      main_test.go

8
main.go

@ -37,7 +37,7 @@ func main() {
ok := true ok := true
for s := range extractUrls(n) { for s := range extractURLs(n) {
if _, err := http.Get(s); err != nil { if _, err := http.Get(s); err != nil {
printToStderr("ERROR: " + err.Error()) printToStderr("ERROR: " + err.Error())
ok = false ok = false
@ -49,7 +49,7 @@ func main() {
} }
} }
func extractUrls(n *html.Node) map[string]bool { func extractURLs(n *html.Node) map[string]bool {
ss := make(map[string]bool) ss := make(map[string]bool)
ns := make([]*html.Node, 0, 1024) ns := make([]*html.Node, 0, 1024)
ns = append(ns, n) ns = append(ns, n)
@ -61,7 +61,7 @@ func extractUrls(n *html.Node) map[string]bool {
if n.Type == html.ElementNode && n.Data == "a" { if n.Type == html.ElementNode && n.Data == "a" {
for _, a := range n.Attr { for _, a := range n.Attr {
if a.Key == "href" && isUrl(a.Val) { if a.Key == "href" && isURL(a.Val) {
ss[a.Val] = true ss[a.Val] = true
break break
} }
@ -76,7 +76,7 @@ func extractUrls(n *html.Node) map[string]bool {
return ss return ss
} }
func isUrl(s string) bool { func isURL(s string) bool {
u, err := url.Parse(s) u, err := url.Parse(s)
return err == nil && (u.Scheme == "http" || u.Scheme == "https") return err == nil && (u.Scheme == "http" || u.Scheme == "https")
} }

14
main_test.go

@ -9,10 +9,10 @@ import (
"golang.org/x/net/html" "golang.org/x/net/html"
) )
func TestExtractUrls(t *testing.T) { func TestExtractURLs(t *testing.T) {
for _, c := range []struct { for _, c := range []struct {
html string html string
numUrls int numURLs int
}{ }{
{`<a href="https://google.com">Google</a>`, 1}, {`<a href="https://google.com">Google</a>`, 1},
{ {
@ -37,23 +37,23 @@ func TestExtractUrls(t *testing.T) {
n, err := html.Parse(strings.NewReader(c.html)) n, err := html.Parse(strings.NewReader(c.html))
assert.Equal(t, nil, err) assert.Equal(t, nil, err)
assert.Equal(t, c.numUrls, len(extractUrls(n))) assert.Equal(t, c.numURLs, len(extractURLs(n)))
} }
} }
func TestUrlParse(t *testing.T) { func TestURLParse(t *testing.T) {
u, err := url.Parse("file-path") u, err := url.Parse("file-path")
assert.Equal(t, nil, err) assert.Equal(t, nil, err)
assert.Equal(t, "", u.Scheme) assert.Equal(t, "", u.Scheme)
} }
func TestIsUrl(t *testing.T) { func TestIsURL(t *testing.T) {
for _, s := range []string{"http://google.com", "https://google.com"} { for _, s := range []string{"http://google.com", "https://google.com"} {
assert.True(t, isUrl(s)) assert.True(t, isURL(s))
} }
for _, s := range []string{"", "file-path"} { for _, s := range []string{"", "file-path"} {
assert.False(t, isUrl(s)) assert.False(t, isURL(s))
} }
} }

Loading…
Cancel
Save