@@ -11,7 +11,6 @@ import Data.Function (on)
11
11
import qualified Data.Aeson as A
12
12
import Data.Aeson ((.=) )
13
13
import qualified Data.Aeson.Types as A
14
- import Data.Text (Text )
15
14
import qualified Data.ByteString.Lazy.Char8 as B
16
15
import qualified Data.HashMap.Strict as H
17
16
import Control.Applicative
@@ -28,43 +27,43 @@ main = defaultMain $ errorHandlingTests ++ otherTests
28
27
29
28
errorHandlingTests :: [Test ]
30
29
errorHandlingTests = [ testCase " invalid JSON" $
31
- assertSubtractResponse (A. String " 5" ) (errRsp A. Null (- 32700 ) )
30
+ assertSubtractResponse (A. String " 5" ) $ nullIdErrRsp (- 32700 )
32
31
33
32
, testCase " invalid JSON RPC" $
34
- assertSubtractResponse (A. object [" id" .= A. Number 10 ]) (errRsp A. Null (- 32600 ) )
33
+ assertSubtractResponse (A. object [" id" .= A. Number 10 ]) $ nullIdErrRsp (- 32600 )
35
34
36
35
, testCase " empty batch call" $
37
- assertSubtractResponse A. emptyArray (errRsp A. Null (- 32600 ) )
36
+ assertSubtractResponse A. emptyArray $ nullIdErrRsp (- 32600 )
38
37
39
38
, testCase " invalid batch element" $
40
- removeErrMsg <$> callSubtractMethods (array [A. Bool True ]) @?= Just (array [errRsp A. Null (- 32600 )])
39
+ removeErrMsg <$> callSubtractMethods (array [A. Bool True ]) @?= Just (array [nullIdErrRsp (- 32600 )])
41
40
42
- , testCase " wrong request version" testWrongVersion
41
+ , testCase " wrong request version" $
42
+ assertSubtractResponse (defaultRq `version` Just " 1.0" ) $ nullIdErrRsp (- 32600 )
43
43
44
- , testCase " wrong id type" testWrongIdType
44
+ , testCase " wrong id type" $
45
+ assertSubtractResponse (defaultRq `id'` (Just $ A. Bool True )) $ nullIdErrRsp (- 32600 )
45
46
46
- , let req = idRequest " add" $ Just defaultArgs
47
- rsp = idErrRsp (- 32601 )
48
- in testCase " method not found" $ assertSubtractResponse req rsp
47
+ , testCase " method not found" $
48
+ assertSubtractResponse (defaultRq `method` " add" ) (defaultIdErrRsp (- 32601 ))
49
49
50
- , let req = idRequest " Subtract" $ Just defaultArgs
51
- rsp = idErrRsp (- 32601 )
52
- in testCase " wrong method name capitalization" $ assertSubtractResponse req rsp
50
+ , testCase " wrong method name capitalization" $
51
+ assertSubtractResponse (defaultRq `method` " Subtract" ) (defaultIdErrRsp (- 32601 ))
53
52
54
53
, testCase " missing required named argument" $
55
- assertInvalidParams " subtract " $ A. object [" a" .= A. Number 1 , " y" .= A. Number 20 ]
54
+ assertInvalidParams $ defaultRq `params` Just ( A. object [" a" .= A. Number 1 , " y" .= A. Number 20 ])
56
55
57
56
, testCase " missing required unnamed argument" $
58
- assertInvalidParams " subtract 2 " $ array [A. Number 0 ]
57
+ assertInvalidParams $ defaultRq `method` " flipped subtract " `params` Just ( array [A. Number 0 ])
59
58
60
59
, testCase " wrong argument type" $
61
- assertInvalidParams " subtract " $ A. object [( " x" , A. Number 1 ), ( " y" , A. String " 2" )]
60
+ assertInvalidParams $ defaultRq `params` Just ( A. object [" x" .= A. Number 1 , " y" .= A. String " 2" ])
62
61
63
62
, testCase " extra unnamed arguments" $
64
- assertInvalidParams " subtract " $ array $ map A. Number [1 , 2 , 3 ]
63
+ assertInvalidParams $ defaultRq `params` Just ( array $ map A. Number [1 , 2 , 3 ])
65
64
66
- , let req = request2_0 Nothing " 12345" $ Just defaultArgs
67
- in testCase " invalid notification" $ callSubtractMethods req @?= ( Nothing :: Maybe A. Value ) ]
65
+ , let req = defaultRq `id'` Nothing `method` " 12345"
66
+ in testCase " invalid notification" $ callSubtractMethods req @?= Nothing ]
68
67
69
68
otherTests :: [Test ]
70
69
otherTests = [ testCase " encode RPC error" $
@@ -79,25 +78,22 @@ otherTests = [ testCase "encode RPC error" $
79
78
, testCase " batch notifications" testBatchNotifications
80
79
, testCase " allow missing version" testAllowMissingVersion
81
80
82
- , testCase " no arguments" $
83
- assertGetTimeResponse Nothing
81
+ , testCase " no arguments" $ assertGetTimeResponse Nothing
84
82
85
- , testCase " empty argument array" $
86
- assertGetTimeResponse $ Just A. emptyArray
83
+ , testCase " empty argument array" $ assertGetTimeResponse $ Just A. emptyArray
87
84
88
- , testCase " empty argument A.object" $
89
- assertGetTimeResponse $ Just A. emptyObject
85
+ , testCase " empty argument A.object" $ assertGetTimeResponse $ Just A. emptyObject
90
86
91
- , let req = subtractRq $ Just $ A. object [" x" .= A. Number 10 , " y" .= A. Number 20 , " z" .= A. String " extra" ]
92
- rsp = idSuccessRsp $ A. Number (- 10 )
87
+ , let req = defaultRq `params` ( Just $ A. object [" x" .= A. Number 10 , " y" .= A. Number 20 , " z" .= A. String " extra" ])
88
+ rsp = defaultRsp `result` A. Number (- 10 )
93
89
in testCase " allow extra named argument" $ assertSubtractResponse req rsp
94
90
95
- , let req = subtractRq $ Just $ A. object [(" x1" , A. Number 500 ), (" x" , A. Number 1000 )]
96
- rsp = idSuccessRsp $ A. Number 1000
91
+ , let req = defaultRq `params` ( Just $ A. object [(" x1" , A. Number 500 ), (" x" , A. Number 1000 )])
92
+ rsp = defaultRsp `result` A. Number 1000
97
93
in testCase " use default named argument" $ assertSubtractResponse req rsp
98
94
99
- , let req = subtractRq $ Just $ array [A. Number 4 ]
100
- rsp = idSuccessRsp $ A. Number 4
95
+ , let req = defaultRq `params` ( Just $ array [A. Number 4 ])
96
+ rsp = defaultRsp `result` A. Number 4
101
97
in testCase " use default unnamed argument" $ assertSubtractResponse req rsp
102
98
103
99
, testCase " string request ID" $ assertEqualId $ A. String " ID 5"
@@ -111,32 +107,18 @@ assertSubtractResponse rq expectedRsp = removeErrMsg <$> rsp @?= Just expectedRs
111
107
where rsp = callSubtractMethods rq
112
108
113
109
assertEqualId :: A. Value -> Assertion
114
- assertEqualId i = let req = request2_0 (Just i) " subtract" (Just defaultArgs)
115
- rsp = successRsp i defaultResult
116
- in assertSubtractResponse req rsp
117
-
118
- assertInvalidParams :: Text -> A. Value -> Assertion
119
- assertInvalidParams name args = let req = idRequest name $ Just args
120
- rsp = idErrRsp (- 32602 )
121
- in assertSubtractResponse req rsp
122
-
123
- testWrongVersion :: Assertion
124
- testWrongVersion = removeErrMsg <$> rsp @?= Just (errRsp A. Null (- 32600 ))
125
- where rsp = callSubtractMethods $ request ver (Just defaultId) (A. String " subtract" ) args
126
- ver = Just " 1.0"
127
- args = Just $ A. object [" x" .= A. Number 4 ]
128
-
129
- testWrongIdType :: Assertion
130
- testWrongIdType = removeErrMsg <$> rsp @?= Just (errRsp A. Null (- 32600 ))
131
- where rsp = callSubtractMethods $ request2_0 (Just $ A. Bool True ) " subtract" args
132
- args = Just $ A. object [" x" .= A. Number 4 ]
110
+ assertEqualId i = assertSubtractResponse (defaultRq `id'` Just i) (defaultRsp `id'` Just i)
111
+
112
+ assertInvalidParams :: A. Value -> Assertion
113
+ assertInvalidParams req = assertSubtractResponse req (defaultIdErrRsp (- 32602 ))
133
114
134
115
testBatch :: Assertion
135
116
testBatch = sortBy (compare `on` idToString) <$> response @?= Just expected
136
- where expected = [successRsp i1 (A. Number 2 ), successRsp i2 (A. Number 4 )]
137
- response :: Maybe [A. Value ]
138
- response = A. decode =<< runIdentity (call (toMethods [subtractMethod]) $ A. encode rq)
139
- rq = [request2_0 (Just i1) " subtract" (toArgs 10 8 ), request2_0 (Just i2) " subtract" (toArgs 24 20 )]
117
+ where expected = [rsp i1 2 , rsp i2 4 ]
118
+ where rsp i x = defaultRsp `id'` Just i `result` A. Number x
119
+ response = A. decode =<< runIdentity (call (toMethods [subtractMethod]) $ A. encode requests)
120
+ requests = [rq i1 10 8 , rq i2 24 20 ]
121
+ where rq i x y = defaultRq `id'` Just i `params` toArgs x y
140
122
toArgs :: Int -> Int -> Maybe A. Value
141
123
toArgs x y = Just $ A. object [" x" .= x, " y" .= y]
142
124
i1 = A. Number 1
@@ -148,13 +130,11 @@ testBatch = sortBy (compare `on` idToString) <$> response @?= Just expected
148
130
testBatchNotifications :: Assertion
149
131
testBatchNotifications = runState response 0 @?= (Nothing , 10 )
150
132
where response = call (toMethods [incrementStateMethod]) $ A. encode rq
151
- rq = replicate 10 $ request2_0 Nothing " increment" Nothing
133
+ rq = replicate 10 $ request Nothing " increment" Nothing
152
134
153
135
testAllowMissingVersion :: Assertion
154
- testAllowMissingVersion = (fromByteString =<< runIdentity response) @?= (Just $ idSuccessRsp (A. Number 1 ))
155
- where response = call (toMethods [subtractMethod]) $ A. encode requestNoVersion
156
- requestNoVersion = request Nothing (Just defaultId) " subtract" args
157
- args = Just $ A. object [" x" .= A. Number 1 ]
136
+ testAllowMissingVersion = callSubtractMethods requestNoVersion @?= (Just $ defaultRsp `result` A. Number 1 )
137
+ where requestNoVersion = defaultRq `version` Nothing `params` Just (A. object [" x" .= A. Number 1 ])
158
138
159
139
incrementStateMethod :: Method (State Int )
160
140
incrementStateMethod = toMethod " increment" f ()
@@ -164,8 +144,8 @@ incrementStateMethod = toMethod "increment" f ()
164
144
assertGetTimeResponse :: Maybe A. Value -> Assertion
165
145
assertGetTimeResponse args = passed @? " unexpected RPC response"
166
146
where passed = (expected == ) <$> rsp
167
- expected = Just $ idSuccessRsp ( A. Number 100 )
168
- req = idRequest " get_time_seconds" args
147
+ expected = Just $ defaultRsp `result` A. Number 100
148
+ req = defaultRq `method` " get_time_seconds" `params` args
169
149
rsp = callGetTimeMethod req
170
150
171
151
callSubtractMethods :: A. Value -> Maybe A. Value
@@ -189,8 +169,8 @@ subtractMethod :: Method Identity
189
169
subtractMethod = toMethod " subtract" subtract (Required " x" :+: Optional " y" 0 :+: () )
190
170
191
171
flippedSubtractMethod :: Method Identity
192
- flippedSubtractMethod = toMethod " subtract 2 " (flip subtract ) params
193
- where params = Optional " y" (- 1000 ) :+: Required " x" :+: ()
172
+ flippedSubtractMethod = toMethod " flipped subtract " (flip subtract ) ps
173
+ where ps = Optional " y" (- 1000 ) :+: Required " x" :+: ()
194
174
195
175
subtract :: Int -> Int -> RpcResult Identity Int
196
176
subtract x y = return (x - y)
@@ -206,12 +186,3 @@ removeErrMsg (A.Object rsp) = A.Object $ H.adjust removeMsg errKey rsp
206
186
removeMsg v = v
207
187
removeErrMsg (A. Array rsps) = A. Array $ removeErrMsg <$> rsps
208
188
removeErrMsg v = v
209
-
210
- defaultArgs :: A. Value
211
- defaultArgs = array $ map A. Number [1 , 2 ]
212
-
213
- defaultResult :: A. Value
214
- defaultResult = A. Number (- 1 )
215
-
216
- subtractRq :: Maybe A. Value -> A. Value
217
- subtractRq = idRequest " subtract"
0 commit comments