-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
59 lines (47 loc) · 1.41 KB
/
main_test.go
File metadata and controls
59 lines (47 loc) · 1.41 KB
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
package main
import (
"crypto/tls"
"io/ioutil"
"net/http"
"testing"
"time"
)
func TestEchoServer(t *testing.T) {
go main() // Start the echo server in a goroutine
time.Sleep(2 * time.Second) // Give the server a moment to start
// Test the echo functionality
conn, err := tls.Dial("tcp", "localhost:20080", &tls.Config{InsecureSkipVerify: true})
if err != nil {
t.Fatalf("Failed to connect to server: %v", err)
}
defer conn.Close()
message := "Hello, Echo Server!"
_, err = conn.Write([]byte(message))
if err != nil {
t.Fatalf("Failed to send message: %v", err)
}
buffer := make([]byte, len(message))
_, err = conn.Read(buffer)
if err != nil {
t.Fatalf("Failed to read message: %v", err)
}
if string(buffer) != message {
t.Fatalf("Expected %q but got %q", message, string(buffer))
}
// Test the metrics endpoint
resp, err := http.Get("http://localhost:9090/metrics")
if err != nil {
t.Fatalf("Failed to get metrics: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("Expected status code 200 but got %d", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Failed to read response body: %v", err)
}
if len(body) == 0 {
t.Fatalf("Expected non-empty response body")
}
}