{-# LANGUAGE OverloadedStrings, CPP, ScopedTypeVariables #-} module Web.ScottySpec (main, spec) where import Test.Hspec import Test.Hspec.Wai import Control.Applicative import Control.Monad import Data.Char import Data.String import qualified Data.Text.Lazy as TL import qualified Data.Text.Lazy.Encoding as TLE import Network.HTTP.Types import Network.Wai (Application, Request(queryString), responseLBS) import qualified Control.Exception.Lifted as EL import qualified Control.Exception as E import Web.Scotty as Scotty hiding (get, post, put, patch, delete, request, options) import qualified Web.Scotty as Scotty import qualified Web.Scotty.Cookie as SC (getCookie, setSimpleCookie, deleteCookie) #if !defined(mingw32_HOST_OS) import Control.Concurrent.Async (withAsync) import Control.Exception (bracketOnError) import qualified Data.ByteString as BS import Data.ByteString (ByteString) import Network.Socket (Family(..), SockAddr(..), Socket, SocketOption(..), SocketType(..), bind, close, connect, listen, maxListenQueue, setSocketOption, socket) import Network.Socket.ByteString (send, recv) import System.Directory (removeFile) #endif main :: IO () main = hspec spec availableMethods :: [StdMethod] availableMethods = [GET, POST, HEAD, PUT, PATCH, DELETE, OPTIONS] spec :: Spec spec = do let withApp = with . scottyApp describe "ScottyM" $ do forM_ [ ("GET", Scotty.get, get) , ("POST", Scotty.post, (`post` "")) , ("PUT", Scotty.put, (`put` "")) , ("PATCH", Scotty.patch, (`patch` "")) , ("DELETE", Scotty.delete, delete) , ("OPTIONS", Scotty.options, options) ] $ \(method, route, makeRequest) -> do describe (map toLower method) $ do withApp (route "/scotty" $ html "") $ do it ("adds route for " ++ method ++ " requests") $ do makeRequest "/scotty" `shouldRespondWith` 200 withApp (route "/scotty" $ html "") $ do it ("properly handles extra slash routes for " ++ method ++ " requests") $ do makeRequest "//scotty" `shouldRespondWith` 200 withApp (route "/:paramName" $ captureParam "paramName" >>= text) $ do it ("captures route parameters for " ++ method ++ " requests with url encoded '/' in path") $ do makeRequest "/a%2Fb" `shouldRespondWith` "a/b" describe "addroute" $ do forM_ availableMethods $ \method -> do withApp (addroute method "/scotty" $ html "") $ do it ("can be used to add route for " ++ show method ++ " requests") $ do request (renderStdMethod method) "/scotty" [] "" `shouldRespondWith` 200 describe "matchAny" $ do withApp (matchAny "/scotty" $ html "") $ do forM_ ("NONSTANDARD" : fmap renderStdMethod availableMethods) $ \method -> do it ("adds route that matches " ++ show method ++ " requests") $ do request method "/scotty" [] "" `shouldRespondWith` 200 describe "notFound" $ do withApp (notFound $ html "my custom not found page") $ do it "adds handler for requests that do not match any route" $ do get "/somewhere" `shouldRespondWith` "my custom not found page" {matchStatus = 404} withApp (notFound $ status status400 >> html "my custom not found page") $ do it "allows to customize the HTTP status code" $ do get "/somewhere" `shouldRespondWith` "my custom not found page" {matchStatus = 400} context "when not specified" $ do withApp (return ()) $ do it "returns 404 when no route matches" $ do get "/" `shouldRespondWith` "
ру́сский язы́к
" withApp (Scotty.get "/scotty" $ html russianLanguageTextInHtml) $ do it "sets body to given text" $ do get "/scotty" `shouldRespondWith` russianLanguageTextInHtml it "sets Content-Type header to \"text/html; charset=utf-8\"" $ do get "/scotty" `shouldRespondWith` 200 {matchHeaders = ["Content-Type" <:> "text/html; charset=utf-8"]} withApp (Scotty.get "/scotty" $ setHeader "Content-Type" "text/somethingweird" >> html russianLanguageTextInHtml) $ do it "doesn't override a previously set Content-Type header" $ do get "/scotty" `shouldRespondWith` 200 {matchHeaders = ["Content-Type" <:> "text/somethingweird"]} describe "json" $ do withApp (Scotty.get "/scotty" $ setHeader "Content-Type" "text/somethingweird" >> json (Just (5::Int))) $ do it "doesn't override a previously set Content-Type header" $ do get "/scotty" `shouldRespondWith` 200 {matchHeaders = ["Content-Type" <:> "text/somethingweird"]} describe "finish" $ do withApp (Scotty.get "/scotty" $ finish) $ do it "responds with 200 by default" $ do get "/scotty" `shouldRespondWith` 200 withApp (Scotty.get "/scotty" $ status status400 >> finish >> status status200) $ do it "stops the execution of an action" $ do get "/scotty" `shouldRespondWith` 400 describe "setSimpleCookie" $ do withApp (Scotty.get "/scotty" $ SC.setSimpleCookie "foo" "bar") $ do it "responds with a Set-Cookie header" $ do get "/scotty" `shouldRespondWith` 200 {matchHeaders = ["Set-Cookie" <:> "foo=bar"]} describe "getCookie" $ do withApp (Scotty.get "/scotty" $ do mt <- SC.getCookie "foo" case mt of Just "bar" -> Scotty.status status200 _ -> Scotty.status status400 ) $ do it "finds the right cookie in the request headers" $ do request "GET" "/scotty" [("Cookie", "foo=bar")] "" `shouldRespondWith` 200 describe "deleteCookie" $ do withApp (Scotty.get "/scotty" $ SC.deleteCookie "foo") $ do it "responds with a Set-Cookie header with expiry date Jan 1, 1970" $ do get "/scotty" `shouldRespondWith` 200 {matchHeaders = ["Set-Cookie" <:> "foo=; Expires=Thu, 01-Jan-1970 00:00:00 GMT"]} describe "nested" $ do let simpleApp :: Application simpleApp _ respond = do putStrLn "I've done some IO here" respond $ responseLBS status200 [("Content-Type", "text/plain")] "Hello, Web!" withApp (Scotty.get "/nested" (nested simpleApp)) $ do it "responds with the expected simpleApp response" $ do get "/nested" `shouldRespondWith` 200 {matchHeaders = ["Content-Type" <:> "text/plain"], matchBody = "Hello, Web!"} -- Unix sockets not available on Windows #if !defined(mingw32_HOST_OS) describe "scottySocket" . it "works with a unix socket" . withServer (Scotty.get "/scotty" $ html "") . E.bracket (socket AF_UNIX Stream 0) close $ \sock -> do connect sock $ SockAddrUnix socketPath _ <- send sock "GET /scotty HTTP/1.1\r\n\n" r1 <- recv sock 1024 _ <- send sock "GET /four-oh-four HTTP/1.1\r\n\n" r2 <- recv sock 1024 (BS.take (BS.length ok) r1, BS.take (BS.length no) r2) `shouldBe` (ok, no) where ok, no :: ByteString ok = "HTTP/1.1 200 OK" no = "HTTP/1.1 404 Not Found" socketPath :: FilePath socketPath = "/tmp/scotty-test.socket" withServer :: ScottyM () -> IO a -> IO a withServer actions inner = E.bracket (listenOn socketPath) (\sock -> close sock >> removeFile socketPath) (\sock -> withAsync (Scotty.scottySocket defaultOptions sock actions) $ const inner) -- See https://github.com/haskell/network/issues/318 listenOn :: String -> IO Socket listenOn path = bracketOnError (socket AF_UNIX Stream 0) close (\sock -> do setSocketOption sock ReuseAddr 1 bind sock (SockAddrUnix path) listen sock maxListenQueue return sock ) #endif