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 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
|
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Snap.Util.FileServe.Tests
( tests ) where
------------------------------------------------------------------------------
import Blaze.ByteString.Builder
import Control.Monad
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as S
import qualified Data.ByteString.Lazy.Char8 as L
import Data.IORef
import qualified Data.Map as Map
import Data.Maybe
import Data.Monoid
import Prelude hiding (take)
import Test.Framework
import Test.Framework.Providers.HUnit
import Test.HUnit hiding (Test, path)
------------------------------------------------------------------------------
import Snap.Internal.Http.Types
import Snap.Internal.Types
import Snap.Util.FileServe
import Snap.Iteratee
import qualified Snap.Types.Headers as H
------------------------------------------------------------------------------
------------------------------------------------------------------------------
tests :: [Test]
tests = [ testFooBin
, testFooTxt
, testFooHtml
, testFooBinBinBin
, test404s
, testFsSingle
, testFsCfgA
, testFsCfgB
, testFsCfgC
, testFsCfgD
, testFsCfgFancy
, testRangeOK
, testRangeBad
, testMultiRange
, testIfRange
]
------------------------------------------------------------------------------
expect404 :: IO Response -> IO ()
expect404 m = do
r <- m
assertBool "expected 404" (rspStatus r == 404)
------------------------------------------------------------------------------
expect302 :: ByteString -> IO Response -> IO ()
expect302 p m = do
r <- m
assertBool "expected 302" (rspStatus r == 302)
assertEqual "redir location"
(Just p)
(getHeader "location" r)
------------------------------------------------------------------------------
getBody :: Response -> IO L.ByteString
getBody r = do
let benum = rspBodyToEnum $ rspBody r
liftM (toLazyByteString . mconcat) (runIteratee consume >>= run_ . benum)
------------------------------------------------------------------------------
runIt :: Snap a -> Request -> Iteratee ByteString IO (Request, Response)
runIt m rq = runSnap m d d rq
where
d = const $ return ()
------------------------------------------------------------------------------
go :: Snap a -> ByteString -> IO Response
go m s = do
rq <- mkRequest s
liftM snd (run_ $ runIt m rq)
------------------------------------------------------------------------------
goIfModifiedSince :: Snap a -> ByteString -> ByteString -> IO Response
goIfModifiedSince m s lm = do
rq <- mkRequest s
let r = setHeader "if-modified-since" lm rq
liftM snd (run_ $ runIt m r)
------------------------------------------------------------------------------
goIfRange :: Snap a -> ByteString -> (Int,Int) -> ByteString -> IO Response
goIfRange m s (start,end) lm = do
rq <- mkRequest s
let r = setHeader "if-range" lm $
setHeader "Range"
(S.pack $ "bytes=" ++ show start ++ "-" ++ show end)
rq
liftM snd (run_ $ runIt m r)
------------------------------------------------------------------------------
goRange :: Snap a -> ByteString -> (Int,Int) -> IO Response
goRange m s (start,end) = do
rq' <- mkRequest s
let rq = setHeader "Range"
(S.pack $ "bytes=" ++ show start ++ "-" ++ show end)
rq'
liftM snd (run_ $ runIt m rq)
------------------------------------------------------------------------------
goMultiRange :: Snap a -> ByteString -> (Int,Int) -> (Int,Int) -> IO Response
goMultiRange m s (start,end) (start2,end2) = do
rq' <- mkRequest s
let rq = setHeader "Range"
(S.pack $ "bytes=" ++ show start ++ "-" ++ show end
++ "," ++ show start2 ++ "-" ++ show end2)
rq'
liftM snd (run_ $ runIt m rq)
------------------------------------------------------------------------------
goRangePrefix :: Snap a -> ByteString -> Int -> IO Response
goRangePrefix m s start = do
rq' <- mkRequest s
let rq = setHeader "Range"
(S.pack $ "bytes=" ++ show start ++ "-")
rq'
liftM snd (run_ $ runIt m rq)
------------------------------------------------------------------------------
goRangeSuffix :: Snap a -> ByteString -> Int -> IO Response
goRangeSuffix m s end = do
rq' <- mkRequest s
let rq = setHeader "Range"
(S.pack $ "bytes=-" ++ show end)
rq'
liftM snd (run_ $ runIt m rq)
------------------------------------------------------------------------------
mkRequest :: ByteString -> IO Request
mkRequest uri = do
enum <- newIORef $ SomeEnumerator returnI
return $ Request "foo" 80 "foo" 999 "foo" 1000 "foo" False H.empty
enum Nothing GET (1,1) [] pathPart "/"
(S.concat ["/",uri]) queryPart
Map.empty Map.empty Map.empty
where
(pathPart, queryPart) = breakQuery uri
breakQuery s = (a, S.drop 1 b)
where
(a,b) = S.break (=='?') s
------------------------------------------------------------------------------
fs :: Snap ()
fs = do
x <- serveDirectory "data/fileServe"
return $! x `seq` ()
------------------------------------------------------------------------------
fsSingle :: Snap ()
fsSingle = do
x <- serveFile "data/fileServe/foo.html"
return $! x `seq` ()
------------------------------------------------------------------------------
fsCfg :: DirectoryConfig Snap -> Snap ()
fsCfg cfg = do
x <- serveDirectoryWith cfg "data/fileServe"
return $! x `seq` ()
------------------------------------------------------------------------------
testFooBin :: Test
testFooBin = testCase "fileServe/foo.bin" $ do
r1 <- go fs "foo.bin"
checkProps "fileServe/foo.bin/default" r1
let !lm = fromJust $ getHeader "last-modified" r1
go fs "foo.bin?blah=blah" >>= checkProps "fileServe/foo.bin/query-string"
-- check last modified stuff
r2 <- goIfModifiedSince fs "foo.bin" lm
assertEqual "foo.bin 304" 304 $ rspStatus r2
r3 <- goIfModifiedSince fs "foo.bin" "Wed, 15 Nov 1995 04:58:08 GMT"
checkProps "fileServe/foo.bin/ifModifiedSince" r3
where
checkProps name r = do
b <- getBody r
assertEqual (name ++ "/contents") "FOO\n" b
assertEqual (name ++ "/content-type")
(Just "application/octet-stream")
(getHeader "content-type" r)
assertEqual (name ++ "/size") (Just 4) (rspContentLength r)
assertBool (name ++ "/last-modified")
(isJust $ getHeader "last-modified" r)
assertEqual (name ++ "/accept-ranges")
(Just "bytes")
(getHeader "accept-ranges" r)
------------------------------------------------------------------------------
testFooTxt :: Test
testFooTxt = testCase "fileServe/foo.txt" $ do
go fs "foo.txt" >>= checkProps "fileServe/foo.txt/default"
go fs "foo.txt?blah=blah" >>= checkProps "fileServe/foo.txt/query"
where
checkProps name r = do
b <- getBody r
assertEqual (name ++ "/contents") "FOO\n" b
assertEqual (name ++ "/content-type")
(Just "text/plain")
(getHeader "content-type" r)
assertEqual (name ++ "/size") (Just 4) (rspContentLength r)
assertBool (name ++ "/last-modified")
(isJust $ getHeader "last-modified" r)
assertEqual (name ++ "/accept-ranges")
(Just "bytes")
(getHeader "accept-ranges" r)
------------------------------------------------------------------------------
testFooHtml :: Test
testFooHtml = testCase "fileServe/foo.html" $ do
go fs "foo.html" >>= checkProps "fileServe/foo.html/default"
go fs "foo.html?bar=bar" >>= checkProps "fileServe/foo.html/query"
where
checkProps name r = do
b <- getBody r
assertEqual (name ++ "/contents") "FOO\n" b
assertEqual (name ++ "/content-type")
(Just "text/html")
(getHeader "content-type" r)
assertEqual (name ++ "/size") (Just 4) (rspContentLength r)
assertBool (name ++ "/last-modified")
(isJust $ getHeader "last-modified" r)
assertEqual (name ++ "/accept-ranges")
(Just "bytes")
(getHeader "accept-ranges" r)
------------------------------------------------------------------------------
testFooBinBinBin :: Test
testFooBinBinBin = testCase "fileServe/foo.bin.bin.bin" $ do
go fs "foo.bin.bin.bin" >>= checkProps "fileServe/foo.bin.bin.bin"
where
checkProps name r = do
b <- getBody r
assertEqual (name ++ "/contents") "FOO\n" b
assertEqual (name ++ "/content-type")
(Just "application/octet-stream")
(getHeader "content-type" r)
assertEqual (name ++ "/size") (Just 4) (rspContentLength r)
assertBool (name ++ "/last-modified")
(isJust $ getHeader "last-modified" r)
assertEqual (name ++ "/accept-ranges")
(Just "bytes")
(getHeader "accept-ranges" r)
------------------------------------------------------------------------------
test404s :: Test
test404s = testCase "fileServe/404s" $ do
expect404 $ go fs "jfldksjflksd"
expect404 $ go fs "dummy/../foo.txt"
expect404 $ go fs "/etc/password"
coverMimeMap
------------------------------------------------------------------------------
printName :: FilePath -> Snap ()
printName c = writeBS $ snd $ S.breakEnd (=='/') $ S.pack c
------------------------------------------------------------------------------
cfgA, cfgB, cfgC, cfgD :: DirectoryConfig Snap
cfgA = DirectoryConfig {
indexFiles = []
, indexGenerator = const pass
, dynamicHandlers = Map.empty
, mimeTypes = defaultMimeTypes
, preServeHook = const $ return ()
}
cfgB = DirectoryConfig {
indexFiles = ["index.txt", "altindex.html"]
, indexGenerator = const pass
, dynamicHandlers = Map.empty
, mimeTypes = defaultMimeTypes
, preServeHook = const $ return ()
}
cfgC = DirectoryConfig {
indexFiles = ["index.txt", "altindex.html"]
, indexGenerator = printName
, dynamicHandlers = Map.empty
, mimeTypes = defaultMimeTypes
, preServeHook = const $ return ()
}
cfgD = DirectoryConfig {
indexFiles = []
, indexGenerator = const pass
, dynamicHandlers = Map.fromList [ (".txt", printName) ]
, mimeTypes = defaultMimeTypes
, preServeHook = const $ return ()
}
------------------------------------------------------------------------------
testFsCfgA :: Test
testFsCfgA = testCase "fileServe/cfgA" $ do
let gooo = go (fsCfg cfgA)
-- Named file in the root directory
gooo "foo.bin" >>= checkProps "cfgA1/1" "application/octet-stream"
gooo "foo.bin?blah=blah" >>=
checkProps "cfgA1/2" "application/octet-stream"
-- Missing file in the root directory
expect404 $ gooo "bar.bin"
-- Named file in a subdirectory
gooo "mydir2/foo.txt" >>= checkProps "cfgA1/subdir/1" "text/plain"
gooo "mydir2/foo.txt?z=z" >>= checkProps "cfgA1/subdir/2" "text/plain"
-- Missing file in a subdirectory
expect404 $ gooo "mydir2/bar.txt"
-- Request for directory with no trailing slash
expect302 "/mydir1/" $ gooo "mydir1"
-- Request for directory with no trailing slash, with query param
expect302 "/mydir1/?z=z" $ gooo "mydir1?z=z"
-- Request for directory with trailing slash, no index
expect404 $ gooo "mydir1/"
expect404 $ gooo "mydir2/"
-- Request file with trailing slash
expect404 $ gooo "foo.html/"
expect404 $ gooo "mydir2/foo.txt/"
where
checkProps name ct r = do
b <- getBody r
assertEqual (name ++ "/contents") "FOO\n" b
assertEqual (name ++ "/content-type")
(Just ct)
(getHeader "content-type" r)
assertEqual (name ++ "/size") (Just 4) (rspContentLength r)
assertBool (name ++ "/last-modified")
(isJust $ getHeader "last-modified" r)
assertEqual (name ++ "/accept-ranges")
(Just "bytes")
(getHeader "accept-ranges" r)
------------------------------------------------------------------------------
testFsCfgB :: Test
testFsCfgB = testCase "fileServe/cfgB" $ do
let gooo = go (fsCfg cfgB)
-- Request for root directory with index
rB1 <- gooo "mydir1/"
bB1 <- getBody rB1
assertEqual "B1" "INDEX\n" bB1
assertEqual "B1 content-type"
(Just "text/plain")
(getHeader "content-type" rB1)
-- Request for root directory with index, query
rB2 <- gooo "mydir1/?z=z"
bB2 <- getBody rB2
assertEqual "B2" "INDEX\n" bB2
assertEqual "B2 content-type"
(Just "text/plain")
(getHeader "content-type" rB2)
-- Request for root directory with alternate index
rB3 <- gooo "mydir3/"
bB3 <- getBody rB3
assertEqual "B3" "ALTINDEX\n" bB3
assertEqual "B3 content-type"
(Just "text/html")
(getHeader "content-type" rB3)
-- Request for root directory with no index
expect404 $ gooo "mydir2/"
------------------------------------------------------------------------------
testFsCfgC :: Test
testFsCfgC = testCase "fileServe/cfgC" $ do
let gooo = go (fsCfg cfgC)
-- Request for root directory with index
rC1 <- gooo "mydir1/"
bC1 <- getBody rC1
assertEqual "C1" "INDEX\n" bC1
assertEqual "C1 content-type"
(Just "text/plain")
(getHeader "content-type" rC1)
-- Request for root directory with index, query
rC2 <- gooo "mydir1/?z=z"
bC2 <- getBody rC2
assertEqual "C2" "INDEX\n" bC2
assertEqual "C2 content-type"
(Just "text/plain")
(getHeader "content-type" rC2)
-- Request for root directory with generated index
rC3 <- gooo "mydir2/"
bC3 <- getBody rC3
assertEqual "C3" "mydir2" bC3
------------------------------------------------------------------------------
testFsCfgD :: Test
testFsCfgD = testCase "fileServe/cfgD" $ do
-- Request for file with dynamic handler
rD1 <- go (fsCfg cfgD) "mydir2/foo.txt"
bD1 <- getBody rD1
assertEqual "D1" "foo.txt" bD1
------------------------------------------------------------------------------
testFsCfgFancy :: Test
testFsCfgFancy = testCase "fileServe/cfgFancy" $ do
-- Request for directory with autogen index
rE1 <- go (fsCfg fancyDirectoryConfig) "mydir2/"
bE1 <- S.concat `fmap` L.toChunks `fmap` getBody rE1
assertBool "autogen-sub-index" $
"Directory Listing: /mydir2/" `S.isInfixOf` bE1
assertBool "autogen-sub-parent" $
"<a href='../'" `S.isInfixOf` bE1
assertBool "autogen-sub-file" $
"<a href='foo.txt'" `S.isInfixOf` bE1
-- Request for directory with autogen index
rE2 <- go (fsCfg fancyDirectoryConfig) "mydir2/?z=z"
bE2 <- S.concat `fmap` L.toChunks `fmap` getBody rE2
assertBool "autogen-sub-index" $
"Directory Listing: /mydir2/" `S.isInfixOf` bE2
assertBool "autogen-sub-parent" $
"<a href='../'" `S.isInfixOf` bE2
assertBool "autogen-sub-file" $
"<a href='foo.txt'" `S.isInfixOf` bE2
------------------------------------------------------------------------------
testFsSingle :: Test
testFsSingle = testCase "fileServe/Single" $ do
r1 <- go fsSingle "foo.html"
b1 <- getBody r1
assertEqual "foo.html" "FOO\n" b1
assertEqual "foo.html content-type"
(Just "text/html")
(getHeader "content-type" r1)
assertEqual "foo.html size" (Just 4) (rspContentLength r1)
------------------------------------------------------------------------------
testRangeOK :: Test
testRangeOK = testCase "fileServe/range/ok" $ do
r1 <- goRange fsSingle "foo.html" (1,2)
assertEqual "foo.html 206" 206 $ rspStatus r1
b1 <- getBody r1
assertEqual "foo.html partial" "OO" b1
assertEqual "foo.html partial size" (Just 2) (rspContentLength r1)
assertEqual "foo.html content-range"
(Just "bytes 1-2/4")
(getHeader "Content-Range" r1)
r2 <- goRangeSuffix fsSingle "foo.html" 3
assertEqual "foo.html 206" 206 $ rspStatus r2
b2 <- getBody r2
assertEqual "foo.html partial suffix" "OO\n" b2
r3 <- goRangePrefix fsSingle "foo.html" 2
assertEqual "foo.html 206" 206 $ rspStatus r3
b3 <- getBody r3
assertEqual "foo.html partial prefix" "O\n" b3
------------------------------------------------------------------------------
testMultiRange :: Test
testMultiRange = testCase "fileServe/range/multi" $ do
r1 <- goMultiRange fsSingle "foo.html" (1,2) (3,3)
-- we don't support multiple ranges so it's ok for us to return 200 here;
-- test this behaviour
assertEqual "foo.html 200" 200 $ rspStatus r1
b1 <- getBody r1
assertEqual "foo.html" "FOO\n" b1
------------------------------------------------------------------------------
testRangeBad :: Test
testRangeBad = testCase "fileServe/range/bad" $ do
r1 <- goRange fsSingle "foo.html" (1,17)
assertEqual "bad range" 416 (rspStatus r1)
assertEqual "bad range content-range"
(Just "bytes */4")
(getHeader "Content-Range" r1)
assertEqual "bad range content-length" (Just 0) (rspContentLength r1)
b1 <- getBody r1
assertEqual "bad range empty body" "" b1
r2 <- goRangeSuffix fsSingle "foo.html" 4893
assertEqual "bad suffix range" 416 $ rspStatus r2
------------------------------------------------------------------------------
coverMimeMap :: (Monad m) => m ()
coverMimeMap = Prelude.mapM_ f $ Map.toList defaultMimeTypes
where
f (!k,!v) = return $ case k `seq` v `seq` () of () -> ()
------------------------------------------------------------------------------
testIfRange :: Test
testIfRange = testCase "fileServe/range/if-range" $ do
r <- goIfRange fs "foo.bin" (1,2) "Wed, 15 Nov 1995 04:58:08 GMT"
assertEqual "foo.bin 200" 200 $ rspStatus r
b <- getBody r
assertEqual "foo.bin" "FOO\n" b
r2 <- goIfRange fs "foo.bin" (1,2) "Tue, 01 Oct 2030 04:58:08 GMT"
assertEqual "foo.bin 206" 206 $ rspStatus r2
b2 <- getBody r2
assertEqual "foo.bin partial" "OO" b2
r3 <- goIfRange fs "foo.bin" (1,24324) "Tue, 01 Oct 2030 04:58:08 GMT"
assertEqual "foo.bin 200" 200 $ rspStatus r3
b3 <- getBody r3
assertEqual "foo.bin" "FOO\n" b3
|