LoginSignup
0

More than 5 years have passed since last update.

Network.Wai.Test

Posted at

Haskell版 WSGI である WAI のテストを書くためのモジュール。任意のアプリについて、テストのリクエストを作って走らせて結果を見れる。

% runghc
{-# LANGUAGE OverloadedStrings #-}
module Main (main) where
import qualified Blaze.ByteString.Builder.Word as W
import Codec.Binary.UTF8.String as U
import Data.ByteString.Lazy as L
import qualified Network.HTTP.Types.Status as ST
import Network.Wai
import Network.Wai.Test

app :: Application
app _ = return $ ResponseBuilder status headers body
  where
    status = ST.status200
    headers = [("Content-Type", "text/plain")]
    body = W.fromWord8s . U.encode $ "わい。\n"

main :: IO ()
main = do
  res <- runSession (request req) app
  mapM_ (($ res) . (Prelude.putStrLn .)) [
    show . simpleStatus, show . simpleHeaders,
    U.decode . L.unpack . simpleBody
    ]
  return ()
  where
    req = (flip setRawPathInfo "/") defaultRequest
Status {statusCode = 200, statusMessage = "OK"}
[("Content-Type","text/plain")]
わい。

WAIってWSGIのポジションを狙ってる割には仕様にBlazeやらResourceTやらConduitやら巻き込んでてごついのがちょっと。パフォーマンスの都合ってのはわかるけどもう少し標準APIでなんとかならないのかな。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0