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
|
package router
import (
"bufio"
"reflect"
"strings"
"testing"
"github.com/valyala/fasthttp"
)
type routerGrouper interface {
Group(string) *Group
ServeFiles(path string, rootPath string)
ServeFilesCustom(path string, fs *fasthttp.FS)
}
func assertGroup(t *testing.T, gs ...routerGrouper) {
for i, g := range gs {
g2 := g.Group("/")
v1 := reflect.ValueOf(g)
v2 := reflect.ValueOf(g2)
if v1.String() != v2.String() { // router -> group
if v1.Pointer() == v2.Pointer() {
t.Errorf("[%d] equal pointers: %p == %p", i, g, g2)
}
} else { // group -> subgroup
if v1.Pointer() != v2.Pointer() {
t.Errorf("[%d] mismatch pointers: %p != %p", i, g, g2)
}
}
if err := catchPanic(func() { g.Group("v999") }); err == nil {
t.Error("an error was expected when a path does not begin with slash")
}
if err := catchPanic(func() { g.Group("/v999/") }); err == nil {
t.Error("an error was expected when a path has a trailing slash")
}
if err := catchPanic(func() { g.Group("") }); err == nil {
t.Error("an error was expected with an empty path")
}
if err := catchPanic(func() { g.ServeFiles("static/{filepath:*}", "./") }); err == nil {
t.Error("an error was expected when a path does not begin with slash")
}
if err := catchPanic(func() {
g.ServeFilesCustom("", &fasthttp.FS{Root: "./"})
}); err == nil {
t.Error("an error was expected with an empty path")
}
}
}
func TestGroup(t *testing.T) {
r1 := New()
r2 := r1.Group("/boo")
r3 := r1.Group("/goo")
r4 := r1.Group("/moo")
r5 := r4.Group("/foo")
r6 := r5.Group("/foo")
assertGroup(t, r1, r2, r3, r4, r5, r6)
hit := false
r1.POST("/foo", func(ctx *fasthttp.RequestCtx) {
hit = true
ctx.SetStatusCode(fasthttp.StatusOK)
})
r2.POST("/bar", func(ctx *fasthttp.RequestCtx) {
hit = true
ctx.SetStatusCode(fasthttp.StatusOK)
})
r3.POST("/bar", func(ctx *fasthttp.RequestCtx) {
hit = true
ctx.SetStatusCode(fasthttp.StatusOK)
})
r4.POST("/bar", func(ctx *fasthttp.RequestCtx) {
hit = true
ctx.SetStatusCode(fasthttp.StatusOK)
})
r5.POST("/bar", func(ctx *fasthttp.RequestCtx) {
hit = true
ctx.SetStatusCode(fasthttp.StatusOK)
})
r6.POST("/bar", func(ctx *fasthttp.RequestCtx) {
hit = true
ctx.SetStatusCode(fasthttp.StatusOK)
})
r6.ServeFiles("/static/{filepath:*}", "./")
r6.ServeFS("/static/fs/{filepath:*}", fsTestFilesystem)
r6.ServeFilesCustom("/custom/static/{filepath:*}", &fasthttp.FS{Root: "./"})
uris := []string{
"POST /foo HTTP/1.1\r\n\r\n",
// testing router group - r2 (grouped from r1)
"POST /boo/bar HTTP/1.1\r\n\r\n",
// testing multiple router group - r3 (grouped from r1)
"POST /goo/bar HTTP/1.1\r\n\r\n",
// testing multiple router group - r4 (grouped from r1)
"POST /moo/bar HTTP/1.1\r\n\r\n",
// testing sub-router group - r5 (grouped from r4)
"POST /moo/foo/bar HTTP/1.1\r\n\r\n",
// testing multiple sub-router group - r6 (grouped from r5)
"POST /moo/foo/foo/bar HTTP/1.1\r\n\r\n",
// testing multiple sub-router group - r6 (grouped from r5) to serve files
"GET /moo/foo/foo/static/router.go HTTP/1.1\r\n\r\n",
// testing multiple sub-router group - r6 (grouped from r5) to serve fs
"GET /moo/foo/foo/static/fs/LICENSE HTTP/1.1\r\n\r\n",
// testing multiple sub-router group - r6 (grouped from r5) to serve files with custom settings
"GET /moo/foo/foo/custom/static/router.go HTTP/1.1\r\n\r\n",
}
for _, uri := range uris {
hit = false
assertWithTestServer(t, uri, r1.Handler, func(rw *readWriter) {
br := bufio.NewReader(&rw.w)
var resp fasthttp.Response
if err := resp.Read(br); err != nil {
t.Fatalf("Unexpected error when reading response: %s", err)
}
if !(resp.Header.StatusCode() == fasthttp.StatusOK) {
t.Fatalf("Status code %d, want %d", resp.Header.StatusCode(), fasthttp.StatusOK)
}
if !strings.Contains(uri, "static") && !hit {
t.Fatalf("Regular routing failed with router chaining. %s", uri)
}
})
}
assertWithTestServer(t, "POST /qax HTTP/1.1\r\n\r\n", r1.Handler, func(rw *readWriter) {
br := bufio.NewReader(&rw.w)
var resp fasthttp.Response
if err := resp.Read(br); err != nil {
t.Fatalf("Unexpected error when reading response: %s", err)
}
if !(resp.Header.StatusCode() == fasthttp.StatusNotFound) {
t.Errorf("NotFound behavior failed with router chaining.")
t.FailNow()
}
})
}
func TestGroup_shortcutsAndHandle(t *testing.T) {
r := New()
g := r.Group("/v1")
shortcuts := []func(path string, handler fasthttp.RequestHandler){
g.GET,
g.HEAD,
g.POST,
g.PUT,
g.PATCH,
g.DELETE,
g.CONNECT,
g.OPTIONS,
g.TRACE,
g.ANY,
}
for _, fn := range shortcuts {
fn("/bar", func(_ *fasthttp.RequestCtx) {})
if err := catchPanic(func() { fn("buzz", func(_ *fasthttp.RequestCtx) {}) }); err == nil {
t.Error("an error was expected when a path does not begin with slash")
}
if err := catchPanic(func() { fn("", func(_ *fasthttp.RequestCtx) {}) }); err == nil {
t.Error("an error was expected with an empty path")
}
}
methods := httpMethods[:len(httpMethods)-1] // Avoid customs methods
for _, method := range methods {
h, _ := r.Lookup(method, "/v1/bar", nil)
if h == nil {
t.Errorf("Bad shorcurt")
}
}
g2 := g.Group("/foo")
for _, method := range httpMethods {
g2.Handle(method, "/bar", func(_ *fasthttp.RequestCtx) {})
if err := catchPanic(func() { g2.Handle(method, "buzz", func(_ *fasthttp.RequestCtx) {}) }); err == nil {
t.Error("an error was expected when a path does not begin with slash")
}
if err := catchPanic(func() { g2.Handle(method, "", func(_ *fasthttp.RequestCtx) {}) }); err == nil {
t.Error("an error was expected with an empty path")
}
h, _ := r.Lookup(method, "/v1/foo/bar", nil)
if h == nil {
t.Errorf("Bad shorcurt")
}
}
}
|