-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
140 lines (114 loc) · 3.39 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
137
138
139
140
package main
import (
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestSendRequest_Success(t *testing.T) {
client := &http.Client{}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("User-Agent") != userAgent {
t.Errorf("Expected User-Agent %v, got %v", userAgent, r.Header.Get("User-Agent"))
}
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
sendRequest(client, server.URL)
}
func TestSendRequest_ErrorCreatingRequest(t *testing.T) {
client := &http.Client{}
invalidURL := string([]byte{0x7f}) // Invalid URL to cause an error in http.NewRequest
sendRequest(client, invalidURL)
}
func TestSendRequest_ErrorSendingRequest(t *testing.T) {
client := &http.Client{}
badServerURL := "http://bad.server.com"
sendRequest(client, badServerURL)
}
func TestDownloadWebsites_Success(t *testing.T) {
mockCSV := `example.com
example.org
example.net`
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(mockCSV))
}))
defer server.Close()
websites, err := downloadWebsites(server.URL)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
expected := []string{"https://example.com", "https://example.org", "https://example.net"}
for i, site := range expected {
if websites[i] != site {
t.Errorf("Expected %v, got %v", site, websites[i])
}
}
}
func TestDownloadWebsites_Error(t *testing.T) {
_, err := downloadWebsites("http://nonexistent.url")
if err == nil {
t.Fatalf("Expected an error, got none")
}
}
func TestSelectRandomWebsites(t *testing.T) {
websites := []string{"https://example1.com", "https://example2.com", "https://example3.com"}
selected := selectRandomWebsites(websites, 2)
if len(selected) != 2 {
t.Errorf("Expected 2 websites, got %d", len(selected))
}
for _, site := range selected {
if !contains(websites, site) {
t.Errorf("Unexpected website selected: %v", site)
}
}
}
func contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
}
func TestRun(t *testing.T) {
mockDownloadWebsites := func(url string) ([]string, error) {
return []string{"https://example.com", "https://example.org"}, nil
}
mockSendRequest := func(client *http.Client, website string) {
// Simulate a successful request
}
go func() {
run(mockDownloadWebsites, mockSendRequest)
}()
time.Sleep(2 * time.Second)
}
func TestMetrics(t *testing.T) {
mockDownloadWebsites := func(url string) ([]string, error) {
return []string{"https://example.com", "https://example.org"}, nil
}
mockSendRequest := func(client *http.Client, website string) {
incrementTotalRequests()
if website == "https://example.com" {
incrementSuccessfulRequests(website)
} else {
incrementFailedRequests(website)
}
}
go func() {
run(mockDownloadWebsites, mockSendRequest)
}()
time.Sleep(2 * time.Second)
mu.Lock()
defer mu.Unlock()
if totalRequests != 2 {
t.Errorf("Expected 2 total requests, got %d", totalRequests)
}
if successfulRequests["https://example.com"] != 1 {
t.Errorf("Expected 1 successful request for https://example.com, got %d", successfulRequests["https://example.com"])
}
if failedRequests["https://example.org"] != 1 {
t.Errorf("Expected 1 failed request for https://example.org, got %d", failedRequests["https://example.org"])
}
}