summaryrefslogtreecommitdiff
path: root/app/fetch_test.go
blob: 9baf9c88690a704ab65fc7e1aadd4544fc1e9b49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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 := CheckPages(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 := CheckPages(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)
	}
}