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
|
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Snap.Internal.Http.Server.Parser.Tests (tests) where
------------------------------------------------------------------------------
import Control.Monad (liftM)
import Control.Parallel.Strategies (rdeepseq, using)
import qualified Data.ByteString.Char8 as S
import qualified Data.ByteString.Lazy.Char8 as L
import Data.List (sort)
import qualified Data.Map as Map
import Data.Monoid (mconcat)
import Text.Printf (printf)
------------------------------------------------------------------------------
import Data.ByteString.Builder (byteString, toLazyByteString)
import Test.Framework (Test)
import Test.Framework.Providers.HUnit (testCase)
import Test.Framework.Providers.QuickCheck2 (testProperty)
import Test.HUnit (assertEqual)
import Test.QuickCheck (Arbitrary (arbitrary))
import Test.QuickCheck.Monadic (forAllM, monadicIO)
import qualified Test.QuickCheck.Monadic as QC
------------------------------------------------------------------------------
import Snap.Internal.Debug (debug)
import Snap.Internal.Http.Server.Parser (HttpParseException (..), IRequest (IRequest, iMethod, iRequestHeaders, iStdHeaders), getStdHost, parseCookie, parseRequest, parseUrlEncoded, readChunkedTransferEncoding, writeChunkedTransferEncoding)
import Snap.Internal.Http.Types (Cookie (Cookie), Method (CONNECT, DELETE, GET, HEAD, Method, OPTIONS, PATCH, POST, PUT, TRACE))
import Snap.Test.Common (coverEqInstance, coverShowInstance, coverTypeableInstance, expectException)
import qualified Snap.Types.Headers as H
import qualified System.IO.Streams as Streams
------------------------------------------------------------------------------
tests :: [Test]
tests = [ testShow
, testCookie
, testChunked
, testNull
, testPartial
, testParseError
, testFormEncoded
, testTrivials
, testMethods
, testSimpleParse
, testSimpleParseErrors
, testWriteChunkedTransferEncoding
]
------------------------------------------------------------------------------
testShow :: Test
testShow = testCase "parser/show" $ do
let i = IRequest GET "/" (1, 1) H.empty undefined
let !b = show i `using` rdeepseq
return $ b `seq` ()
------------------------------------------------------------------------------
testNull :: Test
testNull = testCase "parser/shortParse" $
expectException (Streams.fromList [] >>= parseRequest)
------------------------------------------------------------------------------
testPartial :: Test
testPartial = testCase "parser/partial" $
expectException (Streams.fromList ["GET / "] >>= parseRequest)
------------------------------------------------------------------------------
testParseError :: Test
testParseError = testCase "parser/error" $ do
expectException (Streams.fromList ["ZZZZZZZZZ"] >>= parseRequest)
expectException (Streams.fromList ["GET / HTTP/1.1"] >>= parseRequest)
expectException (Streams.fromList ["GET / HTTP/x.z\r\n\r\n"] >>=
parseRequest)
------------------------------------------------------------------------------
-- | convert a bytestring to chunked transfer encoding
transferEncodingChunked :: L.ByteString -> L.ByteString
transferEncodingChunked = f . L.toChunks
where
toChunk s = L.concat [ len, "\r\n", L.fromChunks [s], "\r\n" ]
where
len = L.pack $ printf "%x" $ S.length s
f l = L.concat $ (map toChunk l ++ ["0\r\n\r\n"])
------------------------------------------------------------------------------
-- | ensure that running 'readChunkedTransferEncoding' against
-- 'transferEncodingChunked' returns the original string
testChunked :: Test
testChunked = testProperty "parser/chunkedTransferEncoding" $
monadicIO $ forAllM arbitrary prop_chunked
where
prop_chunked s = QC.run $ do
debug "=============================="
debug $ "input is " ++ show s
debug $ "chunked is " ++ show chunked
debug "------------------------------"
out <- Streams.fromList (L.toChunks chunked) >>=
readChunkedTransferEncoding >>=
Streams.toList >>=
return . L.fromChunks
assertEqual "chunked" s out
debug "==============================\n"
where
chunked = transferEncodingChunked s
------------------------------------------------------------------------------
testWriteChunkedTransferEncoding :: Test
testWriteChunkedTransferEncoding = testCase "parser/writeChunked" $ do
(os, getList) <- Streams.listOutputStream
os' <- writeChunkedTransferEncoding os
Streams.fromList [byteString "ok"] >>= Streams.connectTo os'
Streams.write Nothing os'
s <- liftM (toLazyByteString . mconcat) getList
assertEqual "chunked" "002\r\nok\r\n0\r\n\r\n" s
------------------------------------------------------------------------------
testCookie :: Test
testCookie =
testCase "parser/parseCookie" $ do
assertEqual "cookie parsing" (Just [cv]) cv2
where
cv = Cookie nm v Nothing Nothing Nothing False False
cv2 = parseCookie ct
nm = "foo"
v = "bar"
ct = S.concat [ nm , "=" , v ]
------------------------------------------------------------------------------
testFormEncoded :: Test
testFormEncoded = testCase "parser/formEncoded" $ do
let bs = "foo1=bar1&foo2=bar2+baz2;foo3=foo%20bar"
let mp = parseUrlEncoded bs
assertEqual "foo1" (Just ["bar1"] ) $ Map.lookup "foo1" mp
assertEqual "foo2" (Just ["bar2 baz2"]) $ Map.lookup "foo2" mp
assertEqual "foo3" (Just ["foo bar"] ) $ Map.lookup "foo3" mp
------------------------------------------------------------------------------
testTrivials :: Test
testTrivials = testCase "parser/trivials" $ do
coverTypeableInstance (undefined :: HttpParseException)
coverShowInstance (HttpParseException "ok")
coverEqInstance (IRequest GET "" (0, 0) H.empty undefined)
------------------------------------------------------------------------------
testMethods :: Test
testMethods = testCase "parser/methods" $ mapM_ testOne ms
where
ms = [ GET, POST, HEAD, PUT, DELETE, TRACE, OPTIONS, CONNECT, PATCH
, Method "ZZZ" ]
mToStr (Method m) = m
mToStr m = S.pack $ show m
restOfRequest = [ " / HTTP/1.1\r\nz:b\r\nq:\r\nw\r\n", "foo: ", "bar"
, "\r\n baz\r\n\r\n" ]
testOne m = let s = mToStr m
in Streams.fromList (s:restOfRequest) >>=
parseRequest >>=
checkMethod m
checkMethod m i = do
assertEqual "method" m $ iMethod i
let expected = sort [ ("z", "b")
, ("q", "")
, ("w", "")
, ("foo", "bar baz")
]
assertEqual "hdrs" expected $ sort $ H.toList $ iRequestHeaders i
------------------------------------------------------------------------------
testSimpleParse :: Test
testSimpleParse = testCase "parser/simpleParse" $ do
Streams.fromList ["GET / HTTP/1.1\r\n\r\n"] >>=
parseRequest >>=
assertEqual "simple0" (IRequest GET "/" (1, 1) H.empty undefined)
Streams.fromList ["GET http://foo.com/ HTTP/1.1\r\n\r\n"] >>=
parseRequest >>=
assertEqual "simple1" (IRequest GET "/" (1, 1) H.empty undefined)
z <- Streams.fromList ["GET http://foo.com HTTP/1.1\r\n\r\n"] >>=
parseRequest
assertEqual "simple2" z (IRequest GET "/" (1, 1) H.empty undefined)
assertEqual "simple2-host" (Just "foo.com") (getStdHost $ iStdHeaders z)
z2 <- Streams.fromList ["GET https://foo.com/ HTTP/1.1\r\n\r\n"] >>=
parseRequest
assertEqual "simpleHttps" (IRequest GET "/" (1, 1) H.empty undefined) z2
assertEqual "simpleHttps-2" (Just "foo.com") (getStdHost $ iStdHeaders z2)
Streams.fromList ["GET / HTTP/1.1\r\nz:b\r\n", "", "\r\n"] >>=
parseRequest >>=
assertEqual "simple4" (IRequest GET "/" (1, 1)
(H.fromList [("z", "b")]) undefined)
Streams.fromList [ "GET / HTTP/1.1\r\na:a\r", "\nz:b\r\n", ""
, "\r\n" ] >>=
parseRequest >>=
assertEqual "simple5" (IRequest GET "/" (1, 1)
(H.fromList [("a", "a"), ("z", "b")]) undefined)
Streams.fromList ["GET /\r\n\r\n"] >>=
parseRequest >>=
assertEqual "simple6" (IRequest GET "/" (1, 0) H.empty undefined)
Streams.fromList ["G", "ET", " /\r", "\n\r", "", "\n"] >>=
parseRequest >>=
assertEqual "simple7" (IRequest GET "/" (1, 0) H.empty undefined)
------------------------------------------------------------------------------
testSimpleParseErrors :: Test
testSimpleParseErrors = testCase "parser/simpleParseErrors" $ do
expectException (
Streams.fromList ["\r\nGET / HTTP/1.1\r\nz:b\r\n \r\n"] >>=
parseRequest)
expectException (
Streams.fromList ["\r\nGET / HTTP/1.1\r\nz:b\r "] >>=
parseRequest)
expectException (
Streams.fromList ["\r\nGET / HTTP/1.1\r"] >>=
parseRequest)
expectException (
Streams.fromList ["\r\nGET / HTTP/1.1\r", "", "foo\nz:b\r "] >>=
parseRequest)
|