-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_e2e_test.go
More file actions
87 lines (78 loc) · 3.36 KB
/
Copy pathserver_e2e_test.go
File metadata and controls
87 lines (78 loc) · 3.36 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
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
//go:build e2e
// By adding go:build e2e to the top of the file, these tests do not run with `go test`,
// but are invoked with `go test -tags e2e`
package web
import (
"fmt"
"net/http"
"testing"
)
func TestServerE2e(t *testing.T) {
h := NewHTTPServer()
h.AddRoute(http.MethodGet, "/*", func(ctx *Context) {
ctx.Resp.WriteHeader(http.StatusOK)
ctx.Resp.Write([]byte("what's your path?"))
})
h.AddRoute(http.MethodGet, "/user/*/home", func(ctx *Context) {
ctx.Resp.WriteHeader(http.StatusOK)
ctx.Resp.Write([]byte(fmt.Sprintf("hello /user/*/home (%s)\n", ctx.Req.URL.Path)))
})
h.AddRoute(http.MethodGet, "/user/nobody/home", func(ctx *Context) {
ctx.Resp.WriteHeader(http.StatusOK)
ctx.Resp.Write([]byte("nobody lives here"))
})
// trailing wildcard VS more specific route
// only something like "/user/something/home" gets matched with "/user/*/home".
// "/user/*/not_home" fallback to :/user/*".
// we DO NOT support "user/*/not_home/something" fallback to "/user/*
h.AddRoute(http.MethodGet, "/user/*", func(ctx *Context) {
ctx.Resp.WriteHeader(http.StatusOK)
ctx.Resp.Write([]byte(fmt.Sprintf("/user/* %s\n", ctx.Req.URL.Path)))
})
h.AddRoute(http.MethodGet, "/index", func(ctx *Context) {
ctx.Resp.WriteHeader(http.StatusOK)
ctx.Resp.Write([]byte("hello /index"))
})
// param
h.AddRoute(http.MethodGet, "/index/:msg1/bruh/:msg2", func(ctx *Context) {
ctx.Resp.WriteHeader(http.StatusOK)
ctx.Resp.Write([]byte(fmt.Sprintf("hello /index/:msg1/bruh/:msg2 [%v]", *ctx.Param)))
})
// trailing wildcard matches anything after it --> /a/(b/c/d/e/f...)
h.AddRoute(http.MethodGet, "/a/*", func(ctx *Context) {
ctx.Resp.WriteHeader(http.StatusOK)
ctx.Resp.Write([]byte(fmt.Sprintf("trailing wildcard /a/* %s\n", ctx.Req.URL.Path)))
})
// not a trailing wildcard
h.AddRoute(http.MethodGet, "/aa/*/bb", func(ctx *Context) {
ctx.Resp.WriteHeader(http.StatusOK)
ctx.Resp.Write([]byte(fmt.Sprintf("/aa/*/bb %s\n", ctx.Req.URL.Path)))
})
// /aa/*/cc/* (also match anything left)
h.AddRoute(http.MethodGet, "/aa/*/cc/*", func(ctx *Context) {
ctx.Resp.WriteHeader(http.StatusOK)
ctx.Resp.Write([]byte(fmt.Sprintf("/aa/*/cc/* %s\n", ctx.Req.URL.Path)))
})
// Regexp
h.AddRoute(http.MethodGet, "/regexp/:key((\\d+))", func(ctx *Context) {
ctx.Resp.WriteHeader(http.StatusOK)
ctx.Resp.Write([]byte(fmt.Sprintf("Route: /regexp/:key((\\d+)) \nPath: %s", ctx.Req.URL.Path)))
})
h.AddRoute(http.MethodGet, "/regexp/user/:role((.+)_.+)", func(ctx *Context) {
ctx.Resp.WriteHeader(http.StatusOK)
ctx.Resp.Write([]byte(fmt.Sprintf("Route: /regexp/:role((.+)_.+) \nPath: %s\nParams:%v", ctx.Req.URL.Path, ctx.Param)))
})
h.AddRoute(http.MethodGet, "/regexp/multi1/:key1:key2:key3((\\d+)([a-z]+)(\\d+))", func(ctx *Context) {
ctx.Resp.WriteHeader(http.StatusOK)
ctx.Resp.Write([]byte(fmt.Sprintf("Route: /regexp/multi/:key1:key2:key3((\\d+)([a-z]+)(\\d+))\nPath: %s\nParams: %v", ctx.Req.URL.Path, ctx.Param)))
})
h.AddRoute(http.MethodGet, "/regexp/multi2/:key2:key3(\\d+([a-z]+)(\\d+))", func(ctx *Context) {
ctx.Resp.WriteHeader(http.StatusOK)
ctx.Resp.Write([]byte(fmt.Sprintf("Route: /regexp/multi/:key1:key2:key3(\\d+([a-z]+)(\\d+))\nPath: %s\nParams: %v", ctx.Req.URL.Path, ctx.Param)))
})
// Usage 1: delegate to http package
// http.ListenAndServe("8081", h)
// http.ListenAndServeTLS("443", "", "", h)
// Usage 2: self managed
h.Start(":8001")
}