-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp_test.go
113 lines (91 loc) · 2.22 KB
/
app_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
package pulse
import (
"net/http"
"net/http/httptest"
"testing"
"time"
)
var app *Pulse
func init() {
app = New(Config{
AppName: "Test App",
})
}
func TestNew(t *testing.T) {
app := New()
if app.config.AppName != DefaultAppName {
t.Errorf("AppName: expected %q, actual %q", DefaultAppName, app.config.AppName)
}
if app.config.Network != DefaultNetwork {
t.Errorf("Network: expected %q, actual %q", DefaultNetwork, app.config.Network)
}
// Test New() function with custom config
app = New(Config{
AppName: "Test App",
Network: "udp",
})
if app.config.AppName != "Test App" {
t.Errorf("AppName: expected %q, actual %q", "Test App", app.config.AppName)
}
if app.config.Network != "udp" {
t.Errorf("Network: expected %q, actual %q", "udp", app.config.Network)
}
}
func TestPulse_startupMessage(t *testing.T) {
app := New(Config{
AppName: "Test App",
})
addr := "localhost:8080"
expected := "=> Server started on <" + addr + ">\n" +
"=> App Name: " + app.config.AppName + "\n" +
"=> Press CTRL+C to stop\n"
actual := app.startupMessage(addr)
if actual != expected {
t.Errorf("startupMessage: expected %q, actual %q", expected, actual)
}
}
func TestRouterHandler2(t *testing.T) {
router := NewRouter()
router.Get("/", func(ctx *Context) error {
ctx.String("Hello, World!")
return nil
})
handler := RouterHandler(router)
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
}
func TestPulse_Run(t *testing.T) {
app := New(Config{
AppName: "test-app",
})
go app.Run(":9090")
// Wait for server to start
time.Sleep(time.Second)
_, err := http.Get("http://localhost:9090/")
if err != nil {
t.Errorf("failed to make GET request: %v", err)
}
err = app.Stop()
if err != nil {
t.Errorf("failed to stop server: %v", err)
}
}
func TestPulse_Stop(t *testing.T) {
app := New()
go app.Run(":9090")
// Wait for server to start
time.Sleep(time.Second)
err := app.Stop()
if err != nil {
t.Errorf("failed to stop server: %v", err)
}
// Make sure server is stopped by attempting to make a GET request
_, err = http.Get("http://localhost:9090/")
if err == nil {
t.Errorf("expected error, got nil")
}
}