|
| 1 | +package xmux |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/rs/xhandler" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "golang.org/x/net/context" |
| 10 | +) |
| 11 | + |
| 12 | +func TestRouteGroupOfARouteGroup(t *testing.T) { |
| 13 | + var get bool |
| 14 | + mux := New() |
| 15 | + foo := mux.NewGroup("/foo") // creates /foo group |
| 16 | + bar := foo.NewGroup("/bar") |
| 17 | + |
| 18 | + bar.GET("/GET", xhandler.HandlerFuncC(func(_ context.Context, _ http.ResponseWriter, _ *http.Request) { |
| 19 | + get = true |
| 20 | + })) |
| 21 | + |
| 22 | + w := new(mockResponseWriter) |
| 23 | + r, _ := http.NewRequest("GET", "/foo/bar/GET", nil) |
| 24 | + mux.ServeHTTPC(context.Background(), w, r) |
| 25 | + assert.True(t, get, "routing GET /foo/bar/GET failed") |
| 26 | +} |
| 27 | + |
| 28 | +func TestRouteNewGroupStripTrailingSlash(t *testing.T) { |
| 29 | + var get bool |
| 30 | + mux := New() |
| 31 | + foo := mux.NewGroup("/foo/") |
| 32 | + |
| 33 | + foo.GET("/GET", xhandler.HandlerFuncC(func(_ context.Context, _ http.ResponseWriter, _ *http.Request) { |
| 34 | + get = true |
| 35 | + })) |
| 36 | + |
| 37 | + w := new(mockResponseWriter) |
| 38 | + r, _ := http.NewRequest("GET", "/foo/GET", nil) |
| 39 | + mux.ServeHTTPC(context.Background(), w, r) |
| 40 | + assert.True(t, get, "routing GET /foo/GET failed") |
| 41 | +} |
| 42 | + |
| 43 | +func TestRouteNewGroupError(t *testing.T) { |
| 44 | + mux := New() |
| 45 | + assert.Panics(t, func() { |
| 46 | + mux.NewGroup("foo") |
| 47 | + }) |
| 48 | + assert.Panics(t, func() { |
| 49 | + mux.NewGroup("/foo").NewGroup("bar") |
| 50 | + }) |
| 51 | +} |
| 52 | + |
| 53 | +func TestRouteGroupAPI(t *testing.T) { |
| 54 | + var get, head, options, post, put, patch, delete bool |
| 55 | + |
| 56 | + mux := New() |
| 57 | + group := mux.NewGroup("/foo") // creates /foo group |
| 58 | + |
| 59 | + group.GET("/GET", xhandler.HandlerFuncC(func(_ context.Context, _ http.ResponseWriter, _ *http.Request) { |
| 60 | + get = true |
| 61 | + })) |
| 62 | + group.HEAD("/GET", xhandler.HandlerFuncC(func(_ context.Context, _ http.ResponseWriter, _ *http.Request) { |
| 63 | + head = true |
| 64 | + })) |
| 65 | + group.OPTIONS("/GET", xhandler.HandlerFuncC(func(_ context.Context, _ http.ResponseWriter, _ *http.Request) { |
| 66 | + options = true |
| 67 | + })) |
| 68 | + group.POST("/POST", xhandler.HandlerFuncC(func(_ context.Context, _ http.ResponseWriter, _ *http.Request) { |
| 69 | + post = true |
| 70 | + })) |
| 71 | + group.PUT("/PUT", xhandler.HandlerFuncC(func(_ context.Context, _ http.ResponseWriter, _ *http.Request) { |
| 72 | + put = true |
| 73 | + })) |
| 74 | + group.PATCH("/PATCH", xhandler.HandlerFuncC(func(_ context.Context, _ http.ResponseWriter, _ *http.Request) { |
| 75 | + patch = true |
| 76 | + })) |
| 77 | + group.DELETE("/DELETE", xhandler.HandlerFuncC(func(_ context.Context, _ http.ResponseWriter, _ *http.Request) { |
| 78 | + delete = true |
| 79 | + })) |
| 80 | + |
| 81 | + w := new(mockResponseWriter) |
| 82 | + |
| 83 | + r, _ := http.NewRequest("GET", "/foo/GET", nil) |
| 84 | + mux.ServeHTTPC(context.Background(), w, r) |
| 85 | + assert.True(t, get, "routing /foo/GET failed") |
| 86 | + |
| 87 | + r, _ = http.NewRequest("HEAD", "/foo/GET", nil) |
| 88 | + mux.ServeHTTPC(context.Background(), w, r) |
| 89 | + assert.True(t, head, "routing /foo/GET failed") |
| 90 | + |
| 91 | + r, _ = http.NewRequest("OPTIONS", "/foo/GET", nil) |
| 92 | + mux.ServeHTTPC(context.Background(), w, r) |
| 93 | + assert.True(t, options, "routing /foo/GET failed") |
| 94 | + |
| 95 | + r, _ = http.NewRequest("POST", "/foo/POST", nil) |
| 96 | + mux.ServeHTTPC(context.Background(), w, r) |
| 97 | + assert.True(t, post, "routing /foo/POST failed") |
| 98 | + |
| 99 | + r, _ = http.NewRequest("PUT", "/foo/PUT", nil) |
| 100 | + mux.ServeHTTPC(context.Background(), w, r) |
| 101 | + assert.True(t, put, "routing /foo/PUT failed") |
| 102 | + |
| 103 | + r, _ = http.NewRequest("PATCH", "/foo/PATCH", nil) |
| 104 | + mux.ServeHTTPC(context.Background(), w, r) |
| 105 | + assert.True(t, patch, "routing /foo/PATCH failed") |
| 106 | + |
| 107 | + r, _ = http.NewRequest("DELETE", "/foo/DELETE", nil) |
| 108 | + mux.ServeHTTPC(context.Background(), w, r) |
| 109 | + assert.True(t, delete, "routing /foo/DELETE failed") |
| 110 | +} |
0 commit comments