This repository was archived by the owner on Aug 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsiris_test.go
522 lines (438 loc) · 14.8 KB
/
siris_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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
// +build testsiris
package siris
import (
stdContext "context"
"crypto/tls"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"path"
"path/filepath"
"strconv"
"strings"
"sync"
"syscall"
"testing"
"time"
"gopkg.in/gavv/httpexpect.v1"
"github.com/go-siris/siris/cache"
"github.com/go-siris/siris/context"
"github.com/go-siris/siris/core/errors"
"github.com/go-siris/siris/core/host"
"github.com/go-siris/siris/core/nettools"
"github.com/go-siris/siris/core/router"
"github.com/go-siris/siris/view"
)
const (
debug = false
expectedFoundResponse = "body{font-size: 30px}\n"
expectedFoundResponse2 = "1234test4321"
)
var (
rwmu = sync.RWMutex{}
args []string
runLetsEncrypt bool
sslCert string
sslKey string
staticDir string
)
func TestSiris(t *testing.T) {
initial()
app := Default()
app.Configure(WithJSONInteratorReplacement, EnableReuseport, EnableQUICSupport, WithTimeFormat(time.RFC3339), WithCharset("UTF-8"), WithRemoteAddrHeader("X-Real-Ip"), WithoutRemoteAddrHeader("X-No-Real-Ip"), WithOtherValue("AppName", "SIRIS"))
app.AttachView(view.HTML("./", ".html").Binary(get_files, get_names))
app.OnErrorCode(StatusPaymentRequired, func(ctx context.Context) {
ctx.IsAjax()
ctx.Application()
ctx.ClientSupportsGzip()
ctx.GetStatusCode()
ctx.GetContentType()
ctx.GetHeader("User-Agent")
ctx.GetCookie("lang")
ctx.SetCookieKV("lang", "value")
ctx.SetMaxRequestBodySize(1234567890)
})
app.OnAnyErrorCode(func(ctx context.Context) {
fmt.Printf("OnAnyErrorCode: %d %#v\n\n", ctx.GetStatusCode(), ctx.Err())
errors.PrintAndReturnErrors(ctx.Err(), app.Logger().Infof)
ctx.Next()
})
app.Favicon(staticDir)
app.Get("/view", context.LimitRequestBodySize(1234567890), func(ctx context.Context) {
ctx.View("view/parent.html")
})
app.Get("/", func(ctx context.Context) {
ctx.Text("hi")
})
app.Post("/", func(ctx context.Context) {
ctx.Text("hi")
})
app.Delete("/", func(ctx context.Context) {
ctx.Text("hi")
})
app.Put("/", func(ctx context.Context) {
ctx.Text("hi")
})
app.Head("/", func(ctx context.Context) {
ctx.Text("hi")
})
app.Options("/", func(ctx context.Context) {
ctx.Text("hi")
})
app.Patch("/", func(ctx context.Context) {
ctx.Text("hi")
})
app.Connect("/", func(ctx context.Context) {
ctx.Text("hi")
})
app.Trace("/", func(ctx context.Context) {
ctx.Text("hi")
})
party := app.Party("/party")
{
party.Get("/", func(ctx context.Context) {
ctx.Text("party")
})
party.Post("/", func(ctx context.Context) {
ctx.Text("party")
})
party.Delete("/", func(ctx context.Context) {
ctx.Text("party")
})
party.Put("/", func(ctx context.Context) {
ctx.Text("party")
})
party.Head("/", func(ctx context.Context) {
ctx.Text("party")
})
party.Options("/", func(ctx context.Context) {
ctx.Text("party")
})
party.Patch("/", func(ctx context.Context) {
ctx.Text("party")
})
party.Connect("/", func(ctx context.Context) {
ctx.Text("party")
})
party.Trace("/", func(ctx context.Context) {
ctx.Text("party")
})
}
app.PartyFunc("/partyfunc", func(u router.Party) {
u.Get("/", func(ctx context.Context) {
ctx.Text("partyfunc")
})
u.Post("/", func(ctx context.Context) {
ctx.Text("partyfunc")
})
u.Delete("/", func(ctx context.Context) {
ctx.Text("partyfunc")
})
})
app.Any("/any", func(ctx context.Context) {
ctx.Text("/any")
})
app.Many("/many", []string{MethodGet, MethodHead, MethodPost}, func(ctx context.Context) {
ctx.Text("/many")
})
// you can use the "string" type which is valid for a single path parameter that can be anything.
app.Get("/username/{name}", func(ctx context.Context) {
ctx.Writef("Hello %s", ctx.Params().Get("name"))
}) // type is missing = {name:string}
// Let's register our first macro attached to int macro type.
// "min" = the function
// "minValue" = the argument of the function
// func(string) bool = the macro's path parameter evaluator, this executes in serve time when
// a user requests a path which contains the :int macro type with the min(...) macro parameter function.
app.Macros().Int.RegisterFunc("min", func(minValue int) func(string) bool {
// do anything before serve here [...]
// at this case we don't need to do anything
return func(paramValue string) bool {
n, err := strconv.Atoi(paramValue)
if err != nil {
return false
}
return n >= minValue
}
})
// http://go-siris.com:8080/profile/id>=1
// this will throw 404 even if it's found as route on : /profile/0, /profile/blabla, /profile/-1
// macro parameter functions are optional of course.
app.Get("/profile/{id:int min(1)}", func(ctx context.Context) {
// second parameter is the error but it will always nil because we use macros,
// the validaton already happened.
id, _ := ctx.Params().GetInt("id")
ctx.Writef("Hello id: %d", id)
})
// to change the error code per route's macro evaluator:
app.Get("/profile/{id:int min(1)}/friends/{friendid:int min(1) else 504}", func(ctx context.Context) {
id, _ := ctx.Params().GetInt("id")
friendid, _ := ctx.Params().GetInt("friendid")
ctx.Writef("Hello id: %d looking for friend id: %d", id, friendid)
}) // this will throw e 504 error code instead of 404 if all route's macros not passed.
// http://go-siris.com:8080/game/a-zA-Z/level/0-9
// remember, alphabetical is lowercase or uppercase letters only.
app.Get("/game/{name:alphabetical}/level/{level:int}", func(ctx context.Context) {
ctx.Writef("name: %s | level: %s", ctx.Params().Get("name"), ctx.Params().Get("level"))
})
app.Get("/lowercase/static", func(ctx context.Context) {
ctx.Writef("static and dynamic paths are not conflicted anymore!")
})
// let's use a trivial custom regexp that validates a single path parameter
// which its value is only lowercase letters.
// http://go-siris.com:8080/lowercase/anylowercase
app.Get("/lowercase/{name:string regexp(^[a-z]+)}", func(ctx context.Context) {
ctx.Writef("name should be only lowercase, otherwise this handler will never executed: %s", ctx.Params().Get("name"))
})
// http://go-siris.com:8080/single_file/app.js
app.Get("/single_file/{myfile:file}", func(ctx context.Context) {
ctx.Writef("file type validates if the parameter value has a form of a file name, got: %s", ctx.Params().Get("myfile"))
})
// http://go-siris.com:8080/myfiles/any/directory/here/
// this is the only macro type that accepts any number of path segments.
app.Get("/myfiles/{directory:path}", func(ctx context.Context) {
ctx.Writef("path type accepts any number of path segments, path after /myfiles/ is: %s", ctx.Params().Get("directory"))
}) // for wildcard path (any number of path segments) without validation you can use:
app.Get("/cache", cache.WrapHandler(func(ctx context.Context) {
ctx.Text("hi cache " + time.Now().String())
}, 2*time.Minute))
// file in staticDir blob.css
app.StaticWeb("/static1/", staticDir)
app.StaticServe(staticDir, "/static2/")
app.StaticContent("/static3", "text/css", []byte(expectedFoundResponse))
app.StaticEmbedded("/static4/", "/", get_files, get_names)
app.Get("/get-siris", func(ctx context.Context) {
ctx.Text("siris")
})
app.Get("/get-payment", func(ctx context.Context) {
ctx.StatusCode(StatusPaymentRequired)
})
app.Get("/get-gzip", func(ctx context.Context) {
ctx.Gzip(ctx.ClientSupportsGzip())
ctx.Text("Gzip")
})
go func() {
ch := make(chan os.Signal, 1)
signal.Notify(ch,
// kill -SIGINT XXXX or Ctrl+c
os.Interrupt,
syscall.SIGINT, // register that too, it should be ok
// os.Kill is equivalent with the syscall.Kill
os.Kill,
syscall.SIGKILL, // register that too, it should be ok
// kill -SIGTERM XXXX
syscall.SIGTERM,
)
select {
case <-ch:
println("shutdown...")
timeout := 5 * time.Second
ctx, cancel := stdContext.WithTimeout(stdContext.Background(), timeout)
defer cancel()
app.Shutdown(ctx)
}
}()
app.Build()
app.GetRoutes()
app.GetRoute("HEAD/")
app.None("/none-active", func(ctx context.Context) {
ctx.Text("none-active route")
})
loggerP := serverErrLogger{app.Logger()}
loggerP.Write([]byte("Logger: Start Servers"))
app.ConfigureHost(configureHosts)
go app.Run(Addr("go-siris.com:9080"), WithoutBanner, WithoutInterruptHandler)
if runLetsEncrypt {
go app.Run(AutoTLS("go-siris.com:443"), WithoutBanner, WithoutInterruptHandler)
} else {
go app.Run(TLS("go-siris.com:9443", sslCert, sslKey), WithoutBanner, WithoutInterruptHandler)
}
unicl, err := nettools.UNIX("/tmp/srv.sock", 0777) // see its code to see how you can manually create a new file listener, it's easy.
if err != nil {
panic(err)
}
go app.Run(Listener(unicl))
loggerP.Write([]byte("Logger: Servers started"))
time.Sleep(time.Duration(5 * time.Second))
for _, s := range app.Hosts {
s.RegisterOnErrorHook(func(err error) {
fmt.Println("RegisterOnErrorHook: " + err.Error())
})
}
time.Sleep(time.Duration(5 * time.Second))
client := newTester(t, "http://go-siris.com:9080", nil)
testClient(client)
clientTls := newTester(t, "https://go-siris.com:9443", nil)
testClient(clientTls)
time.Sleep(time.Duration(5 * time.Second))
app.Shutdown(stdContext.TODO())
time.Sleep(time.Duration(5 * time.Second))
}
func configureHosts(su *host.Supervisor) {
// here we have full access to the host that will be created
// inside the `app.Run` or `app.NewHost` function .
//
// we're registering a shutdown "event" callback here:
su.RegisterOnShutdownHook(func() {
println("server is closed")
})
su.RegisterOnErrorHook(func(err error) {
println("error:" + err.Error())
})
su.RegisterOnServeHook(func(th host.TaskHost) {
println("Server Started")
println(th.HostURL())
println(th.Hostname())
})
// su.RegisterOnError
// su.RegisterOnServe
}
func initial() {
for _, arg := range os.Args {
switch {
case arg == "--letsencrypt":
runLetsEncrypt = true
default:
args = append(args, arg)
}
}
os.Args = args
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
fmt.Println(dir)
sslCert = path.Join(dir, "coverage-tests", "fixtures", "server.crt")
sslKey = path.Join(dir, "coverage-tests", "fixtures", "server.key")
staticDir = path.Join(dir, "coverage-tests", "fixtures", "static")
fmt.Println(staticDir)
}
var files2017 = map[string]string{
"views/parent.html": `<!DOCTYPE html>
<html>
<head>
<title>Iris with Pongo2 template engine</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
</head>
<body>
<h1>demo</h1>
</body>
</html>`,
"css/css.css": "body{}",
}
func get_names() []string {
var res []string
for name := range files2017 {
res = append(res, name)
}
return res
}
func get_files(name string) ([]byte, error) {
content, exists := files2017[name]
if !exists {
return nil, os.ErrNotExist
}
return []byte(content), nil
}
func newTester(t *testing.T, baseURL string, handler http.Handler) *httpexpect.Expect {
var transporter http.RoundTripper
if strings.HasPrefix(baseURL, "http") { // means we are testing real serve time
transporter = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
} else { // means we are testing the handler itself
transporter = httpexpect.NewBinder(handler)
}
testConfiguration := httpexpect.Config{
BaseURL: baseURL,
Client: &http.Client{
Transport: transporter,
Jar: httpexpect.NewJar(),
},
Reporter: httpexpect.NewAssertReporter(t),
}
if debug {
testConfiguration.Printers = []httpexpect.Printer{
httpexpect.NewDebugPrinter(t, true),
}
}
return httpexpect.WithConfig(testConfiguration)
}
func testClient(e *httpexpect.Expect) {
e.GET("/static1/").Expect().Status(StatusNotFound)
e.GET("/static1/test.css").Expect().Status(StatusOK).
Body().Equal(expectedFoundResponse)
e.GET("/static1/demo/test.txt").Expect().Status(StatusOK).
Body().Equal(expectedFoundResponse2)
e.GET("/static2/").Expect().Status(StatusNotFound)
e.GET("/static2/test.css").Expect().Status(StatusOK).
Body().Equal(expectedFoundResponse)
e.GET("/static2/demo/test.txt").Expect().Status(StatusOK).
Body().Equal(expectedFoundResponse2)
e.GET("/static3").Expect().Status(StatusOK).
Body().Equal(expectedFoundResponse)
e.GET("/static4/").Expect().Status(StatusNotFound)
bytes, _ := get_files("css/css.css")
e.GET("/static4/css/css.css").Expect().Status(StatusOK).
Body().Equal(string(bytes))
bytes, _ = get_files("views/parent.html")
e.GET("/static4/views/parent.html").Expect().Status(StatusOK).
Body().Equal(string(bytes))
e.GET("/get-siris").Expect().Status(StatusOK)
e.GET("/cache").Expect().Status(StatusOK)
time.Sleep(time.Duration(1 * time.Second))
e.GET("/cache").Expect().Status(StatusOK)
time.Sleep(time.Duration(1 * time.Second))
e.GET("/").Expect().Status(StatusOK)
e.POST("/").Expect().Status(StatusOK)
e.PUT("/").Expect().Status(StatusOK)
e.DELETE("/").Expect().Status(StatusOK)
e.OPTIONS("/").Expect().Status(StatusOK)
e.HEAD("/").Expect().Status(StatusOK)
e.PATCH("/").Expect().Status(StatusOK)
e.GET("/party/").Expect().Status(StatusOK)
e.POST("/party/").Expect().Status(StatusOK)
e.PUT("/party/").Expect().Status(StatusOK)
e.DELETE("/party/").Expect().Status(StatusOK)
e.OPTIONS("/party/").Expect().Status(StatusOK)
e.HEAD("/party/").Expect().Status(StatusOK)
e.PATCH("/party/").Expect().Status(StatusOK)
e.GET("/username/Bob").Expect().Status(StatusOK).
Body().Equal("Hello Bob")
e.GET("/profile/1029").
Expect().Status(StatusOK).
Body().Equal("Hello id: 1029")
e.GET("/profile/0").
Expect().Status(StatusNotFound)
e.GET("/profile/1029/friends/4321").
Expect().Status(StatusOK).
Body().Equal("Hello id: 1029 looking for friend id: 4321")
e.GET("/profile/1029/friends/0").
Expect().Status(StatusGatewayTimeout)
e.GET("/game/Siris/level/302").
Expect().Status(StatusOK).
Body().Equal("name: Siris | level: 302")
e.GET("/lowercase/static").
Expect().Status(StatusOK).
Body().Equal("static and dynamic paths are not conflicted anymore!")
e.GET("/lowercase/staticlow").
Expect().Status(StatusOK).
Body().Equal("name should be only lowercase, otherwise this handler will never executed: staticlow")
e.GET("/lowercase/killer").
Expect().Status(StatusOK).
Body().Equal("name should be only lowercase, otherwise this handler will never executed: killer")
e.GET("/single_file/whatthehell.zip").
Expect().Status(StatusOK).
Body().Equal("file type validates if the parameter value has a form of a file name, got: whatthehell.zip")
e.GET("/myfiles/whatthehell/get/me").
Expect().Status(StatusOK).
Body().Equal("path type accepts any number of path segments, path after /myfiles/ is: whatthehell/get/me")
e.GET("/notfound").Expect().Status(StatusNotFound)
e.GET("/favicon.ico").Expect().Status(StatusOK)
e.GET("/get-payment").Expect().Status(StatusPaymentRequired)
e.GET("/get-gzip").Expect().Status(StatusOK)
}