-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
136 lines (118 loc) · 4.42 KB
/
main_test.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
// run the unit tests : go test main.go main_test.go -v
// stop at first failing test : go test -v -failfast
// test specific unit : go test -run TestFetch_Valid -v
// test specific unit : go test -run TestFetch_EndToEnd -v
// test specific unit : go test -run ExampleFirstWorker -v
// view the test coverage : go test -cover
import (
"net/http"
"net/http/httptest"
"testing"
"time"
)
// TestFetch unit tests Fetch function on returned status accuracy.
func TestFetch_Valid(t *testing.T) {
expected := "200 OK"
// mock a server which only sends status 200.
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
status := Fetch(server.URL)
if status != expected {
t.Errorf("expected status to be %s but got %s", expected, status)
}
}
// TestFetch unit tests Fetch function on returned status accuracy.
func TestFetch_Invalid(t *testing.T) {
notExpected := "200 OK"
// mock a server which only sends status <404 Not Found>.
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
}))
defer server.Close()
status := Fetch(server.URL)
if status == notExpected {
t.Errorf("status should not be %s but instead %s", notExpected, status)
}
}
// TestFetch_Timeout unit tests Fetch function on timeout behaviour.
func TestFetch_Timeout(t *testing.T) {
expected := "n/a"
// mock a server which only sends status 200.
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// delay the response for more than 5secs.
time.Sleep(6 * time.Second)
w.WriteHeader(http.StatusFound)
}))
defer server.Close()
status := Fetch(server.URL)
if status != expected {
t.Errorf("expected status to be %s but got %s", expected, status)
}
}
// TestFetch_Error unit tests Fetch function on bad url where request should fail.
func TestFetch_Error(t *testing.T) {
expected := "n/a"
// mock a server which only sends status 200.
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusFound)
}))
defer server.Close()
status := Fetch(server.URL + "bad url")
if status != expected {
t.Errorf("expected status to be %s but got %s", expected, status)
}
}
// TestFetch_EndToEnd performs a parallel table-driven end-to-end testing of Fetch function.
func TestFetch_EndToEnd(t *testing.T) {
// slice of anonymous struct for functional end-to-end testings.
var testsTable = []struct {
name string
url string
want string
}{
{name: "testing status 200", url: "https://httpstat.us/200", want: "200 OK"},
{name: "testing status 400", url: "https://httpstat.us/400", want: "400 Bad Request"},
{name: "testing status 401", url: "https://httpstat.us/401", want: "401 Unauthorized"},
{name: "testing status 404", url: "https://httpstat.us/404", want: "404 Not Found"},
{name: "testing status 503", url: "https://httpstat.us/503", want: "503 Service Unavailable"},
{name: "testing status n/a", url: "https://hxxxxxxx.us/", want: "n/a"},
}
for _, tc := range testsTable {
t.Run(tc.name, func(t *testing.T) {
if got := Fetch(tc.url); got != tc.want {
t.Errorf("expected: %v, got %v", tc.want, got)
}
})
}
}
// ExampleFirstWorker performs end-to-end functional testing of FirstWorker function.
// Make sure that the website used here (https://cisco.com) is available & accessible.
func ExampleFirstWorker() {
FirstWorker([]string{"https://cisco.com"})
// Output:
// https://cisco.com : 200 OK
}
// ExampleSecondWorker performs end-to-end functional testing of SecondWorker function.
// Make sure that the website used here (https://cisco.com) is available & accessible.
func ExampleSecondWorker() {
SecondWorker([]string{"https://cisco.com"})
// Output:
// https://cisco.com : 200 OK
}
// ExampleThirdWorker performs end-to-end functional testing of ThirdWorker function.
// Make sure that the website used here (https://cisco.com) is available & accessible.
func ExampleThirdWorker() {
ThirdWorker([]string{"https://cisco.com"})
// Output:
// https://cisco.com : 200 OK
}
// ExampleFourthWorker performs end-to-end functional testing of FourthWorker function.
// Make sure that the website used here (https://cisco.com) is available & accessible.
func ExampleFourthWorker() {
FourthWorker([]string{"https://cisco.com"})
// Output:
// worker 0 :: https://cisco.com : 200 OK
}