-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_test.go
72 lines (62 loc) · 2.02 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
package main
import (
"fmt"
"net/http"
"testing"
"time"
"github.com/r3labs/sse/v2"
)
// TestCheckURLResponse tests the checkURLResponse function.
func TestCheckURLResponse(t *testing.T) {
// Start a simple HTTP server for testing purposes
go func() {
http.HandleFunc("/ok", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
http.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
})
http.ListenAndServe(":8081", nil)
}()
time.Sleep(1 * time.Second) // Wait for the server to start
okService := Service{
Name: "OKService",
Endpoint: "http://localhost:8081/ok",
ExpectedCode: http.StatusOK,
}
// Test the function with a URL that returns 200 OK
urlOK := "http://localhost:8081/ok"
resultOK, err := checkService(okService)
if err != nil || !resultOK {
t.Errorf("Expected checkService to return true and no error for URL %s, but got %v and %v", urlOK, resultOK, err)
}
errorService := Service{
Name: "ErrorService",
Endpoint: "http://localhost:8081/error",
ExpectedCode: http.StatusOK,
}
// Test the function with a URL that returns an error status code
urlError := "http://localhost:8081/error"
resultError, err := checkService(errorService)
if err == nil || resultError {
t.Errorf("Expected checkService to return false and an error for URL %s, but got %v and %v", urlError, resultError, err)
}
}
// TestSendStream tests the sendStream function.
func TestSendStream(t *testing.T) {
server := sse.New()
server.CreateStream("messages")
testService := Service{
Name: "TestService",
Endpoint: "http://localhost:8081/test",
Frequency: 1 * time.Second,
ExpectedCode: http.StatusOK,
}
// Test the sendStream function with a test service and no error
err := fmt.Errorf("No error")
sendStream(server, testService, err)
// Test the sendStream function with a test service and an error
err = fmt.Errorf("Test error")
sendStream(server, testService, err)
// TODO
}