|
| 1 | +package nchi_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/muir/nchi" |
| 7 | +) |
| 8 | + |
| 9 | +func TestWithRedirectTrailingSlash(t *testing.T) { |
| 10 | + mux := nchi.NewRouter(nchi.WithRedirectTrailingSlash(true)) |
| 11 | + mux.Use("") |
| 12 | + mux.Use(makeDown("a")) |
| 13 | + mux.Get("/ts1", makeDown("b"), bottom) |
| 14 | + |
| 15 | + doTest(t, mux, []testCase{ |
| 16 | + {path: "/ts1", want: "ab"}, |
| 17 | + {path: "/ts1/", want: "<a href=\"/ts1\">Moved Permanently</a>.\n\n"}, |
| 18 | + }) |
| 19 | + |
| 20 | + mux = nchi.NewRouter(nchi.WithRedirectTrailingSlash(false)) |
| 21 | + mux.Use("") |
| 22 | + mux.Use(makeDown("a")) |
| 23 | + mux.Get("/ts2", makeDown("b"), bottom) |
| 24 | + |
| 25 | + doTest(t, mux, []testCase{ |
| 26 | + {path: "/ts2", want: "ab"}, |
| 27 | + {path: "/ts2/", want: "404 page not found\n"}, |
| 28 | + }) |
| 29 | + |
| 30 | + mux = nchi.NewRouter() |
| 31 | + mux.Use("") |
| 32 | + mux.Use(makeDown("a")) |
| 33 | + mux.Get("/ts3", makeDown("b"), bottom) |
| 34 | + |
| 35 | + doTest(t, mux, []testCase{ |
| 36 | + {path: "/ts3", want: "ab"}, |
| 37 | + {path: "/ts3/", want: "<a href=\"/ts3\">Moved Permanently</a>.\n\n"}, |
| 38 | + }) |
| 39 | +} |
| 40 | + |
| 41 | +func TestWithRedirectFixedPath(t *testing.T) { |
| 42 | + mux := nchi.NewRouter(nchi.WithRedirectFixedPath(true)) |
| 43 | + mux.Use("") |
| 44 | + mux.Use(makeDown("a")) |
| 45 | + mux.Get("/ts1", makeDown("b"), bottom) |
| 46 | + |
| 47 | + doTest(t, mux, []testCase{ |
| 48 | + {path: "/ts1", want: "ab"}, |
| 49 | + {path: "/xx/..//ts1/", want: "<a href=\"/ts1\">Moved Permanently</a>.\n\n"}, |
| 50 | + }) |
| 51 | + |
| 52 | + mux = nchi.NewRouter(nchi.WithRedirectFixedPath(false)) |
| 53 | + mux.Use("") |
| 54 | + mux.Use(makeDown("a")) |
| 55 | + mux.Get("/ts2", makeDown("b"), bottom) |
| 56 | + |
| 57 | + doTest(t, mux, []testCase{ |
| 58 | + {path: "/ts2", want: "ab"}, |
| 59 | + {path: "/xx/..//ts2/", want: "404 page not found\n"}, |
| 60 | + }) |
| 61 | + |
| 62 | + mux = nchi.NewRouter() |
| 63 | + mux.Use("") |
| 64 | + mux.Use(makeDown("a")) |
| 65 | + mux.Get("/ts3", makeDown("b"), bottom) |
| 66 | + |
| 67 | + doTest(t, mux, []testCase{ |
| 68 | + {path: "/ts3", want: "ab"}, |
| 69 | + {path: "/xx/..//ts3", want: "<a href=\"/ts3\">Moved Permanently</a>.\n\n"}, |
| 70 | + }) |
| 71 | +} |
| 72 | + |
| 73 | +func TestWithHandleMethodNotAllowed(t *testing.T) { |
| 74 | + mux := nchi.NewRouter(nchi.WithHandleMethodNotAllowed(true)) |
| 75 | + mux.Use("") |
| 76 | + mux.Use(makeDown("a")) |
| 77 | + mux.Post("/mna1", makeDown("b"), bottom) |
| 78 | + |
| 79 | + doTest(t, mux, []testCase{ |
| 80 | + {path: "/mna1", want: "Method Not Allowed\n"}, |
| 81 | + }) |
| 82 | + |
| 83 | + mux = nchi.NewRouter(nchi.WithHandleMethodNotAllowed(false)) |
| 84 | + mux.Use("") |
| 85 | + mux.Use(makeDown("a")) |
| 86 | + mux.Post("/mna2", makeDown("b"), bottom) |
| 87 | + |
| 88 | + doTest(t, mux, []testCase{ |
| 89 | + {path: "/mna2", want: "404 page not found\n"}, |
| 90 | + }) |
| 91 | + |
| 92 | + mux = nchi.NewRouter() |
| 93 | + mux.Use("") |
| 94 | + mux.Use(makeDown("a")) |
| 95 | + mux.Post("/mna3", makeDown("b"), bottom) |
| 96 | + |
| 97 | + doTest(t, mux, []testCase{ |
| 98 | + {path: "/mna3", want: "Method Not Allowed\n"}, |
| 99 | + }) |
| 100 | +} |
| 101 | + |
| 102 | +func TestWithHandleOPTIONS(t *testing.T) { |
| 103 | + mux := nchi.NewRouter(nchi.WithHandleOPTIONS(true)) |
| 104 | + mux.Use("") |
| 105 | + mux.Use(makeDown("a")) |
| 106 | + mux.Get("/ho1", makeDown("b"), bottom) |
| 107 | + mux.Options("/ho1", makeDown("c"), bottom) |
| 108 | + mux.Get("/ho2", makeDown("c"), bottom) |
| 109 | + |
| 110 | + doTestMethod(t, mux, "OPTIONS", []testCase{ |
| 111 | + {path: "/ho1", want: "ac"}, |
| 112 | + {path: "/ho2", want: ""}, |
| 113 | + {path: "/ho3", want: "404 page not found\n"}, |
| 114 | + }) |
| 115 | + |
| 116 | + mux = nchi.NewRouter(nchi.WithHandleOPTIONS(false)) |
| 117 | + mux.Use("") |
| 118 | + mux.Use(makeDown("a")) |
| 119 | + mux.Get("/ho3", makeDown("b"), bottom) |
| 120 | + mux.Options("/ho3", makeDown("c"), bottom) |
| 121 | + mux.Get("/ho4", makeDown("c"), bottom) |
| 122 | + |
| 123 | + doTestMethod(t, mux, "OPTIONS", []testCase{ |
| 124 | + {path: "/ho3", want: "ac"}, |
| 125 | + {path: "/ho4", want: "Method Not Allowed\n"}, |
| 126 | + {path: "/ho5", want: "404 page not found\n"}, |
| 127 | + }) |
| 128 | + |
| 129 | + mux = nchi.NewRouter() |
| 130 | + mux.Use("") |
| 131 | + mux.Use(makeDown("a")) |
| 132 | + mux.Get("/ho6", makeDown("b"), bottom) |
| 133 | + mux.Options("/ho6", makeDown("c"), bottom) |
| 134 | + mux.Get("/ho7", makeDown("c"), bottom) |
| 135 | + |
| 136 | + doTestMethod(t, mux, "OPTIONS", []testCase{ |
| 137 | + {path: "/ho6", want: "ac"}, |
| 138 | + {path: "/ho7", want: ""}, |
| 139 | + {path: "/ho8", want: "404 page not found\n"}, |
| 140 | + }) |
| 141 | +} |
0 commit comments