-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
57 lines (50 loc) · 1.67 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
package main
import (
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestGetEndpointsFromURL(t *testing.T) {
// Mock HTTP server
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`
<a href="http://example.com/page1">Page 1</a>
<a href="http://example.com/page2">Page 2</a>
<link rel="stylesheet" href="http://example.com/style.css">
<img src="http://example.com/image.jpg">
`))
}))
defer mockServer.Close()
url := mockServer.URL
endpoints, err := getEndpointsFromURL(url)
if err != nil {
t.Errorf("getEndpointsFromURL(%s) returned an error: %v", url, err)
}
expectedEndpoints := []string{"http://example.com/page1", "http://example.com/page2"}
for i, expected := range expectedEndpoints {
if endpoints[i] != expected {
t.Errorf("Expected endpoint %s at index %d, got %s", expected, i, endpoints[i])
}
}
}
func TestCheckLatency(t *testing.T) {
// Mock HTTP server
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(2 * time.Second) // Simulate latency
w.WriteHeader(http.StatusOK)
}))
defer mockServer.Close()
url := mockServer.URL
timeout := 5 * time.Second // Increase timeout duration
latency, err := checkLatency(url, timeout)
if err != nil {
t.Errorf("checkLatency(%s, %v) returned an error: %v", url, timeout, err)
}
expected := time.Duration(2 * time.Second)
// Allow for a small difference in latency (e.g., 100 milliseconds)
maxDeviation := 100 * time.Millisecond
if latency < expected-maxDeviation || latency > expected+maxDeviation {
t.Errorf("Expected latency around %v, but got %v", expected, latency)
}
}