| 1 |
{-# LANGUAGE BangPatterns #-} |
| 2 |
{-# LANGUAGE CPP #-} |
| 3 |
{-# LANGUAGE OverloadedStrings #-} |
| 4 |
{-# LANGUAGE Rank2Types #-} |
| 5 |
{-# LANGUAGE ScopedTypeVariables #-} |
| 6 |
|
| 7 |
module Snap.Util.GZip.Tests |
| 8 |
( tests ) where |
| 9 |
------------------------------------------------------------------------------ |
| 10 |
import qualified Codec.Compression.GZip as GZip |
| 11 |
import qualified Codec.Compression.Zlib as Zlib |
| 12 |
import Control.Monad (replicateM) |
| 13 |
import Data.Bits ((.&.)) |
| 14 |
import qualified Data.ByteString as B |
| 15 |
import Data.ByteString.Builder (byteString) |
| 16 |
import Data.ByteString.Char8 (ByteString) |
| 17 |
import qualified Data.ByteString.Char8 as S |
| 18 |
import qualified Data.ByteString.Lazy.Char8 as L |
| 19 |
import Data.CaseInsensitive (CI) |
| 20 |
import Data.Word (Word8) |
| 21 |
import Snap.Core (Request, Response, Snap, getHeader, modifyResponse, runSnap, setContentType, setHeader, setResponseBody, writeBS) |
| 22 |
import qualified Snap.Test as Test |
| 23 |
import Snap.Test.Common (coverTypeableInstance, expectException, expectExceptionH, liftQ) |
| 24 |
import Snap.Util.GZip (BadAcceptEncodingException, noCompression, withCompression) |
| 25 |
import qualified System.IO.Streams as Streams |
| 26 |
import System.Random (randomIO) |
| 27 |
import Test.Framework (Test) |
| 28 |
import Test.Framework.Providers.HUnit (testCase) |
| 29 |
import Test.Framework.Providers.QuickCheck2 (testProperty) |
| 30 |
import Test.HUnit (assertEqual) |
| 31 |
import qualified Test.HUnit as H |
| 32 |
import Test.QuickCheck (Arbitrary (arbitrary)) |
| 33 |
import Test.QuickCheck.Monadic (PropertyM, assert, forAllM, monadicIO) |
| 34 |
#if !MIN_VERSION_base(4,8,0) |
| 35 |
import Control.Applicative ((<$>)) |
| 36 |
#endif |
| 37 |
------------------------------------------------------------------------------ |
| 38 |
|
| 39 |
------------------------------------------------------------------------------ |
| 40 |
tests :: [Test] |
| 41 |
tests = [ testIdentity1 |
| 42 |
, testIdentity1_charset |
| 43 |
, testIdentity2 |
| 44 |
, testIdentity3 |
| 45 |
, testIdentity4 |
| 46 |
, testIdentity5 |
| 47 |
, testNoHeaders |
| 48 |
, testNoAcceptEncoding |
| 49 |
, testAcceptEncodingBad |
| 50 |
, testNopWhenContentEncodingSet |
| 51 |
, testCompositionDoesn'tExplode |
| 52 |
, testGzipLotsaChunks |
| 53 |
, testNoCompression |
| 54 |
, testBadHeaders |
| 55 |
, testTrivials |
| 56 |
] |
| 57 |
|
| 58 |
|
| 59 |
------------------------------------------------------------------------------ |
| 60 |
gzipHdrs, xGzipHdrs, xGzipHdrs2, badHdrs, deflateHdrs, xDeflateHdrs |
| 61 |
:: (CI ByteString, ByteString) |
| 62 |
|
| 63 |
gzipHdrs = ("Accept-Encoding", "deflate,froz,gzip,glorble, x-gzip" ) |
| 64 |
xGzipHdrs = ("Accept-Encoding", "x-gzip;q=1.0" ) |
| 65 |
xGzipHdrs2 = ("Accept-Encoding", "x-gzip;q=1" ) |
| 66 |
badHdrs = ("Accept-Encoding", "*&%^&^$%&%&*^\023" ) |
| 67 |
deflateHdrs = ("Accept-Encoding", "deflate" ) |
| 68 |
xDeflateHdrs = ("Accept-Encoding", "x-deflate" ) |
| 69 |
|
| 70 |
|
| 71 |
------------------------------------------------------------------------------ |
| 72 |
mkNoHeaders :: IO Request |
| 73 |
mkNoHeaders = Test.buildRequest $ return () |
| 74 |
|
| 75 |
|
| 76 |
------------------------------------------------------------------------------ |
| 77 |
mkGzipRq :: IO Request |
| 78 |
mkGzipRq = Test.buildRequest $ uncurry Test.setHeader gzipHdrs |
| 79 |
|
| 80 |
|
| 81 |
------------------------------------------------------------------------------ |
| 82 |
mkXGzipRq :: IO Request |
| 83 |
mkXGzipRq = Test.buildRequest $ uncurry Test.setHeader xGzipHdrs |
| 84 |
|
| 85 |
|
| 86 |
------------------------------------------------------------------------------ |
| 87 |
mkXGzip2Rq :: IO Request |
| 88 |
mkXGzip2Rq = Test.buildRequest $ uncurry Test.setHeader xGzipHdrs2 |
| 89 |
|
| 90 |
|
| 91 |
------------------------------------------------------------------------------ |
| 92 |
mkDeflateRq :: IO Request |
| 93 |
mkDeflateRq = Test.buildRequest $ uncurry Test.setHeader deflateHdrs |
| 94 |
|
| 95 |
|
| 96 |
------------------------------------------------------------------------------ |
| 97 |
mkXDeflateRq :: IO Request |
| 98 |
mkXDeflateRq = Test.buildRequest $ uncurry Test.setHeader xDeflateHdrs |
| 99 |
|
| 100 |
|
| 101 |
------------------------------------------------------------------------------ |
| 102 |
mkBadRq :: IO Request |
| 103 |
mkBadRq = Test.buildRequest $ uncurry Test.setHeader badHdrs |
| 104 |
|
| 105 |
|
| 106 |
------------------------------------------------------------------------------ |
| 107 |
seqSnap :: Snap a -> Snap a |
| 108 |
seqSnap m = do |
| 109 |
!x <- m |
| 110 |
return $! x `seq` x |
| 111 |
|
| 112 |
|
| 113 |
------------------------------------------------------------------------------ |
| 114 |
goGeneric :: IO Request -> Snap a -> IO (Request, Response) |
| 115 |
goGeneric mkRq m = do |
| 116 |
rq <- mkRq |
| 117 |
runSnap (seqSnap m) d d rq |
| 118 |
|
| 119 |
where |
| 120 |
d = (const $ return ()) |
| 121 |
|
| 122 |
|
| 123 |
goGZip, goDeflate, goXGZip, goXGZip2 :: Snap a -> IO (Request,Response) |
| 124 |
goNoHeaders, goXDeflate, goBad :: Snap a -> IO (Request,Response) |
| 125 |
|
| 126 |
goGZip = goGeneric mkGzipRq |
| 127 |
goDeflate = goGeneric mkDeflateRq |
| 128 |
goXGZip = goGeneric mkXGzipRq |
| 129 |
goXGZip2 = goGeneric mkXGzip2Rq |
| 130 |
goXDeflate = goGeneric mkXDeflateRq |
| 131 |
goBad = goGeneric mkBadRq |
| 132 |
goNoHeaders = goGeneric mkNoHeaders |
| 133 |
|
| 134 |
|
| 135 |
------------------------------------------------------------------------------ |
| 136 |
noContentType :: L.ByteString -> Snap () |
| 137 |
noContentType body = modifyResponse $ setResponseBody e |
| 138 |
where |
| 139 |
e s = do |
| 140 |
Streams.writeList (map byteString $ L.toChunks body) s |
| 141 |
return s |
| 142 |
|
| 143 |
------------------------------------------------------------------------------ |
| 144 |
withContentType :: ByteString -> L.ByteString -> Snap () |
| 145 |
withContentType ct body = modifyResponse $ |
| 146 |
setResponseBody e . setContentType ct |
| 147 |
where |
| 148 |
e s = do |
| 149 |
Streams.writeList (map byteString $ L.toChunks body) s |
| 150 |
return s |
| 151 |
|
| 152 |
|
| 153 |
------------------------------------------------------------------------------ |
| 154 |
textPlain :: L.ByteString -> Snap () |
| 155 |
textPlain = withContentType "text/plain" |
| 156 |
|
| 157 |
|
| 158 |
------------------------------------------------------------------------------ |
| 159 |
binary :: L.ByteString -> Snap () |
| 160 |
binary = withContentType "application/octet-stream" |
| 161 |
|
| 162 |
|
| 163 |
------------------------------------------------------------------------------ |
| 164 |
testNoHeaders :: Test |
| 165 |
testNoHeaders = testProperty "gzip/noheaders" $ |
| 166 |
monadicIO $ |
| 167 |
forAllM arbitrary prop |
| 168 |
where |
| 169 |
prop :: L.ByteString -> PropertyM IO () |
| 170 |
prop s = liftQ $ do |
| 171 |
-- if there's no content-type, withCompression should be a no-op |
| 172 |
(!_,!rsp) <- goNoHeaders (seqSnap $ withCompression |
| 173 |
$ noContentType s) |
| 174 |
assertEqual "" Nothing $ getHeader "Content-Encoding" rsp |
| 175 |
assertEqual "" Nothing $ getHeader "Vary" rsp |
| 176 |
body <- Test.getResponseBody rsp |
| 177 |
|
| 178 |
assertEqual "" s $ L.fromChunks [body] |
| 179 |
|
| 180 |
|
| 181 |
------------------------------------------------------------------------------ |
| 182 |
testNoAcceptEncoding :: Test |
| 183 |
testNoAcceptEncoding = testProperty "gzip/noAcceptEncoding" $ |
| 184 |
monadicIO $ |
| 185 |
forAllM arbitrary prop |
| 186 |
where |
| 187 |
prop :: L.ByteString -> PropertyM IO () |
| 188 |
prop s = liftQ $ do |
| 189 |
-- if there's no accept-encoding, withCompression should be a no-op |
| 190 |
(!_,!rsp) <- goNoHeaders (seqSnap $ withCompression |
| 191 |
$ textPlain s) |
| 192 |
assertEqual "" Nothing $ getHeader "Content-Encoding" rsp |
| 193 |
assertEqual "" Nothing $ getHeader "Vary" rsp |
| 194 |
|
| 195 |
body <- Test.getResponseBody rsp |
| 196 |
assertEqual "" s $ L.fromChunks [body] |
| 197 |
|
| 198 |
|
| 199 |
------------------------------------------------------------------------------ |
| 200 |
testAcceptEncodingBad :: Test |
| 201 |
testAcceptEncodingBad = testCase "gzip/acceptEncodingBad" $ do |
| 202 |
expectExceptionH $ Test.runHandler (Test.setHeader "Accept-Encoding" "$") |
| 203 |
snap |
| 204 |
expectExceptionH $ Test.runHandler |
| 205 |
(Test.setHeader "Accept-Encoding" "gzip;q=^") snap |
| 206 |
where |
| 207 |
snap = withCompression $ do |
| 208 |
modifyResponse $ setHeader "Content-Type" "text/plain" |
| 209 |
writeBS "ok" |
| 210 |
|
| 211 |
|
| 212 |
------------------------------------------------------------------------------ |
| 213 |
testIdentity1 :: Test |
| 214 |
testIdentity1 = testProperty "gzip/identity1" $ monadicIO $ forAllM arbitrary prop |
| 215 |
where |
| 216 |
prop :: L.ByteString -> PropertyM IO () |
| 217 |
prop s = liftQ $ do |
| 218 |
(!_,!rsp) <- goGZip (seqSnap $ withCompression $ textPlain s) |
| 219 |
assertEqual "" (Just "gzip") $ getHeader "Content-Encoding" rsp |
| 220 |
assertEqual "" (Just "Accept-Encoding") $ getHeader "Vary" rsp |
| 221 |
|
| 222 |
body <- Test.getResponseBody rsp |
| 223 |
let s1 = GZip.decompress $ L.fromChunks [body] |
| 224 |
assertEqual "" s s1 |
| 225 |
|
| 226 |
|
| 227 |
------------------------------------------------------------------------------ |
| 228 |
testIdentity1_charset :: Test |
| 229 |
testIdentity1_charset = testProperty "gzip/identity1_charset" $ |
| 230 |
monadicIO $ forAllM arbitrary prop |
| 231 |
where |
| 232 |
prop :: L.ByteString -> PropertyM IO () |
| 233 |
prop s = liftQ $ do |
| 234 |
(!_,!rsp) <- goGZip (seqSnap $ withCompression $ |
| 235 |
withContentType "text/plain; charset=utf-8" s) |
| 236 |
assertEqual "" (Just "gzip") $ getHeader "Content-Encoding" rsp |
| 237 |
assertEqual "" (Just "Accept-Encoding") $ getHeader "Vary" rsp |
| 238 |
|
| 239 |
body <- Test.getResponseBody rsp |
| 240 |
let s1 = GZip.decompress $ L.fromChunks [body] |
| 241 |
assertEqual "" s s1 |
| 242 |
|
| 243 |
|
| 244 |
------------------------------------------------------------------------------ |
| 245 |
testIdentity2 :: Test |
| 246 |
testIdentity2 = testProperty "gzip/identity2" $ monadicIO $ |
| 247 |
forAllM arbitrary prop |
| 248 |
where |
| 249 |
prop :: L.ByteString -> PropertyM IO () |
| 250 |
prop s = liftQ $ do |
| 251 |
(!_,!rsp) <- goDeflate (seqSnap $ withCompression $ textPlain s) |
| 252 |
|
| 253 |
assertEqual "" (Just "deflate") $ getHeader "Content-Encoding" rsp |
| 254 |
assertEqual "" (Just "Accept-Encoding") $ getHeader "Vary" rsp |
| 255 |
|
| 256 |
body <- Test.getResponseBody rsp |
| 257 |
let s' = Zlib.decompress $ L.fromChunks [body] |
| 258 |
assertEqual "" s s' |
| 259 |
|
| 260 |
|
| 261 |
------------------------------------------------------------------------------ |
| 262 |
testIdentity3 :: Test |
| 263 |
testIdentity3 = testProperty "gzip/identity3" $ monadicIO $ forAllM arbitrary prop |
| 264 |
where |
| 265 |
prop :: L.ByteString -> PropertyM IO () |
| 266 |
prop s = liftQ $ do |
| 267 |
(!_,!rsp) <- goGZip (seqSnap $ withCompression $ binary s) |
| 268 |
body <- Test.getResponseBody rsp |
| 269 |
assertEqual "identify" s $ L.fromChunks [body] |
| 270 |
|
| 271 |
|
| 272 |
------------------------------------------------------------------------------ |
| 273 |
testIdentity4 :: Test |
| 274 |
testIdentity4 = testProperty "gzip/identity4" $ monadicIO $ forAllM arbitrary prop |
| 275 |
where |
| 276 |
prop :: L.ByteString -> PropertyM IO () |
| 277 |
prop s = liftQ $ do |
| 278 |
(!_,!rsp) <- goXGZip (seqSnap $ withCompression $ textPlain s) |
| 279 |
assertEqual "encoding" (Just "x-gzip") (getHeader "Content-Encoding" rsp) |
| 280 |
body <- Test.getResponseBody rsp |
| 281 |
let s1 = GZip.decompress $ L.fromChunks [body] |
| 282 |
assertEqual "identity" s s1 |
| 283 |
|
| 284 |
(!_,!rsp2) <- goXGZip2 (seqSnap $ withCompression $ textPlain s) |
| 285 |
assertEqual "encoding" (Just "x-gzip") (getHeader "Content-Encoding" rsp2) |
| 286 |
body2 <- Test.getResponseBody rsp2 |
| 287 |
let s2 = GZip.decompress $ L.fromChunks [body2] |
| 288 |
assertEqual "identity2" s s2 |
| 289 |
|
| 290 |
|
| 291 |
------------------------------------------------------------------------------ |
| 292 |
testIdentity5 :: Test |
| 293 |
testIdentity5 = testProperty "gzip/identity5" $ monadicIO $ forAllM arbitrary prop |
| 294 |
where |
| 295 |
prop :: L.ByteString -> PropertyM IO () |
| 296 |
prop s = liftQ $ do |
| 297 |
(!_,!rsp) <- goXDeflate (seqSnap $ withCompression $ textPlain s) |
| 298 |
|
| 299 |
assertEqual "" (Just "x-deflate") $ getHeader "Content-Encoding" rsp |
| 300 |
body <- Test.getResponseBody rsp |
| 301 |
let s2 = Zlib.decompress $ L.fromChunks [body] |
| 302 |
assertEqual "gzip" s s2 |
| 303 |
|
| 304 |
|
| 305 |
------------------------------------------------------------------------------ |
| 306 |
testBadHeaders :: Test |
| 307 |
testBadHeaders = testProperty "gzip/bad headers" $ monadicIO $ forAllM arbitrary prop |
| 308 |
where |
| 309 |
prop :: L.ByteString -> PropertyM IO () |
| 310 |
prop s = expectException $ do |
| 311 |
(!_,!rsp) <- goBad (seqSnap $ withCompression $ textPlain s) |
| 312 |
Test.getResponseBody rsp |
| 313 |
|
| 314 |
|
| 315 |
------------------------------------------------------------------------------ |
| 316 |
testNopWhenContentEncodingSet :: Test |
| 317 |
testNopWhenContentEncodingSet = |
| 318 |
testProperty "gzip/testNopWhenContentEncodingSet" $ |
| 319 |
monadicIO $ |
| 320 |
forAllM arbitrary prop |
| 321 |
where |
| 322 |
prop :: L.ByteString -> PropertyM IO () |
| 323 |
prop s = do |
| 324 |
(!_,!rsp) <- liftQ $ goGZip $ f s |
| 325 |
assert $ getHeader "Content-Encoding" rsp == Just "identity" |
| 326 |
|
| 327 |
f !s = seqSnap $ withCompression $ do |
| 328 |
modifyResponse $ setHeader "Content-Encoding" "identity" |
| 329 |
textPlain s |
| 330 |
|
| 331 |
|
| 332 |
------------------------------------------------------------------------------ |
| 333 |
testCompositionDoesn'tExplode :: Test |
| 334 |
testCompositionDoesn'tExplode = |
| 335 |
testProperty "gzip/testCompositionDoesn'tExplode" $ |
| 336 |
monadicIO $ |
| 337 |
forAllM arbitrary prop |
| 338 |
where |
| 339 |
prop :: L.ByteString -> PropertyM IO () |
| 340 |
prop s = liftQ $ do |
| 341 |
(!_,!rsp) <- goGZip (seqSnap $ |
| 342 |
withCompression $ |
| 343 |
withCompression $ |
| 344 |
withCompression $ textPlain s) |
| 345 |
|
| 346 |
assertEqual "" (Just "gzip") $ getHeader "Content-Encoding" rsp |
| 347 |
|
| 348 |
c <- Test.getResponseBody rsp |
| 349 |
let s1 = GZip.decompress $ L.fromChunks [c] |
| 350 |
assertEqual "composition" s s1 |
| 351 |
|
| 352 |
|
| 353 |
------------------------------------------------------------------------------ |
| 354 |
testGzipLotsaChunks :: Test |
| 355 |
testGzipLotsaChunks = testCase "gzip/lotsOfChunks" prop |
| 356 |
where |
| 357 |
prop = do |
| 358 |
a <- genRandom 12000 |
| 359 |
let s = L.take 120000 $ L.cycle $ L.fromChunks [a, B.reverse a] |
| 360 |
(!_,!rsp) <- goGZip (seqSnap $ withCompression $ textPlain s) |
| 361 |
|
| 362 |
body <- Test.getResponseBody rsp |
| 363 |
let s1 = GZip.decompress $ L.fromChunks [body] |
| 364 |
H.assertEqual "streams equal" s s1 |
| 365 |
|
| 366 |
genRandom n = B.pack <$> replicateM n randomWord8 |
| 367 |
|
| 368 |
t8 c = toEnum $ c .&. 0xff |
| 369 |
|
| 370 |
randomWord8 :: IO Word8 |
| 371 |
randomWord8 = t8 <$> randomIO |
| 372 |
|
| 373 |
|
| 374 |
------------------------------------------------------------------------------ |
| 375 |
testNoCompression :: Test |
| 376 |
testNoCompression = testProperty "gzip/noCompression" $ |
| 377 |
monadicIO $ forAllM arbitrary prop |
| 378 |
where |
| 379 |
prop :: L.ByteString -> PropertyM IO () |
| 380 |
prop s = liftQ $ do |
| 381 |
(!_,!rsp) <- goGZip (seqSnap $ withCompression $ |
| 382 |
(noCompression >> textPlain s)) |
| 383 |
assertEqual "" (Just "identity") $ getHeader "Content-Encoding" rsp |
| 384 |
body <- Test.getResponseBody rsp |
| 385 |
assertEqual "body matches" (S.concat $ L.toChunks s) body |
| 386 |
|
| 387 |
|
| 388 |
------------------------------------------------------------------------------ |
| 389 |
testTrivials :: Test |
| 390 |
testTrivials = testCase "gzip/trivials" $ do |
| 391 |
coverTypeableInstance (undefined :: BadAcceptEncodingException) |