@@ -60,47 +60,38 @@ testEncodeErrorWithData = fromByteString (encode err) @?= Just testError
60
60
errorData = (' \x03BB ' , [True ], () )
61
61
62
62
testInvalidJson :: Assertion
63
- testInvalidJson = do
64
- (rspToErrCode =<< rsp) @?= Just (- 32700 )
65
- rspId <$> rsp @?= Just idNull
63
+ testInvalidJson = removeErrMsg <$> rsp @?= Just (errResponse idNull (- 32700 ))
66
64
where rsp = callSubtractMethods (" 5" :: String )
67
65
68
66
testInvalidJsonRpc :: Assertion
69
- testInvalidJsonRpc = do
70
- (rspToErrCode =<< rsp) @?= Just (- 32600 )
71
- rspId <$> rsp @?= Just idNull
67
+ testInvalidJsonRpc = removeErrMsg <$> rsp @?= Just (errResponse idNull (- 32600 ))
72
68
where rsp = callSubtractMethods $ object [" id" .= (10 :: Int )]
73
69
74
70
testEmptyBatchCall :: Assertion
75
- testEmptyBatchCall = do
76
- (rspToErrCode =<< rsp) @?= Just (- 32600 )
77
- rspId <$> rsp @?= Just idNull
71
+ testEmptyBatchCall = removeErrMsg <$> rsp @?= Just (errResponse idNull (- 32600 ))
78
72
where rsp = callSubtractMethods emptyArray
79
73
80
74
testInvalidBatchElement :: Assertion
81
- testInvalidBatchElement = do
82
- length <$> rsp @?= Just 1
83
- (rspToErrCode . head =<< rsp) @?= Just (- 32600 )
84
- rspId . head <$> rsp @?= Just idNull
75
+ testInvalidBatchElement = map removeErrMsg <$> rsp @?= Just [errResponse idNull (- 32600 )]
85
76
where rsp = callSubtractMethods [True ]
86
77
87
78
testWrongVersion :: Assertion
88
- testWrongVersion = checkResponseWithSubtract (encode requestWrongVersion) idNull (- 32600 )
89
- where requestWrongVersion = Object $ H. insert versionKey (String " 1" ) hm
90
- Object hm = toJSON $ subtractRequestNamed [(" a1 " , Number 4 )] (idNumber 10 )
79
+ testWrongVersion = removeErrMsg <$> rsp @?= Just (errResponse idNull (- 32600 ) )
80
+ where rsp = callSubtractMethods $ Object $ H. insert versionKey (String " 1" ) hm
81
+ Object hm = toJSON $ subtractRequestNamed [(" x " , Number 4 )] defaultId
91
82
92
83
testMethodNotFound :: Assertion
93
- testMethodNotFound = (rspToErrCode =<< callSubtractMethods req) @?= Just (- 32601 )
94
- where req = TestRequest " ad" (Just [ 1 , 2 :: Int ] ) (Just defaultId)
84
+ testMethodNotFound = removeErrMsg <$> rsp @?= Just (errResponse defaultId ( - 32601 ) )
85
+ where rsp = callSubtractMethods $ TestRequest " ad" (Just defaultArgs ) (Just defaultId)
95
86
96
87
testWrongMethodNameCapitalization :: Assertion
97
- testWrongMethodNameCapitalization = (rspToErrCode =<< callSubtractMethods req) @?= Just (- 32601 )
98
- where req = TestRequest " Add" (Just [ Number 1 , Number 2 ] ) (Just defaultId)
88
+ testWrongMethodNameCapitalization = removeErrMsg <$> rsp @?= Just (errResponse defaultId ( - 32601 ) )
89
+ where rsp = callSubtractMethods $ TestRequest " Add" (Just defaultArgs ) (Just defaultId)
99
90
100
91
testMissingRequiredNamedArg :: Assertion
101
- testMissingRequiredNamedArg = checkResponseWithSubtract (encode request) i ( - 32602 )
102
- where request = subtractRequestNamed [( " A1 " , Number 1 ), ( " a2 " , Number 20 )] i
103
- i = idNumber 2
92
+ testMissingRequiredNamedArg = removeErrMsg <$> rsp @?= Just (errResponse defaultId ( - 32602 ) )
93
+ where rsp = callSubtractMethods $ subtractRequestNamed args defaultId
94
+ args = [( " X " , Number 1 ), ( " y " , Number 20 )]
104
95
105
96
testMissingRequiredUnnamedArg :: Assertion
106
97
testMissingRequiredUnnamedArg = checkResponseWithSubtract (encode request) i (- 32602 )
@@ -109,7 +100,7 @@ testMissingRequiredUnnamedArg = checkResponseWithSubtract (encode request) i (-3
109
100
110
101
testWrongArgType :: Assertion
111
102
testWrongArgType = checkResponseWithSubtract (encode request) i (- 32602 )
112
- where request = subtractRequestNamed [(" a1 " , Number 1 ), (" a2 " , Bool True )] i
103
+ where request = subtractRequestNamed [(" x " , Number 1 ), (" y " , Bool True )] i
113
104
i = idString " ABC"
114
105
115
106
testDisallowExtraUnnamedArg :: Assertion
@@ -127,7 +118,7 @@ testBatch = sortBy (compare `on` fromIntId) (fromJust (fromByteString =<< runIde
127
118
where expected = [TestResponse i1 (Right $ Number 2 ), TestResponse i2 (Right $ Number 4 )]
128
119
response = call (toMethods [subtractMethod]) $ encode request
129
120
request = [subtractRequestNamed (toArgs 10 8 ) i1, subtractRequestNamed (toArgs 24 20 ) i2]
130
- toArgs x y = [(" a1 " , Number x), (" a2 " , Number y)]
121
+ toArgs x y = [(" x " , Number x), (" y " , Number y)]
131
122
i1 = idNumber 1
132
123
i2 = idNumber 2
133
124
fromIntId rsp = (fromNumId $ rspId rsp) :: Maybe Int
@@ -140,22 +131,22 @@ testBatchNotifications = runState response 0 @?= (Nothing, 10)
140
131
testAllowMissingVersion :: Assertion
141
132
testAllowMissingVersion = (fromByteString =<< runIdentity response) @?= (Just $ TestResponse i (Right $ Number 1 ))
142
133
where requestNoVersion = Object $ H. delete versionKey hm
143
- Object hm = toJSON $ subtractRequestNamed [(" a1 " , Number 1 )] i
134
+ Object hm = toJSON $ subtractRequestNamed [(" x " , Number 1 )] i
144
135
response = call (toMethods [subtractMethod]) $ encode requestNoVersion
145
136
i = idNumber (- 1 )
146
137
147
138
testAllowExtraNamedArg :: Assertion
148
139
testAllowExtraNamedArg = (fromByteString =<< runIdentity response) @?= (Just $ TestResponse i (Right $ Number (- 10 )))
149
140
where response = call (toMethods [subtractMethod]) $ encode request
150
141
request = subtractRequestNamed args i
151
- args = [(" a1 " , Number 10 ), (" a2 " , Number 20 ), (" a3 " , String " extra" )]
142
+ args = [(" x " , Number 10 ), (" y " , Number 20 ), (" z " , String " extra" )]
152
143
i = idString " ID"
153
144
154
145
testDefaultNamedArg :: Assertion
155
146
testDefaultNamedArg = (fromByteString =<< runIdentity response) @?= (Just $ TestResponse i (Right $ Number 1000 ))
156
147
where response = call (toMethods [subtractMethod]) $ encode request
157
148
request = subtractRequestNamed args i
158
- args = [(" a " , Number 500 ), (" a1 " , Number 1000 )]
149
+ args = [(" x1 " , Number 500 ), (" x " , Number 1000 )]
159
150
i = idNumber 3
160
151
161
152
testDefaultUnnamedArg :: Assertion
@@ -168,7 +159,7 @@ testNullId :: Assertion
168
159
testNullId = (fromByteString =<< runIdentity response) @?= (Just $ TestResponse idNull (Right $ Number (- 80 )))
169
160
where response = call (toMethods [subtractMethod]) $ encode request
170
161
request = subtractRequestNamed args idNull
171
- args = [(" a2 " , Number 70 ), (" a1 " , Number (- 10 ))]
162
+ args = [(" y " , Number 70 ), (" x " , Number (- 10 ))]
172
163
173
164
testNoArgs :: Assertion
174
165
testNoArgs = compareGetTimeResult Nothing
@@ -221,7 +212,7 @@ getErrorCode (TestResponse _ (Left (TestRpcError code _ _))) = Just code
221
212
getErrorCode _ = Nothing
222
213
223
214
subtractMethod :: Method Identity
224
- subtractMethod = toMethod " subtract 1" sub (Required " a1 " :+: Optional " a2 " 0 :+: () )
215
+ subtractMethod = toMethod " subtract 1" sub (Required " x " :+: Optional " y " 0 :+: () )
225
216
where sub :: Int -> Int -> RpcResult Identity Int
226
217
sub x y = return (x - y)
227
218
@@ -238,9 +229,16 @@ getTimeMethod = toMethod "get_time_seconds" getTime ()
238
229
getTestTime :: IO Integer
239
230
getTestTime = return 100
240
231
241
- rspToErrCode :: TestResponse -> Maybe Int
242
- rspToErrCode (TestResponse _ (Left (TestRpcError code _ _))) = Just code
243
- rspToErrCode _ = Nothing
232
+ removeErrMsg :: TestResponse -> TestResponse
233
+ removeErrMsg (TestResponse i (Left (TestRpcError code _ _)))
234
+ = TestResponse i (Left (TestRpcError code " " Nothing ))
235
+ removeErrMsg rsp = rsp
236
+
237
+ errResponse :: TestId -> Int -> TestResponse
238
+ errResponse i code = TestResponse i (Left (TestRpcError code " " Nothing ))
244
239
245
240
defaultId :: TestId
246
241
defaultId = idNumber 3
242
+
243
+ defaultArgs :: [Int ]
244
+ defaultArgs = [1 , 2 ]
0 commit comments