1{-# LANGUAGE CPP #-} 2 3module FrameSpec where 4 5#if __GLASGOW_HASKELL__ < 709 6import Control.Applicative ((<$>)) 7#endif 8import Control.Monad (forM_) 9import Data.Aeson (eitherDecode) 10import qualified Data.ByteString.Base16 as B16 11import qualified Data.ByteString.Lazy as BL 12import Network.HTTP2.Frame 13import System.FilePath.Glob (compile, globDir) 14import Test.Hspec 15 16import JSON 17 18testDir :: FilePath 19testDir = "test-frame/http2-frame-test-case" 20 21getTestFiles :: FilePath -> IO [FilePath] 22getTestFiles dir = head <$> globDir [compile "*/*.json"] dir 23 24check :: FilePath -> IO () 25check file = do 26 bs <- BL.readFile file 27 let etc = eitherDecode bs :: Either String Case 28 case etc of 29 Left _ -> putStrLn $ "JSON error: " ++ file 30 Right tc -> do 31 let bin = B16.decodeLenient $ wire tc 32 erc = decodeFrame defaultSettings bin 33 case erc of 34 Left h2err -> case err tc of 35 Nothing -> do 36 putStrLn file -- fixme 37 print h2err 38 Just errs -> do 39 let e = fromErrorCodeId $ errorCodeId h2err 40 errs `shouldContain` [e] 41 Right frm -> do 42 case frame tc of 43 Just fp -> do 44 fpFrame fp `shouldBe` frm 45 let einfo = EncodeInfo { 46 encodeFlags = flags $ frameHeader $ fpFrame fp 47 , encodeStreamId = streamId (frameHeader frm) 48 , encodePadding = unPad <$> fpPad fp 49 } 50 payload = framePayload frm 51 encodeFrame einfo payload `shouldBe` bin 52 Nothing -> putStrLn file -- fixme 53 54spec :: Spec 55spec = do 56 describe "decodeFrame and encodeFrame" $ do 57 it "decodes test cases well" $ do 58 files <- getTestFiles testDir 59 forM_ files check 60