Add test for FormUrlEncoded using Network.URL

This commit is contained in:
Timo von Holtz 2015-02-23 16:54:10 +11:00
parent 1d378e644c
commit 4f91a28d57
3 changed files with 14 additions and 1 deletions

View file

@ -94,3 +94,4 @@ test-suite spec
, servant , servant
, string-conversions , string-conversions
, text , text
, url

View file

@ -261,6 +261,7 @@ encodeFormUrlEncoded xs =
let escape :: TextS.Text -> ByteString let escape :: TextS.Text -> ByteString
escape = cs . escapeURIString isUnreserved . cs escape = cs . escapeURIString isUnreserved . cs
encodePair :: (TextS.Text, TextS.Text) -> ByteString encodePair :: (TextS.Text, TextS.Text) -> ByteString
encodePair (k, "") = escape k
encodePair (k, v) = escape k <> "=" <> escape v encodePair (k, v) = escape k <> "=" <> escape v
in B.intercalate "&" $ map encodePair xs in B.intercalate "&" $ map encodePair xs
@ -275,6 +276,7 @@ decodeFormUrlEncoded q = do
[k,v] -> return ( unescape k [k,v] -> return ( unescape k
, unescape v , unescape v
) )
[k] -> return ( unescape k, "" )
_ -> Left $ "not a valid pair: " <> cs p _ -> Left $ "not a valid pair: " <> cs p
unescape :: TextS.Text -> TextS.Text unescape :: TextS.Text -> TextS.Text
unescape = cs . unEscapeString . cs . TextS.intercalate "%20" . TextS.splitOn "+" unescape = cs . unEscapeString . cs . TextS.intercalate "%20" . TextS.splitOn "+"

View file

@ -6,11 +6,12 @@
module Servant.API.ContentTypesSpec where module Servant.API.ContentTypesSpec where
import Control.Applicative import Control.Applicative
import Control.Arrow
import Data.Aeson import Data.Aeson
import Data.Function (on) import Data.Function (on)
import Data.Proxy import Data.Proxy
import Data.ByteString.Char8 import Data.ByteString.Char8 (ByteString, append, pack)
import qualified Data.ByteString.Lazy as BSL import qualified Data.ByteString.Lazy as BSL
import Data.List (maximumBy) import Data.List (maximumBy)
import Data.Maybe (fromJust, isJust, isNothing) import Data.Maybe (fromJust, isJust, isNothing)
@ -19,6 +20,7 @@ import Data.String.Conversions (cs)
import qualified Data.Text as TextS import qualified Data.Text as TextS
import qualified Data.Text.Lazy as TextL import qualified Data.Text.Lazy as TextL
import GHC.Generics import GHC.Generics
import Network.URL (importParams, exportParams)
import Test.Hspec import Test.Hspec
import Test.QuickCheck import Test.QuickCheck
import Test.QuickCheck.Instances () import Test.QuickCheck.Instances ()
@ -44,6 +46,14 @@ spec = describe "Servant.API.ContentTypes" $ do
let p = Proxy :: Proxy FormUrlEncoded let p = Proxy :: Proxy FormUrlEncoded
property $ \x -> fromByteString p (toByteString p x) == Right (x::[(TextS.Text,TextS.Text)]) property $ \x -> fromByteString p (toByteString p x) == Right (x::[(TextS.Text,TextS.Text)])
it "has fromByteString reverse exportParams (Network.URL)" $ do
let p = Proxy :: Proxy FormUrlEncoded
property $ \x -> (fromByteString p . cs . exportParams . map (cs *** cs) $ x) == Right (x::[(TextS.Text,TextS.Text)])
it "has importParams (Network.URL) reverse toByteString" $ do
let p = Proxy :: Proxy FormUrlEncoded
property $ \x -> (fmap (map (cs *** cs)) . importParams . cs . toByteString p $ x) == Just (x::[(TextS.Text,TextS.Text)])
describe "The PlainText Content-Type type" $ do describe "The PlainText Content-Type type" $ do
it "has fromByteString reverse toByteString (lazy Text)" $ do it "has fromByteString reverse toByteString (lazy Text)" $ do