summaryrefslogtreecommitdiff
path: root/app/fetch_test.go
diff options
context:
space:
mode:
authorHorus_Arch2015-02-23 12:13:32 +0100
committerHorus_Arch2015-02-23 12:13:32 +0100
commit70d080b69a1bce3125d8d0b83e23775880241763 (patch)
tree6fac80f9291e361045098b2ce9eb0839652e6b42 /app/fetch_test.go
parent00b28812fb5ab68156ead5b45b66740a4d5ca688 (diff)
downloadstatuspage-70d080b69a1bce3125d8d0b83e23775880241763.tar.gz
Refactor and more unit tests.
Diffstat (limited to 'app/fetch_test.go')
-rw-r--r--app/fetch_test.go60
1 files changed, 60 insertions, 0 deletions
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)
+ }
+}