|
232 | 232 | (is (= 302 (:status unauthed))) |
233 | 233 | (is (= "/login" (get-in unauthed [:headers "Location"])))))) |
234 | 234 |
|
| 235 | +(deftest test-create-handler-with-denormalized-routes |
| 236 | + (testing "Denormalized (nested) routes are served and receive hyper context" |
| 237 | + (let [received-req (atom nil) |
| 238 | + app-state* (atom (state/init-state)) |
| 239 | + routes [["" |
| 240 | + ["/" |
| 241 | + ["" {:name :home |
| 242 | + :get (fn [req] |
| 243 | + (reset! received-req req) |
| 244 | + [:div "Home"])}]] |
| 245 | + ["/about" |
| 246 | + ["" {:name :about |
| 247 | + :get (fn [_] [:div "About"]) |
| 248 | + :title "About Us"}]] |
| 249 | + ["/users/:id" {:name :user-profile |
| 250 | + :get (fn [_] [:div "User"])}]]] |
| 251 | + handler (server/create-handler routes app-state*) |
| 252 | + response (handler {:uri "/" :request-method :get})] |
| 253 | + (is (= 200 (:status response)) |
| 254 | + "Nested home route should be served") |
| 255 | + (is (.contains (:body response) "Home")) |
| 256 | + (is (some? @received-req) |
| 257 | + "Handler should have been called") |
| 258 | + (is (string? (:hyper/session-id @received-req)) |
| 259 | + "Request should carry :hyper/session-id") |
| 260 | + (is (string? (:hyper/tab-id @received-req)) |
| 261 | + "Request should carry :hyper/tab-id") |
| 262 | + (is (= app-state* (:hyper/app-state @received-req)) |
| 263 | + "Request should carry :hyper/app-state"))) |
| 264 | + |
| 265 | + (testing "All sibling routes in a denormalized tree are reachable" |
| 266 | + (let [app-state* (atom (state/init-state)) |
| 267 | + routes [["" |
| 268 | + ["/" |
| 269 | + ["" {:name :home |
| 270 | + :get (fn [_] [:div "Home"])}]] |
| 271 | + ["/about" |
| 272 | + ["" {:name :about |
| 273 | + :get (fn [_] [:div "About"]) |
| 274 | + :title "About Us"}]] |
| 275 | + ["/users/:id" {:name :user-profile |
| 276 | + :get (fn [_] [:div "User"])}]]] |
| 277 | + handler (server/create-handler routes app-state*)] |
| 278 | + (is (= 200 (:status (handler {:uri "/about" :request-method :get})))) |
| 279 | + (is (.contains (:body (handler {:uri "/about" :request-method :get})) "About")) |
| 280 | + (is (= 200 (:status (handler {:uri "/users/42" :request-method :get})))) |
| 281 | + (is (.contains (:body (handler {:uri "/users/42" :request-method :get})) "User")))) |
| 282 | + |
| 283 | + (testing "Denormalized routes are indexed correctly in app-state" |
| 284 | + (let [app-state* (atom (state/init-state)) |
| 285 | + routes [["" |
| 286 | + ["/" |
| 287 | + ["" {:name :home |
| 288 | + :get (fn [_] [:div "Home"])}]] |
| 289 | + ["/about" |
| 290 | + ["" {:name :about |
| 291 | + :get (fn [_] [:div "About"]) |
| 292 | + :title "About Us"}]] |
| 293 | + ["/users/:id" {:name :user-profile |
| 294 | + :get (fn [_] [:div "User"])}]]] |
| 295 | + _handler (server/create-handler routes app-state*) |
| 296 | + route-idx (routes/live-route-index app-state*)] |
| 297 | + (is (contains? route-idx :home)) |
| 298 | + (is (contains? route-idx :about)) |
| 299 | + (is (contains? route-idx :user-profile)) |
| 300 | + (is (= "About Us" (routes/find-route-title route-idx :about)))))) |
| 301 | + |
235 | 302 | (deftest test-create-handler-with-hyper-disabled |
236 | 303 | (testing "render fn can disable endpoint wrapping" |
237 | 304 | (let [app-state* (atom (state/init-state)) |
|
0 commit comments