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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
|
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Snap.Internal.Routing.Tests
( tests ) where
------------------------------------------------------------------------------
import Control.Applicative ((<|>))
import Control.Monad
import Data.ByteString (ByteString)
import qualified Data.ByteString as S
import qualified Data.Map as Map
import Data.Maybe
import Test.Framework
import Test.Framework.Providers.HUnit
import Test.HUnit hiding (Test, path)
------------------------------------------------------------------------------
import Snap.Internal.Http.Types
import Snap.Internal.Routing
import Snap.Internal.Types
import Snap.Test
import Snap.Test.Common
------------------------------------------------------------------------------
tests :: [Test]
tests = [ testRouting1
, testRouting2
, testRouting3
, testRouting4
, testRouting5
, testRouting6
, testRouting7
, testRouting8
, testRouting9
, testRouting10
, testRouting11
, testRouting12
, testRouting13
, testRouting14
, testRouting15
, testRouting16
, testRouting17
, testRouting18
, testRouting19
, testRouting20
, testRouting21
, testRouting22
, testRouting23
, testRouting24
, testRouting25
, testRouting26
, testRouting27
, testRouting28
, testRouteLocal
, testRouteUrlDecode
, testRouteUrlEncodedPath
, testRouteEmptyCapture
]
------------------------------------------------------------------------------
go :: Snap a -> ByteString -> IO a
go m s0 = evalHandler (get s Map.empty) m
where
s = if S.isPrefixOf "/" s0
then s0
else S.append "/" s0
------------------------------------------------------------------------------
routes :: Snap ByteString
routes = route [ ("foo" , topFoo )
, ("foo/bar" , fooBar )
, ("foo/bar/baz" , getRqPathInfo )
, ("foo/:id" , fooCapture )
, ("bar/:id" , fooCapture )
, ("herp/:derp/" , getRqPathInfo )
, ("nerp/:derp/" , getRqContextPath)
, ("a b c d" , return "OK" )
, ("bar/quux" , barQuux )
, ("bar" , bar )
, ("z/:a/:b/:c/d" , zabc ) ]
------------------------------------------------------------------------------
routesLocal :: Snap ByteString
routesLocal = routeLocal [ ("foo/bar/baz" , getRqPathInfo )
, ("bar" , pass ) ]
------------------------------------------------------------------------------
routes2 :: Snap ByteString
routes2 = route [ ("" , topTop )
, ("foo" , topFoo ) ]
------------------------------------------------------------------------------
routes3 :: Snap ByteString
routes3 = route [ (":foo" , topCapture )
, ("" , topTop ) ]
------------------------------------------------------------------------------
routes4 :: Snap ByteString
routes4 = route [ (":foo" , pass )
, (":foo" , topCapture )
, (":qqq/:id" , fooCapture )
, (":id2/baz" , fooCapture2 ) ]
------------------------------------------------------------------------------
routes5 :: Snap ByteString
routes5 = route [ ("" , pass )
, ("" , topTop ) ]
------------------------------------------------------------------------------
routes6 :: Snap ByteString
routes6 = route [ (":a/:a" , dblA ) ]
------------------------------------------------------------------------------
routes7 :: Snap ByteString
routes7 = route [ ("foo/:id" , fooCapture )
, ("foo/:id/:id2" , fooCapture2)
, ("fooo/:id/:id2" , fooCapture2)
, ("foooo/bar/baz" , bar )
, ("" , topTop ) ]
------------------------------------------------------------------------------
routesEmptyCapture :: Snap ByteString
routesEmptyCapture = route [ ("foo/:id", fooCapture) ]
------------------------------------------------------------------------------
topTop, topFoo, fooBar, fooCapture, getRqPathInfo, bar,
getRqContextPath, barQuux, dblA, zabc, topCapture,
fooCapture2 :: Snap ByteString
dblA = do
ma <- getParam "a"
unless (ma == Just "a a") pass
return "ok"
zabc = do
ma <- getParam "a"
mb <- getParam "b"
mc <- getParam "c"
unless ( ma == Just "a"
&& mb == Just "b"
&& mc == Just "c" ) pass
return "ok"
topCapture = do
mp <- getParam "foo"
maybe pass return mp
topTop = return "topTop"
topFoo = return "topFoo"
fooBar = return "fooBar"
fooCapture = liftM (head . fromJust . rqParam "id") getRequest
fooCapture2 = liftM (head . fromJust . rqParam "id2") getRequest
getRqPathInfo = liftM rqPathInfo getRequest
getRqContextPath = liftM rqContextPath getRequest
barQuux = return "barQuux"
bar = return "bar"
-----------
-- Tests --
-----------
------------------------------------------------------------------------------
-- TODO more useful test names
testRouting1 :: Test
testRouting1 = testCase "route/1" $ do
r1 <- go routes "foo"
assertEqual "/foo" "topFoo" r1
------------------------------------------------------------------------------
testRouting2 :: Test
testRouting2 = testCase "route/2" $ do
r2 <- go routes "foo/baz"
assertEqual "/foo/baz" "baz" r2
------------------------------------------------------------------------------
testRouting3 :: Test
testRouting3 = testCase "route/3" $ do
expectExceptionH $ go routes "/xsaxsaxsax"
------------------------------------------------------------------------------
testRouting4 :: Test
testRouting4 = testCase "route/4" $ do
r3 <- go routes "foo/bar"
assertEqual "/foo/bar" "fooBar" r3
------------------------------------------------------------------------------
testRouting5 :: Test
testRouting5 = testCase "route/5" $ do
r4 <- go routes "foo/bar/baz/quux"
assertEqual "/foo/bar/baz/quux" "quux" r4
------------------------------------------------------------------------------
testRouting6 :: Test
testRouting6 = testCase "route/6" $ do
r5 <- go routes "foo/bar/sproing"
assertEqual "/foo/bar/sproing" "fooBar" r5
------------------------------------------------------------------------------
testRouting7 :: Test
testRouting7 = testCase "route/7" $ do
r <- go routes "bar"
assertEqual "/bar" "bar" r
------------------------------------------------------------------------------
testRouting8 :: Test
testRouting8 = testCase "route/8" $ do
r2 <- go routes "bar/quux"
assertEqual "/bar/quux" "barQuux" r2
------------------------------------------------------------------------------
testRouting9 :: Test
testRouting9 = testCase "route/9" $ do
r3 <- go routes "bar/whatever"
assertEqual "/bar/whatever" "whatever" r3
------------------------------------------------------------------------------
testRouting10 :: Test
testRouting10 = testCase "route/10" $ do
r4 <- go routes "bar/quux/whatever"
assertEqual "/bar/quux/whatever" "barQuux" r4
------------------------------------------------------------------------------
testRouting11 :: Test
testRouting11 = testCase "route/11" $ do
r1 <- go routes2 ""
assertEqual "/" "topTop" r1
------------------------------------------------------------------------------
testRouting12 :: Test
testRouting12 = testCase "route/12" $ do
r1 <- go routes2 "foo"
assertEqual "/foo" "topFoo" r1
------------------------------------------------------------------------------
testRouting13 :: Test
testRouting13 = testCase "route/13" $ do
r1 <- go routes3 "zzzz"
assertEqual "/zzzz" "zzzz" r1
------------------------------------------------------------------------------
testRouting14 :: Test
testRouting14 = testCase "route/14" $ do
r1 <- go routes3 ""
assertEqual "/" "topTop" r1
------------------------------------------------------------------------------
testRouting15 :: Test
testRouting15 = testCase "route/15" $ do
r1 <- go routes4 "zzzz"
assertEqual "/zzzz" "zzzz" r1
------------------------------------------------------------------------------
testRouting16 :: Test
testRouting16 = testCase "route/16" $ do
r1 <- go routes5 ""
assertEqual "/" "topTop" r1
------------------------------------------------------------------------------
testRouting17 :: Test
testRouting17 = testCase "route/17" $ do
r1 <- go routes "z/a/b/c/d"
assertEqual "/z/a/b/c/d" "ok" r1
------------------------------------------------------------------------------
testRouting18 :: Test
testRouting18 = testCase "route/18" $ do
r1 <- go routes6 "a/a"
assertEqual "/a/a" "ok" r1
------------------------------------------------------------------------------
testRouting19 :: Test
testRouting19 = testCase "route/19" $ do
r1 <- go routes7 "foo"
assertEqual "/foo" "topTop" r1
------------------------------------------------------------------------------
testRouting20 :: Test
testRouting20 = testCase "route/20" $ do
r1 <- go routes7 "foo/baz"
assertEqual "/foo/baz" "baz" r1
------------------------------------------------------------------------------
testRouting21 :: Test
testRouting21 = testCase "route/21" $ do
r1 <- go routes7 "foo/baz/quux"
assertEqual "/foo/baz/quux" "quux" r1
------------------------------------------------------------------------------
testRouting22 :: Test
testRouting22 = testCase "route/22" $ do
r1 <- go routes7 "fooo/baz"
assertEqual "/fooo/baz" "topTop" r1
------------------------------------------------------------------------------
testRouting23 :: Test
testRouting23 = testCase "route/23" $ do
r1 <- go routes7 "fooo/baz/quux"
assertEqual "/fooo/baz/quux" "quux" r1
------------------------------------------------------------------------------
testRouting24 :: Test
testRouting24 = testCase "route/24" $ do
r1 <- go routes7 "foooo/bar/bax"
assertEqual "/foooo/bar/bax" "topTop" r1
------------------------------------------------------------------------------
testRouting25 :: Test
testRouting25 = testCase "route/25" $ do
r1 <- go routes7 "foooo/bar/baz"
assertEqual "/foooo/bar/baz" "bar" r1
------------------------------------------------------------------------------
testRouting26 :: Test
testRouting26 = testCase "route/26" $ do
r1 <- go routes4 "foo/bar"
assertEqual "capture union" "bar" r1
------------------------------------------------------------------------------
testRouting27 :: Test
testRouting27 = testCase "route/27" $ do
r1 <- go routes4 "foo"
assertEqual "capture union" "foo" r1
------------------------------------------------------------------------------
testRouting28 :: Test
testRouting28 = testCase "route/28" $ do
r1 <- go routes4 "quux/baz"
assertEqual "capture union" "quux" r1
------------------------------------------------------------------------------
testRouteUrlDecode :: Test
testRouteUrlDecode = testCase "route/urlDecode" $ do
r1 <- go routes "herp/%7Bderp%7D/"
assertEqual "rqPathInfo on urldecode" "" r1
r2 <- go routes "foo/%7Bderp%7D/"
assertEqual "urldecoded capture" "{derp}" r2
r3 <- go routes "nerp/%7Bderp%7D/"
assertEqual "rqContextPath on urldecode" "/nerp/%7Bderp%7D/" r3
------------------------------------------------------------------------------
testRouteUrlEncodedPath :: Test
testRouteUrlEncodedPath = testCase "route/urlEncodedPath" $ do
-- make sure path search urlDecodes.
r1 <- go routes "a+b+c+d"
assertEqual "urlEncoded search works" "OK" r1
------------------------------------------------------------------------------
testRouteLocal :: Test
testRouteLocal = testCase "route/routeLocal" $ do
r4 <- go routesLocal "foo/bar/baz/quux"
assertEqual "/foo/bar/baz/quux" "foo/bar/baz/quux" r4
expectExceptionH $ go routesLocal "bar"
------------------------------------------------------------------------------
testRouteEmptyCapture :: Test
testRouteEmptyCapture = testCase "route/emptyCapture" $ do
r <- go m "foo"
assertEqual "empty capture must fail" expected r
r2 <- go m "foo/"
assertEqual "empty capture must fail" expected r2
where
expected = "ZOMG_OK"
m = routesEmptyCapture <|> return expected
|