Skip to content

Commit

Permalink
Add a 'noCompression' function to the gzip utilities.
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorycollins committed Oct 12, 2011
1 parent 53b8d6c commit ef43c1d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Snap/Util/GZip.hs
Expand Up @@ -6,7 +6,8 @@


module Snap.Util.GZip module Snap.Util.GZip
( withCompression ( withCompression
, withCompression' ) where , withCompression'
, noCompression ) where


import Blaze.ByteString.Builder import Blaze.ByteString.Builder
import qualified Codec.Zlib.Enum as Z import qualified Codec.Zlib.Enum as Z
Expand Down Expand Up @@ -122,6 +123,13 @@ withCompression' mimeTable action = do
chooseType (_:xs) = chooseType xs chooseType (_:xs) = chooseType xs




------------------------------------------------------------------------------
-- | Turn off compression by setting \"Content-Encoding: identity\" in the
-- response headers.
noCompression :: MonadSnap m => m ()
noCompression = modifyResponse $ setHeader "Content-Encoding" "identity"


------------------------------------------------------------------------------ ------------------------------------------------------------------------------
-- private following -- private following
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
Expand Down
22 changes: 22 additions & 0 deletions test/suite/Snap/Util/GZip/Tests.hs
Expand Up @@ -50,6 +50,7 @@ tests = [ testIdentity1
, testNopWhenContentEncodingSet , testNopWhenContentEncodingSet
, testCompositionDoesn'tExplode , testCompositionDoesn'tExplode
, testGzipLotsaChunks , testGzipLotsaChunks
, testNoCompression
, testBadHeaders ] , testBadHeaders ]




Expand Down Expand Up @@ -401,3 +402,24 @@ testGzipLotsaChunks = testCase "gzip/lotsOfChunks" prop
frobnicate s = let s' = encode $ md5 $ L.fromChunks [s] frobnicate s = let s' = encode $ md5 $ L.fromChunks [s]
in (s:frobnicate s') in (s:frobnicate s')



------------------------------------------------------------------------------
testNoCompression :: Test
testNoCompression = testProperty "gzip/noCompression" $
monadicIO $ forAllM arbitrary prop
where
prop :: L.ByteString -> PropertyM IO ()
prop s = do
(!_,!rsp) <- liftQ $ goGZip (seqSnap $ withCompression $
(noCompression >> textPlain s))
assert $ getHeader "Content-Encoding" rsp == Just "identity"
let body = rspBodyToEnum $ rspBody rsp

s1 <- liftQ $
runIteratee stream2stream >>= run_ . body

assert $ s == s1




0 comments on commit ef43c1d

Please sign in to comment.