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
|
{-# LANGUAGE CPP #-}
module FrameSpec where
#if __GLASGOW_HASKELL__ < 709
import Control.Applicative ((<$>))
#endif
import Control.Monad (forM_)
import Data.Aeson (eitherDecode)
import qualified Data.ByteString.Base16 as B16
import qualified Data.ByteString.Lazy as BL
import Network.HTTP2.Frame
import System.FilePath.Glob (compile, globDir)
import Test.Hspec
import JSON
testDir :: FilePath
testDir = "test-frame/http2-frame-test-case"
getTestFiles :: FilePath -> IO [FilePath]
getTestFiles dir = head <$> globDir [compile "*/*.json"] dir
check :: FilePath -> IO ()
check file = do
bs <- BL.readFile file
let etc = eitherDecode bs :: Either String Case
case etc of
Left _ -> putStrLn $ "JSON error: " ++ file
Right tc -> do
let bin = B16.decodeLenient $ wire tc
erc = decodeFrame bin
case erc of
Left fderr -> case err tc of
Nothing -> do
putStrLn file -- fixme
print fderr
Just errs -> do
let e = case fderr of
FrameDecodeError x _ _ -> x
errs `shouldContain` [e]
Right frm -> do
case frame tc of
Just fp -> do
fpFrame fp `shouldBe` frm
let einfo =
EncodeInfo
{ encodeFlags = flags $ frameHeader $ fpFrame fp
, encodeStreamId = streamId (frameHeader frm)
, encodePadding = unPad <$> fpPad fp
}
payload = framePayload frm
encodeFrame einfo payload `shouldBe` bin
Nothing -> putStrLn file -- fixme
spec :: Spec
spec = do
describe "decodeFrame and encodeFrame" $ do
it "decodes test cases well" $ do
files <- getTestFiles testDir
forM_ files check
|