Skip to content

Commit 566ceed

Browse files
committed
Improved error code tests
1 parent 8919432 commit 566ceed

File tree

3 files changed

+48
-29
lines changed

3 files changed

+48
-29
lines changed

json-rpc-server.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,4 @@ test-suite tests
6868
text >=0.11 && <1.2,
6969
vector >=0.8 && <0.11,
7070
unordered-containers >=0.1 && <0.3
71-
ghc-options: -Wall -fno-warn-incomplete-patterns
71+
ghc-options: -Wall

tests/TestParallelism.hs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,23 @@ import Data.List (sort)
88
import qualified Data.Aeson as A
99
import Data.Maybe (fromJust)
1010
import qualified Data.HashMap.Strict as H
11-
import qualified Data.ByteString.Lazy.Char8 as B
1211
import Control.Applicative ((<$>))
1312
import Control.Monad.Trans (liftIO)
1413
import Control.Concurrent (forkIO)
1514
import Control.Concurrent.MVar (MVar, newEmptyMVar, putMVar, takeMVar)
1615
import Test.HUnit ((@?=), Assertion)
1716

18-
-- | Tests sending a batch request where each request either
17+
-- | Tests parallelizing a batch request. Each request either
1918
-- locks or unlocks an MVar. The MVar is initially locked,
20-
-- so the first lock request would not succeed if the
21-
-- requests were serialized.
19+
-- so the first lock request cannot succeed if the requests
20+
-- are serialized.
2221
testParallelizingTasks :: Assertion
2322
testParallelizingTasks = do
2423
methods <- createMethods <$> newEmptyMVar
2524
output <- callWithBatchStrategy parallelize methods input
26-
let result = fromJust $ fromByteString =<< output
27-
sort (map rspToIntId result) @?= map Just [1..3]
28-
sort (map rspToCharVal result) @?= map Just "ABC"
25+
let rsp = fromJust $ fromJson =<< A.decode =<< output
26+
sort (map rspToIntId rsp) @?= map Just [1..3]
27+
sort (map rspToCharResult rsp) @?= map Just "ABC"
2928
where input = A.encode [ lockRequest 1
3029
, lockRequest 3
3130
, unlockRequest 'C'
@@ -37,9 +36,9 @@ testParallelizingTasks = do
3736
rspToIntId :: TestResponse -> Maybe Int
3837
rspToIntId = fromNumId . rspId
3938

40-
rspToCharVal :: TestResponse -> Maybe Char
41-
rspToCharVal resp = let (Right r) = rspResult resp
42-
in fromJust $ fromJson r
39+
rspToCharResult :: TestResponse -> Maybe Char
40+
rspToCharResult rsp = let (Right r) = rspResult rsp
41+
in fromJust $ fromJson r
4342

4443
lockRequest :: Int -> TestRequest
4544
lockRequest i = TestRequest "lock" (Nothing :: Maybe ()) (Just $ idNumber i)
@@ -63,8 +62,3 @@ parallelize tasks = mapM takeMVar =<< mapM fork tasks
6362
mvar <- newEmptyMVar
6463
_ <- forkIO $ putMVar mvar =<< t
6564
return mvar
66-
67-
fromByteString :: A.FromJSON a => B.ByteString -> Maybe a
68-
fromByteString str = case A.fromJSON <$> A.decode str of
69-
Just (A.Success x) -> Just x
70-
_ -> Nothing

tests/TestSuite.hs

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ main = defaultMain [ testCase "encode RPC error" testEncodeRpcError
2727
, testCase "invalid JSON" testInvalidJson
2828
, testCase "invalid JSON RPC" testInvalidJsonRpc
2929
, testCase "empty batch call" testEmptyBatchCall
30+
, testCase "invalid batch element" testInvalidBatchElement
3031
, testCase "wrong version in request" testWrongVersion
3132
, testCase "method not found" testMethodNotFound
3233
, testCase "wrong method name capitalization" testWrongMethodNameCapitalization
@@ -53,34 +54,48 @@ testEncodeRpcError = fromByteString (encode err) @?= Just testError
5354
testError = TestRpcError (-1) "error" Nothing
5455

5556
testEncodeErrorWithData :: Assertion
56-
testEncodeErrorWithData = fromByteString (toByteString err) @?= Just testError
57+
testEncodeErrorWithData = fromByteString (encode err) @?= Just testError
5758
where err = rpcErrorWithData 1 "my message" errorData
5859
testError = TestRpcError 1 "my message" $ Just $ toJSON errorData
5960
errorData = ('\x03BB', [True], ())
6061

6162
testInvalidJson :: Assertion
62-
testInvalidJson = checkResponseWithSubtract "5" idNull (-32700)
63+
testInvalidJson = do
64+
(rspToErrCode =<< rsp) @?= Just (-32700)
65+
rspId <$> rsp @?= Just idNull
66+
where rsp = callSubtractMethods ("5" :: String)
6367

6468
testInvalidJsonRpc :: Assertion
65-
testInvalidJsonRpc = checkResponseWithSubtract (encode $ object ["id" .= (10 :: Int)]) idNull (-32600)
69+
testInvalidJsonRpc = do
70+
(rspToErrCode =<< rsp) @?= Just (-32600)
71+
rspId <$> rsp @?= Just idNull
72+
where rsp = callSubtractMethods $ object ["id" .= (10 :: Int)]
6673

6774
testEmptyBatchCall :: Assertion
68-
testEmptyBatchCall = checkResponseWithSubtract (encode emptyArray) idNull (-32600)
75+
testEmptyBatchCall = do
76+
(rspToErrCode =<< rsp) @?= Just (-32600)
77+
rspId <$> rsp @?= Just idNull
78+
where rsp = callSubtractMethods emptyArray
79+
80+
testInvalidBatchElement :: Assertion
81+
testInvalidBatchElement = do
82+
length <$> rsp @?= Just 1
83+
(rspToErrCode . head =<< rsp) @?= Just (-32600)
84+
rspId . head <$> rsp @?= Just idNull
85+
where rsp = callSubtractMethods [True]
6986

7087
testWrongVersion :: Assertion
7188
testWrongVersion = checkResponseWithSubtract (encode requestWrongVersion) idNull (-32600)
7289
where requestWrongVersion = Object $ H.insert versionKey (String "1") hm
7390
Object hm = toJSON $ subtractRequestNamed [("a1", Number 4)] (idNumber 10)
7491

7592
testMethodNotFound :: Assertion
76-
testMethodNotFound = checkResponseWithSubtract (encode request) i (-32601)
77-
where request = TestRequest "ad" (Just [Number 1, Number 2]) (Just i)
78-
i = idNumber 3
93+
testMethodNotFound = (rspToErrCode =<< callSubtractMethods req) @?= Just (-32601)
94+
where req = TestRequest "ad" (Just [1, 2 :: Int]) (Just defaultId)
7995

8096
testWrongMethodNameCapitalization :: Assertion
81-
testWrongMethodNameCapitalization = checkResponseWithSubtract (encode request) i (-32601)
82-
where request = TestRequest "Add" (Just [Number 1, Number 2]) (Just i)
83-
i = idNull
97+
testWrongMethodNameCapitalization = (rspToErrCode =<< callSubtractMethods req) @?= Just (-32601)
98+
where req = TestRequest "Add" (Just [Number 1, Number 2]) (Just defaultId)
8499

85100
testMissingRequiredNamedArg :: Assertion
86101
testMissingRequiredNamedArg = checkResponseWithSubtract (encode request) i (-32602)
@@ -182,6 +197,12 @@ subtractRequestNamed args i = TestRequest "subtract 1" (Just $ H.fromList args)
182197
subtractRequestUnnamed :: [Value] -> TestId -> TestRequest
183198
subtractRequestUnnamed args i = TestRequest "subtract 1" (Just args) (Just i)
184199

200+
callSubtractMethods :: (ToJSON a, FromJSON b) => a -> Maybe b
201+
callSubtractMethods req = let methods :: Methods Identity
202+
methods = toMethods [subtractMethod, flippedSubtractMethod]
203+
rsp = call methods $ encode req
204+
in fromByteString =<< runIdentity rsp
205+
185206
checkResponseWithSubtract :: B.ByteString -> TestId -> Int -> Assertion
186207
checkResponseWithSubtract input expectedId expectedCode = do
187208
rspId <$> res2 @?= Just expectedId
@@ -195,9 +216,6 @@ fromByteString str = case fromJSON <$> decode str of
195216
Just (Success x) -> Just x
196217
_ -> Nothing
197218

198-
toByteString :: ToJSON a => a -> B.ByteString
199-
toByteString = encode . toJSON
200-
201219
getErrorCode :: TestResponse -> Maybe Int
202220
getErrorCode (TestResponse _ (Left (TestRpcError code _ _))) = Just code
203221
getErrorCode _ = Nothing
@@ -219,3 +237,10 @@ getTimeMethod = toMethod "get_time_seconds" getTime ()
219237

220238
getTestTime :: IO Integer
221239
getTestTime = return 100
240+
241+
rspToErrCode :: TestResponse -> Maybe Int
242+
rspToErrCode (TestResponse _ (Left (TestRpcError code _ _))) = Just code
243+
rspToErrCode _ = Nothing
244+
245+
defaultId :: TestId
246+
defaultId = idNumber 3

0 commit comments

Comments
 (0)