Skip to content

Commit e987158

Browse files
authored
Updates: Add test env (#19)
* add tests files * remove flag * handle errors * fix set & get cookies method * fix ci error
1 parent bfc49fd commit e987158

File tree

10 files changed

+476
-132
lines changed

10 files changed

+476
-132
lines changed

.github/workflows/codecov.yml

-1
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,3 @@ jobs:
3131
uses: codecov/codecov-action@v3
3232
with:
3333
working-directory: main
34-
flags: unittests

app.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package pulse
22

33
import (
4+
"errors"
45
"fmt"
56
"github.com/common-nighthawk/go-figure"
67
"net"
@@ -68,15 +69,20 @@ func (f *Pulse) Run(address string) {
6869
// start server
6970
err = f.server.Serve(listener)
7071
if err != nil {
71-
panic(fmt.Errorf("failed to serve: %v", err))
72+
fmt.Errorf("failed to start server on %s: %v", listener.Addr().String(), err)
7273
}
7374
}
7475

75-
func (f *Pulse) Stop() {
76+
func (f *Pulse) Stop() error {
77+
if f.server == nil {
78+
return errors.New("server not running")
79+
}
7680
err := f.server.Shutdown(nil)
7781
if err != nil {
78-
return
82+
return fmt.Errorf("failed to stop server: %v", err)
7983
}
84+
f.server = nil
85+
return nil
8086
}
8187

8288
func (f *Pulse) startupMessage(addr string) string {

app_test.go

+101
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,110 @@
11
package pulse
22

3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"testing"
7+
"time"
8+
)
9+
310
var app *Pulse
411

512
func init() {
613
app = New(Config{
714
AppName: "Test App",
815
})
916
}
17+
18+
func TestNew(t *testing.T) {
19+
app := New()
20+
if app.config.AppName != DefaultAppName {
21+
t.Errorf("AppName: expected %q, actual %q", DefaultAppName, app.config.AppName)
22+
}
23+
if app.config.Network != DefaultNetwork {
24+
t.Errorf("Network: expected %q, actual %q", DefaultNetwork, app.config.Network)
25+
}
26+
27+
// Test New() function with custom config
28+
app = New(Config{
29+
AppName: "Test App",
30+
Network: "udp",
31+
})
32+
if app.config.AppName != "Test App" {
33+
t.Errorf("AppName: expected %q, actual %q", "Test App", app.config.AppName)
34+
}
35+
if app.config.Network != "udp" {
36+
t.Errorf("Network: expected %q, actual %q", "udp", app.config.Network)
37+
}
38+
}
39+
40+
func TestPulse_startupMessage(t *testing.T) {
41+
app := New(Config{
42+
AppName: "Test App",
43+
})
44+
45+
addr := "localhost:8080"
46+
expected := "=> Server started on <" + addr + ">\n" +
47+
"=> App Name: " + app.config.AppName + "\n" +
48+
"=> Press CTRL+C to stop\n"
49+
actual := app.startupMessage(addr)
50+
51+
if actual != expected {
52+
t.Errorf("startupMessage: expected %q, actual %q", expected, actual)
53+
}
54+
}
55+
56+
func TestRouterHandler2(t *testing.T) {
57+
router := NewRouter()
58+
router.Get("/", func(ctx *Context) error {
59+
ctx.String("Hello, World!")
60+
return nil
61+
})
62+
63+
handler := RouterHandler(router)
64+
65+
req, err := http.NewRequest("GET", "/", nil)
66+
if err != nil {
67+
t.Fatal(err)
68+
}
69+
70+
rr := httptest.NewRecorder()
71+
handler.ServeHTTP(rr, req)
72+
}
73+
74+
func TestPulse_Run_Stop(t *testing.T) {
75+
// Create a new Pulse instance.
76+
pulse := &Pulse{
77+
Router: NewRouter(),
78+
config: &Config{Network: "tcp"},
79+
server: &http.Server{},
80+
}
81+
82+
// Start the server.
83+
address := "localhost:9000"
84+
go pulse.Run(address)
85+
86+
// Wait for the server to start.
87+
time.Sleep(100 * time.Millisecond)
88+
89+
// Make a test request to verify that the server is running.
90+
req, err := http.NewRequest("GET", "http://"+address, nil)
91+
if err != nil {
92+
t.Fatalf("unexpected error creating request: %v", err)
93+
}
94+
respRecorder := httptest.NewRecorder()
95+
pulse.server.Handler.ServeHTTP(respRecorder, req)
96+
97+
err = pulse.Stop()
98+
if err != nil {
99+
t.Fatalf("failed to stop server: %v", err)
100+
}
101+
102+
// Wait for the server to stop.
103+
time.Sleep(3 * time.Second)
104+
105+
// Close the test request to ensure that all active connections are closed.
106+
err = respRecorder.Result().Body.Close()
107+
if err != nil {
108+
t.Fatalf("failed to close response body: %v", err)
109+
}
110+
}

constants/routingSigns_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package constants
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestConstants(t *testing.T) {
9+
expected := map[string]string{
10+
"WildcardSign": WildcardSign,
11+
"ParamSign": ParamSign,
12+
"OptionalSign": OptionalSign,
13+
}
14+
15+
actual := map[string]string{
16+
"WildcardSign": "*",
17+
"ParamSign": ":",
18+
"OptionalSign": "?",
19+
}
20+
21+
if !reflect.DeepEqual(expected, actual) {
22+
t.Errorf("Expected %v, but got %v", expected, actual)
23+
}
24+
}

context.go

+18-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type handlerFunc func(ctx *Context) error
1111

1212
type Context struct {
1313
ResponseWriter http.ResponseWriter
14+
Response http.Response
1415
Request *http.Request
1516
Params map[string]string
1617
paramValues []string
@@ -113,15 +114,28 @@ func (c *Context) SetCookie(cookie *Cookie) {
113114
HttpOnly: cookie.HTTPOnly,
114115
SameSite: cookie.SameSite,
115116
})
117+
118+
c.Cookies = append(c.Cookies, &http.Cookie{
119+
Name: cookie.Name,
120+
Value: cookie.Value,
121+
Path: cookie.Path,
122+
Domain: cookie.Domain,
123+
Expires: cookie.Expires,
124+
MaxAge: cookie.MaxAge,
125+
Secure: cookie.Secure,
126+
HttpOnly: cookie.HTTPOnly,
127+
SameSite: cookie.SameSite,
128+
})
116129
}
117130

118131
// GetCookie returns the value of the cookie with the given name.
119132
func (c *Context) GetCookie(name string) string {
120-
cookie, err := c.Request.Cookie(name)
121-
if err != nil {
122-
return ""
133+
for _, cookie := range c.Cookies {
134+
if cookie.Name == name {
135+
return cookie.Value
136+
}
123137
}
124-
return cookie.Value
138+
return ""
125139
}
126140

127141
// ClearCookie deletes the cookie with the given name.

0 commit comments

Comments
 (0)