git.iko.soy/typeable/servant
on master help sign in
Fix errors in test suite for servant-js.
commit00179572ecb1b863fe3dd98a5438c9208e86642b authorArian van Putten <aeroboy94@gmail.com> dateFri 2 Oct 2015, 14:38 (11 years ago) parentcf59abd
2 files changed+38−27
servant-js/test/Servant/JSSpec.hs+35−26 servant-js/test/Servant/JSSpec/CustomHeaders.hs+3−1
servant-js/test/Servant/JSSpec.hs+35−26
@@ -4,13 +4,17 @@
44{-# LANGUAGE ScopedTypeVariables #-}
55{-# LANGUAGE TypeFamilies #-}
66{-# LANGUAGE TypeOperators #-}
7+{-# LANGUAGE OverloadedStrings #-}
78{-# OPTIONS_GHC -fno-warn-orphans #-}
89module Servant.JSSpec where
910
1011import Data.Either (isRight)
12+import Data.Monoid ((<>))
1113import Data.Proxy
12−import Language.ECMAScript3.Parser (parseFromString)
13−import Test.Hspec
14+import Data.Text (Text)
15+import qualified Data.Text as T
16+import Language.ECMAScript3.Parser (program, parse)
17+import Test.Hspec hiding (shouldContain, shouldNotContain)
1418
1519import Servant.JS
1620import Servant.JS.Internal
@@ -20,22 +24,22 @@ import qualified Servant.JS.JQuery as JQ
2024import qualified Servant.JS.Vanilla as JS
2125import Servant.JSSpec.CustomHeaders
2226
23−type TestAPI = "simple" :> ReqBody '[JSON,FormUrlEncoded] String :> Post '[JSON] Bool
27+type TestAPI = "simple" :> ReqBody '[JSON,FormUrlEncoded] Text :> Post '[JSON] Bool
2428 :<|> "has.extension" :> Get '[FormUrlEncoded,JSON] Bool
2529
2630type TopLevelRawAPI = "something" :> Get '[JSON] Int
2731 :<|> Raw
2832
29−type HeaderHandlingAPI = "something" :> Header "Foo" String
33+type HeaderHandlingAPI = "something" :> Header "Foo" Text
3034 :> Get '[JSON] Int
3135
32−type CustomAuthAPI = "something" :> Authorization "Basic" String
36+type CustomAuthAPI = "something" :> Authorization "Basic" Text
3337 :> Get '[JSON] Int
3438
35−type CustomHeaderAPI = "something" :> MyLovelyHorse String
39+type CustomHeaderAPI = "something" :> MyLovelyHorse Text
3640 :> Get '[JSON] Int
3741
38−type CustomHeaderAPI2 = "something" :> WhatsForDinner String
42+type CustomHeaderAPI2 = "something" :> WhatsForDinner Text
3943 :> Get '[JSON] Int
4044
4145headerHandlingProxy :: Proxy HeaderHandlingAPI
@@ -81,12 +85,16 @@ spec = describe "Servant.JQuery" $ do
8185 axiosSpec
8286 --angularSpec AngularCustom
8387
88+
89+a `shouldContain` b = shouldSatisfy a (T.isInfixOf b)
90+a `shouldNotContain` b = shouldNotSatisfy a (T.isInfixOf b)
91+
8492axiosSpec :: Spec
8593axiosSpec = describe specLabel $ do
8694 it "should add withCredentials when needed" $ do
8795 let jsText = genJS withCredOpts $ listFromAPI (Proxy :: Proxy TestAPI)
8896 output jsText
89− jsText `shouldContain` ("withCredentials: true")
97+ jsText `shouldContain` "withCredentials: true"
9098 it "should add xsrfCookieName when needed" $ do
9199 let jsText = genJS cookieOpts $ listFromAPI (Proxy :: Proxy TestAPI)
92100 output jsText
@@ -101,79 +109,80 @@ axiosSpec = describe specLabel $ do
101109 withCredOpts = AX.defAxiosOptions { AX.withCredentials = True }
102110 cookieOpts = AX.defAxiosOptions { AX.xsrfCookieName = Just "MyXSRFcookie" }
103111 headerOpts = AX.defAxiosOptions { AX.xsrfHeaderName = Just "MyXSRFheader" }
104− genJS :: AxiosOptions -> [AjaxReq] -> String
105− genJS opts req = concatMap (AX.generateAxiosJS opts) req
112+ genJS :: AxiosOptions -> [AjaxReq] -> Text
113+ genJS opts req = mconcat . map (AX.generateAxiosJS opts) $ req
106114
107115angularSpec :: TestNames -> Spec
108116angularSpec test = describe specLabel $ do
109117 it "should implement a service globally" $ do
110118 let jsText = genJS $ listFromAPI (Proxy :: Proxy TestAPI)
111119 output jsText
112− jsText `shouldContain` (".service('" ++ testName ++ "'")
120+ jsText `shouldContain` (".service('" <> testName <> "'")
113121
114122 it "should depend on $http service globally" $ do
115123 let jsText = genJS $ listFromAPI (Proxy :: Proxy TestAPI)
116124 output jsText
117− jsText `shouldContain` ("('" ++ testName ++ "', function($http) {")
125+ jsText `shouldContain` ("('" <> testName <> "', function($http) {")
118126
119127 it "should not depend on $http service in handlers" $ do
120128 let jsText = genJS $ listFromAPI (Proxy :: Proxy TestAPI)
121129 output jsText
122130 jsText `shouldNotContain` "getsomething($http, "
123131 where
124− specLabel = "AngularJS(" ++ (show test) ++ ")"
132+ specLabel = "AngularJS(" <> (show test) <> ")"
125133 output _ = return ()
126134 testName = "MyService"
127135 ngOpts = NG.defAngularOptions { NG.serviceName = testName }
128136 genJS req = NG.angularService ngOpts req
129137
130−generateJSSpec :: TestNames -> (AjaxReq -> String) -> Spec
138+parseFromText = parse program ""
139+generateJSSpec :: TestNames -> (AjaxReq -> Text) -> Spec
131140generateJSSpec n gen = describe specLabel $ do
132141 it "should generate valid javascript" $ do
133− let s = jsForAPI (Proxy :: Proxy TestAPI) (concatMap gen)
134− parseFromString s `shouldSatisfy` isRight
142+ let s = jsForAPI (Proxy :: Proxy TestAPI) (mconcat . map gen)
143+ parseFromText s `shouldSatisfy` isRight
135144
136145 it "should use non-empty function names" $ do
137146 let (_ :<|> topLevel) = javascript (Proxy :: Proxy TopLevelRawAPI)
138147 output $ genJS (topLevel "GET")
139− parseFromString (genJS $ topLevel "GET") `shouldSatisfy` isRight
148+ parseFromText (genJS $ topLevel "GET") `shouldSatisfy` isRight
140149
141150 it "should handle simple HTTP headers" $ do
142151 let jsText = genJS $ javascript headerHandlingProxy
143152 output jsText
144− parseFromString jsText `shouldSatisfy` isRight
153+ parseFromText jsText `shouldSatisfy` isRight
145154 jsText `shouldContain` "headerFoo"
146155 jsText `shouldContain` (header n "Foo" $ "headerFoo")
147156
148157 it "should handle complex HTTP headers" $ do
149158 let jsText = genJS $ javascript customAuthProxy
150159 output jsText
151− parseFromString jsText `shouldSatisfy` isRight
160+ parseFromText jsText `shouldSatisfy` isRight
152161 jsText `shouldContain` "headerAuthorization"
153162 jsText `shouldContain` (header n "Authorization" $ "\"Basic \" + headerAuthorization")
154163
155164 it "should handle complex, custom HTTP headers" $ do
156165 let jsText = genJS $ javascript customHeaderProxy
157166 output jsText
158− parseFromString jsText `shouldSatisfy` isRight
167+ parseFromText jsText `shouldSatisfy` isRight
159168 jsText `shouldContain` "headerXMyLovelyHorse"
160169 jsText `shouldContain` (header n "X-MyLovelyHorse" $ "\"I am good friends with \" + headerXMyLovelyHorse")
161170
162171 it "should handle complex, custom HTTP headers (template replacement)" $ do
163172 let jsText = genJS $ javascript customHeaderProxy2
164173 output jsText
165− parseFromString jsText `shouldSatisfy` isRight
174+ parseFromText jsText `shouldSatisfy` isRight
166175 jsText `shouldContain` "headerXWhatsForDinner"
167176 jsText `shouldContain` (header n "X-WhatsForDinner" $ "\"I would like \" + headerXWhatsForDinner + \" with a cherry on top.\"")
168177
169178 it "can generate the whole javascript code string at once with jsForAPI" $ do
170179 let jsStr = jsForAPI (Proxy :: Proxy TestAPI) (concatMap gen)
171− parseFromString jsStr `shouldSatisfy` isRight
180+ parseFromText jsStr `shouldSatisfy` isRight
172181 where
173− specLabel = "generateJS(" ++ (show n) ++ ")"
182+ specLabel = "generateJS(" <> (show n) <> ")"
174183 output _ = return ()
175184 genJS req = gen req
176− header :: TestNames -> String -> String -> String
185+ header :: TestNames -> Text -> Text -> Text
177186 header v headerName headerValue
178− | v `elem` [Vanilla, VanillaCustom] = "xhr.setRequestHeader(\"" ++ headerName ++ "\", " ++ headerValue ++ ");\n"
179− | otherwise = "headers: { \"" ++ headerName ++ "\": " ++ headerValue ++ " }\n"
187+ | v `elem` [Vanilla, VanillaCustom] = "xhr.setRequestHeader(\"" <> headerName <> "\", " <> headerValue <> ");\n"
188+ | otherwise = "headers: { \"" <> headerName <> "\": " <> headerValue <> " }\n"
servant-js/test/Servant/JSSpec/CustomHeaders.hs+3−1
@@ -5,12 +5,14 @@
55{-# LANGUAGE ScopedTypeVariables #-}
66{-# LANGUAGE TypeFamilies #-}
77{-# LANGUAGE TypeOperators #-}
8+{-# LANGUAGE OverloadedStrings #-}
89
910module Servant.JSSpec.CustomHeaders where
1011
1112import Control.Lens
1213import Data.Monoid
1314import Data.Proxy
15+import Data.Text (pack)
1416import GHC.TypeLits
1517import Servant.JS.Internal
1618
@@ -25,7 +27,7 @@ instance (KnownSymbol sym, HasForeign sublayout)
2527
2628 foreignFor Proxy req = foreignFor (Proxy :: Proxy sublayout) $
2729 req & reqHeaders <>~ [ ReplaceHeaderArg "Authorization" $
28− tokenType (symbolVal (Proxy :: Proxy sym)) ]
30+ tokenType (pack . symbolVal $ (Proxy :: Proxy sym)) ]
2931 where
3032 tokenType t = t <> " {Authorization}"
3133