From 5ef46be8c4d3d44181971dced04d90d4a5832c8f Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Sun, 15 Feb 2015 00:58:01 -0800 Subject: [PATCH] add (presently failing) tests for discovering a simple site map --- src/docrawler/docrawler.go | 13 +++++++++++++ src/docrawler/docrawler_test.go | 31 +++++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/docrawler/docrawler.go b/src/docrawler/docrawler.go index c8f4ae0..1134f4a 100644 --- a/src/docrawler/docrawler.go +++ b/src/docrawler/docrawler.go @@ -2,5 +2,18 @@ package main import () +// Page is a struct which defines a single page, which URLs (links and assets) it contains +type Page struct { + URL string + Assets []string + Links []string +} + +// docrawl begins crawling the site at "url" +func docrawl(url string) []Page { + var result []Page + return result +} + func main() { } diff --git a/src/docrawler/docrawler_test.go b/src/docrawler/docrawler_test.go index 10dae64..4b491fb 100644 --- a/src/docrawler/docrawler_test.go +++ b/src/docrawler/docrawler_test.go @@ -44,7 +44,7 @@ func TestServerRunning(t *testing.T) { resp, err := http.Get(baseURL) if resp.StatusCode != 200 || err != nil { fmt.Printf("got %v, err = %v\n", resp.StatusCode, err) - t.Error(err) + t.Fatal(err) } } @@ -53,7 +53,7 @@ func TestBadRequest(t *testing.T) { resp, err := http.Get(baseURL + "doesnotexist.html") if resp.StatusCode != 404 || err != nil { fmt.Printf("got %v, err = %v\n", resp.StatusCode, err) - t.Error("request for non existent file didn't fail properly") + t.Fatal("request for non existent file didn't fail properly") } } @@ -61,7 +61,7 @@ func TestBadRequest(t *testing.T) { func TestSimpleFetch(t *testing.T) { body, err := fetch(baseURL + "fetch_test.html") if err != nil { - t.Error(err) + t.Fatal(err) } desired := "\n" if body != desired { @@ -86,7 +86,7 @@ func TestSimpleParse(t *testing.T) { t.Error(err) } if len(matches) != 2 { - t.Error("invalid number of matches in parse") + t.Fatal("invalid number of matches in parse") } if matches[0] != "/about.html" { t.Error("match text is invalid") @@ -95,3 +95,26 @@ func TestSimpleParse(t *testing.T) { t.Error("match text is invalid") } } + +// TestSimpleMap figures out the site map for the site in baseURL +func TestSimpleMap(t *testing.T) { + pages := docrawl(baseURL) + if len(pages) != 1 { + t.Fatal("got wrong number of pages") + } + if pages[0].URL != baseURL { + t.Error("page URL is invalid") + } + if len(pages[0].Assets) != 1 { + t.Fatal("got wrong number of assets") + } + if pages[0].Assets[0] != baseURL+"/assets/image.png" { + t.Error("asset name is incorrect") + } + if len(pages[0].Links) != 1 { + t.Fatal("got wrong number of links") + } + if pages[0].Links[0] != baseURL+"/about.html" { + t.Error("link name is incorrect") + } +}