@@ -27,6 +27,7 @@ main = defaultMain [ testCase "encode RPC error" testEncodeRpcError
27
27
, testCase " invalid JSON" testInvalidJson
28
28
, testCase " invalid JSON RPC" testInvalidJsonRpc
29
29
, testCase " empty batch call" testEmptyBatchCall
30
+ , testCase " invalid batch element" testInvalidBatchElement
30
31
, testCase " wrong version in request" testWrongVersion
31
32
, testCase " method not found" testMethodNotFound
32
33
, testCase " wrong method name capitalization" testWrongMethodNameCapitalization
@@ -53,34 +54,48 @@ testEncodeRpcError = fromByteString (encode err) @?= Just testError
53
54
testError = TestRpcError (- 1 ) " error" Nothing
54
55
55
56
testEncodeErrorWithData :: Assertion
56
- testEncodeErrorWithData = fromByteString (toByteString err) @?= Just testError
57
+ testEncodeErrorWithData = fromByteString (encode err) @?= Just testError
57
58
where err = rpcErrorWithData 1 " my message" errorData
58
59
testError = TestRpcError 1 " my message" $ Just $ toJSON errorData
59
60
errorData = (' \x03BB ' , [True ], () )
60
61
61
62
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 )
63
67
64
68
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 )]
66
73
67
74
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 ]
69
86
70
87
testWrongVersion :: Assertion
71
88
testWrongVersion = checkResponseWithSubtract (encode requestWrongVersion) idNull (- 32600 )
72
89
where requestWrongVersion = Object $ H. insert versionKey (String " 1" ) hm
73
90
Object hm = toJSON $ subtractRequestNamed [(" a1" , Number 4 )] (idNumber 10 )
74
91
75
92
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)
79
95
80
96
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)
84
99
85
100
testMissingRequiredNamedArg :: Assertion
86
101
testMissingRequiredNamedArg = checkResponseWithSubtract (encode request) i (- 32602 )
@@ -182,6 +197,12 @@ subtractRequestNamed args i = TestRequest "subtract 1" (Just $ H.fromList args)
182
197
subtractRequestUnnamed :: [Value ] -> TestId -> TestRequest
183
198
subtractRequestUnnamed args i = TestRequest " subtract 1" (Just args) (Just i)
184
199
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
+
185
206
checkResponseWithSubtract :: B. ByteString -> TestId -> Int -> Assertion
186
207
checkResponseWithSubtract input expectedId expectedCode = do
187
208
rspId <$> res2 @?= Just expectedId
@@ -195,9 +216,6 @@ fromByteString str = case fromJSON <$> decode str of
195
216
Just (Success x) -> Just x
196
217
_ -> Nothing
197
218
198
- toByteString :: ToJSON a => a -> B. ByteString
199
- toByteString = encode . toJSON
200
-
201
219
getErrorCode :: TestResponse -> Maybe Int
202
220
getErrorCode (TestResponse _ (Left (TestRpcError code _ _))) = Just code
203
221
getErrorCode _ = Nothing
@@ -219,3 +237,10 @@ getTimeMethod = toMethod "get_time_seconds" getTime ()
219
237
220
238
getTestTime :: IO Integer
221
239
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