From 70d080b69a1bce3125d8d0b83e23775880241763 Mon Sep 17 00:00:00 2001 From: Horus_Arch Date: Mon, 23 Feb 2015 12:13:32 +0100 Subject: Refactor and more unit tests. --- app/fetch_test.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 app/fetch_test.go (limited to 'app/fetch_test.go') diff --git a/app/fetch_test.go b/app/fetch_test.go new file mode 100644 index 0000000..b71a3fe --- /dev/null +++ b/app/fetch_test.go @@ -0,0 +1,60 @@ +// +build all general + +package main + +import ( + "fmt" + "net/http" + "net/http/httptest" + "testing" +) + +func TestCheckPage(t *testing.T) { + answer := "" + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/html") + w.WriteHeader(0) + fmt.Fprintln(w, answer) + })) + defer ts.Close() + + fakeUrlSuccess := ts.URL + + h := []Host{} + host := Host{ + Url: fakeUrlSuccess, + Monitored: true, + } + h = append(h, host) + test := CheckPage(h) + if test == nil { + t.Fatal("Expected slice to be not nil.") + } + if test[0].StatusCode != 0 { + t.Errorf("Expected StatusCode 0 instead of %v.\n", test[0].StatusCode) + t.Logf("Failed test for %v .\n", test[0].Url) + } else { + t.Logf("Passed test for %v.\n", test[0].Url) + } + + ts2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/html") + w.WriteHeader(200) + fmt.Fprintln(w, answer) + })) + defer ts.Close() + + h2 := []Host{} + host2 := Host{ + Url: ts2.URL, + Monitored: true, + } + h2 = append(h2, host2) + test2 := CheckPage(h2) + if test2[0].StatusCode != 200 { + t.Errorf("Expected StatusCode 200 instead of %v.\n", test2[0].StatusCode) + t.Logf("Failed test for %v .\n", test2[0].Url) + } else { + t.Logf("Passed test for %v .\n", test2[0].Url) + } +} -- cgit v1.2.3