This repository has been archived by the owner on Jul 6, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathApplication_test.go
181 lines (140 loc) · 3.79 KB
/
Application_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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package aero_test
import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"os"
"syscall"
"testing"
"time"
"github.com/aerogo/aero"
"github.com/aerogo/http/client"
"github.com/akyoto/assert"
)
const helloWorld = "Hello World"
func TestApplicationAny(t *testing.T) {
app := aero.New()
app.Any("/", func(ctx aero.Context) error {
return ctx.Text(helloWorld)
})
app.OnError(func(ctx aero.Context, err error) {
t.Fatal(err)
})
methods := []string{
"GET",
"POST",
"PUT",
"DELETE",
}
for _, method := range methods {
// Test existing route
request := httptest.NewRequest(method, "/", nil)
response := httptest.NewRecorder()
app.ServeHTTP(response, request)
assert.Equal(t, response.Code, http.StatusOK)
assert.Equal(t, response.Body.String(), helloWorld)
// Test non-existing route
request = httptest.NewRequest(method, "/404", nil)
response = httptest.NewRecorder()
app.ServeHTTP(response, request)
assert.Equal(t, response.Code, http.StatusNotFound)
assert.Equal(t, response.Body.String(), "")
}
}
func TestApplicationRewrite(t *testing.T) {
app := aero.New()
app.Get("/hello", func(ctx aero.Context) error {
return ctx.Text(helloWorld)
})
// Rewrite route
app.Rewrite(func(ctx aero.RewriteContext) {
if ctx.Path() == "/" {
ctx.SetPath("/hello")
return
}
})
response := test(app, "/")
assert.Equal(t, response.Code, http.StatusOK)
assert.Equal(t, response.Body.String(), helloWorld)
}
func TestApplicationLoadConfig(t *testing.T) {
app := aero.New()
workingDirectory, _ := os.Getwd()
err := os.Chdir("testdata")
assert.Nil(t, err)
app.Load()
err = os.Chdir(workingDirectory)
assert.Nil(t, err)
}
func TestApplicationRun(t *testing.T) {
start := time.Now()
app := aero.New()
// When frontpage is requested, kill the server
app.Get("/", func(ctx aero.Context) error {
return ctx.HTML(helloWorld)
})
// When the server is started, we request the frontpage
app.OnStart(func() {
_, err := client.Get(fmt.Sprintf("http://localhost:%d/", app.Config.Ports.HTTP)).End()
assert.Nil(t, err)
err = syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
assert.Nil(t, err)
})
// When the server ends, check elapsed time
app.OnEnd(func() {
elapsed := time.Since(start)
assert.Equal(t, elapsed < 2*time.Second, true)
})
// Run
app.Run()
}
func TestApplicationRunHTTPS(t *testing.T) {
app := aero.New()
app.Security.Load("testdata/fullchain.pem", "testdata/privkey.pem")
app.Get("/", func(ctx aero.Context) error {
return ctx.HTML(helloWorld)
})
// When the server is started, we request the frontpage
app.OnStart(func() {
_, err := client.Get(fmt.Sprintf("http://localhost:%d/", app.Config.Ports.HTTP)).End()
assert.Nil(t, err)
_, err = client.Get(fmt.Sprintf("https://localhost:%d/", app.Config.Ports.HTTPS)).End()
assert.Nil(t, err)
go func() {
err = syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
assert.Nil(t, err)
}()
})
// Run
app.Run()
}
func TestApplicationUnavailablePort(t *testing.T) {
defer func() {
_ = recover()
// assert.NotNil(t, r)
// assert.Contains(t, r.(error).Error(), "bind: permission denied")
}()
app := aero.New()
app.Config.Ports.HTTP = 80
app.Config.Ports.HTTPS = 443
app.ListenAndServe()
}
// test sends a request to the server and returns the response.
func test(app http.Handler, route string) *httptest.ResponseRecorder {
request := httptest.NewRequest("GET", route, nil)
request.Header.Set("Accept-Encoding", "gzip")
response := httptest.NewRecorder()
app.ServeHTTP(response, request)
return response
}
func TestApplicationOnError(t *testing.T) {
app := aero.New()
app.Get("/", func(ctx aero.Context) error {
return errors.New("something happened")
})
app.OnError(func(ctx aero.Context, err error) {
assert.Equal(t, err.Error(), "something happened")
})
test(app, "/")
}