Browse Source

Send http requests

renovate/configure
Yota Toyama 8 years ago
parent
commit
08fa209895
  1. 17
      examples/markdown.feature
  2. 27
      main.go

17
examples/markdown.feature

@ -24,3 +24,20 @@ Feature: Markdown
""" """
When I successfully run `linkcheck foo.md` When I successfully run `linkcheck foo.md`
Then the stdout should contain exactly "" Then the stdout should contain exactly ""
Scenario: Check a markdown file which contains a live link
Given a file named "foo.md" with:
"""
[Google](https://google.com)
"""
When I successfully run `linkcheck foo.md`
Then the stdout should contain exactly ""
Scenario: Check a markdown file which contains a dead link
Given a file named "foo.md" with:
"""
[The answer](https://some-say-the-answer-is-42.com)
"""
When I run `linkcheck foo.md`
Then the exit status should be 1
And the stderr should contain "ERROR: "

27
main.go

@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http"
"os" "os"
"strings" "strings"
@ -14,7 +15,7 @@ import (
func main() { func main() {
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
fmt.Fprintln(os.Stderr, r.(error).Error()) printToStderr(r.(error).Error())
os.Exit(1) os.Exit(1)
} }
}() }()
@ -27,7 +28,25 @@ func main() {
panic(err) panic(err)
} }
html.Parse(strings.NewReader(mark.Render(string(bs)))) n, err := html.Parse(strings.NewReader(mark.Render(string(bs))))
if err != nil {
panic(err)
}
ss := extractUrls(n)
ok := true
for s, _ := range ss {
if _, err := http.Get(s); err != nil {
printToStderr("ERROR: " + err.Error())
ok = false
}
}
if !ok {
os.Exit(1)
}
} }
func extractUrls(n *html.Node) map[string]bool { func extractUrls(n *html.Node) map[string]bool {
@ -71,3 +90,7 @@ Usage:
return args return args
} }
func printToStderr(xs ...interface{}) {
fmt.Fprintln(os.Stderr, xs...)
}

Loading…
Cancel
Save