@@ -23,18 +23,46 @@ import Test.Framework.Providers.HUnit
23
23
24
24
main :: IO ()
25
25
main = defaultMain [ testCase " encode RPC error" testEncodeRpcError
26
+
26
27
, testCase " encode error with data" testEncodeErrorWithData
27
- , testCase " invalid JSON" testInvalidJson
28
- , testCase " invalid JSON RPC" testInvalidJsonRpc
29
- , testCase " empty batch call" testEmptyBatchCall
28
+
29
+ , testCase " invalid JSON" $
30
+ assertSubtractResponse (" 5" :: String ) (errResponse idNull (- 32700 ))
31
+
32
+ , testCase " invalid JSON RPC" $
33
+ assertSubtractResponse (object [" id" .= (10 :: Int )]) (errResponse idNull (- 32600 ))
34
+
35
+ , testCase " empty batch call" $
36
+ assertSubtractResponse emptyArray (errResponse idNull (- 32600 ))
37
+
30
38
, testCase " invalid batch element" testInvalidBatchElement
39
+
31
40
, testCase " wrong version in request" testWrongVersion
32
- , testCase " method not found" testMethodNotFound
33
- , testCase " wrong method name capitalization" testWrongMethodNameCapitalization
34
- , testCase " missing required named argument" testMissingRequiredNamedArg
35
- , testCase " missing required unnamed argument" testMissingRequiredUnnamedArg
36
- , testCase " wrong argument type" testWrongArgType
37
- , testCase " disallow extra unnamed arguments" testDisallowExtraUnnamedArg
41
+
42
+ , let req = TestRequest " add" (Just defaultArgs) (Just nonNullId)
43
+ rsp = errResponse nonNullId (- 32601 )
44
+ in testCase " method not found" $ assertSubtractResponse req rsp
45
+
46
+ , let req = TestRequest " Subtract" (Just defaultArgs) (Just nonNullId)
47
+ rsp = errResponse nonNullId (- 32601 )
48
+ in testCase " wrong method name capitalization" $ assertSubtractResponse req rsp
49
+
50
+ , let req = subtractRequestNamed [(" X" , Number 1 ), (" y" , Number 20 )] nonNullId
51
+ rsp = errResponse nonNullId (- 32602 )
52
+ in testCase " missing required named argument" $ assertSubtractResponse req rsp
53
+
54
+ , let req = TestRequest " subtract 2" (Just [Number 0 ]) (Just nonNullId)
55
+ rsp = errResponse nonNullId (- 32602 )
56
+ in testCase " missing required unnamed argument" $ assertSubtractResponse req rsp
57
+
58
+ , let req = subtractRequestNamed [(" x" , Number 1 ), (" y" , String " 2" )] nonNullId
59
+ rsp = errResponse nonNullId (- 32602 )
60
+ in testCase " wrong argument type" $ assertSubtractResponse req rsp
61
+
62
+ , let req = subtractRequestUnnamed (map Number [1 , 2 , 3 ]) nonNullId
63
+ rsp = errResponse nonNullId (- 32602 )
64
+ in testCase " disallow extra unnamed arguments" $ assertSubtractResponse req rsp
65
+
38
66
, testCase " invalid notification" testNoResponseToInvalidNotification
39
67
, testCase " batch request" testBatch
40
68
, testCase " batch notifications" testBatchNotifications
@@ -59,17 +87,9 @@ testEncodeErrorWithData = fromByteString (encode err) @?= Just testError
59
87
testError = TestRpcError 1 " my message" $ Just $ toJSON errorData
60
88
errorData = (' \x03BB ' , [True ], () )
61
89
62
- testInvalidJson :: Assertion
63
- testInvalidJson = removeErrMsg <$> rsp @?= Just (errResponse idNull (- 32700 ))
64
- where rsp = callSubtractMethods (" 5" :: String )
65
-
66
- testInvalidJsonRpc :: Assertion
67
- testInvalidJsonRpc = removeErrMsg <$> rsp @?= Just (errResponse idNull (- 32600 ))
68
- where rsp = callSubtractMethods $ object [" id" .= (10 :: Int )]
69
-
70
- testEmptyBatchCall :: Assertion
71
- testEmptyBatchCall = removeErrMsg <$> rsp @?= Just (errResponse idNull (- 32600 ))
72
- where rsp = callSubtractMethods emptyArray
90
+ assertSubtractResponse :: ToJSON a => a -> TestResponse -> Assertion
91
+ assertSubtractResponse request expectedRsp = removeErrMsg <$> rsp @?= Just expectedRsp
92
+ where rsp = callSubtractMethods request
73
93
74
94
testInvalidBatchElement :: Assertion
75
95
testInvalidBatchElement = map removeErrMsg <$> rsp @?= Just [errResponse idNull (- 32600 )]
@@ -78,35 +98,7 @@ testInvalidBatchElement = map removeErrMsg <$> rsp @?= Just [errResponse idNull
78
98
testWrongVersion :: Assertion
79
99
testWrongVersion = removeErrMsg <$> rsp @?= Just (errResponse idNull (- 32600 ))
80
100
where rsp = callSubtractMethods $ Object $ H. insert versionKey (String " 1" ) hm
81
- Object hm = toJSON $ subtractRequestNamed [(" x" , Number 4 )] defaultId
82
-
83
- testMethodNotFound :: Assertion
84
- testMethodNotFound = removeErrMsg <$> rsp @?= Just (errResponse defaultId (- 32601 ))
85
- where rsp = callSubtractMethods $ TestRequest " ad" (Just defaultArgs) (Just defaultId)
86
-
87
- testWrongMethodNameCapitalization :: Assertion
88
- testWrongMethodNameCapitalization = removeErrMsg <$> rsp @?= Just (errResponse defaultId (- 32601 ))
89
- where rsp = callSubtractMethods $ TestRequest " Add" (Just defaultArgs) (Just defaultId)
90
-
91
- testMissingRequiredNamedArg :: Assertion
92
- testMissingRequiredNamedArg = removeErrMsg <$> rsp @?= Just (errResponse defaultId (- 32602 ))
93
- where rsp = callSubtractMethods $ subtractRequestNamed args defaultId
94
- args = [(" X" , Number 1 ), (" y" , Number 20 )]
95
-
96
- testMissingRequiredUnnamedArg :: Assertion
97
- testMissingRequiredUnnamedArg = checkResponseWithSubtract (encode request) i (- 32602 )
98
- where request = TestRequest " subtract 2" (Just [Number 0 ]) (Just i)
99
- i = idString " "
100
-
101
- testWrongArgType :: Assertion
102
- testWrongArgType = checkResponseWithSubtract (encode request) i (- 32602 )
103
- where request = subtractRequestNamed [(" x" , Number 1 ), (" y" , Bool True )] i
104
- i = idString " ABC"
105
-
106
- testDisallowExtraUnnamedArg :: Assertion
107
- testDisallowExtraUnnamedArg = checkResponseWithSubtract (encode request) i (- 32602 )
108
- where request = subtractRequestUnnamed (map Number [1 , 2 , 3 ]) i
109
- i = idString " i"
101
+ Object hm = toJSON $ subtractRequestNamed [(" x" , Number 4 )] nonNullId
110
102
111
103
testNoResponseToInvalidNotification :: Assertion
112
104
testNoResponseToInvalidNotification = runIdentity response @?= Nothing
@@ -194,23 +186,11 @@ callSubtractMethods req = let methods :: Methods Identity
194
186
rsp = call methods $ encode req
195
187
in fromByteString =<< runIdentity rsp
196
188
197
- checkResponseWithSubtract :: B. ByteString -> TestId -> Int -> Assertion
198
- checkResponseWithSubtract input expectedId expectedCode = do
199
- rspId <$> res2 @?= Just expectedId
200
- (getErrorCode =<< res2) @?= Just expectedCode
201
- where res1 :: Identity (Maybe B. ByteString )
202
- res1 = call (toMethods [subtractMethod, flippedSubtractMethod]) input
203
- res2 = fromByteString =<< runIdentity res1
204
-
205
189
fromByteString :: FromJSON a => B. ByteString -> Maybe a
206
190
fromByteString str = case fromJSON <$> decode str of
207
191
Just (Success x) -> Just x
208
192
_ -> Nothing
209
193
210
- getErrorCode :: TestResponse -> Maybe Int
211
- getErrorCode (TestResponse _ (Left (TestRpcError code _ _))) = Just code
212
- getErrorCode _ = Nothing
213
-
214
194
subtractMethod :: Method Identity
215
195
subtractMethod = toMethod " subtract 1" sub (Required " x" :+: Optional " y" 0 :+: () )
216
196
where sub :: Int -> Int -> RpcResult Identity Int
@@ -237,8 +217,8 @@ removeErrMsg rsp = rsp
237
217
errResponse :: TestId -> Int -> TestResponse
238
218
errResponse i code = TestResponse i (Left (TestRpcError code " " Nothing ))
239
219
240
- defaultId :: TestId
241
- defaultId = idNumber 3
220
+ nonNullId :: TestId
221
+ nonNullId = idNumber 3
242
222
243
223
defaultArgs :: [Int ]
244
224
defaultArgs = [1 , 2 ]
0 commit comments