2
2
3
3
module Main (main ) where
4
4
5
- import Network.JsonRpc.Server
6
- import Internal
5
+ import qualified Network.JsonRpc.Server as S
6
+ import Network.JsonRpc.Server ((:+:) (.. ))
7
+ import Internal ( request , defaultRq , defaultRsp
8
+ , defaultIdErrRsp , nullIdErrRsp
9
+ , version , result , rpcErr , method
10
+ , params , id' , array , rspToIdString )
7
11
import qualified TestParallelism
8
- import Data.Maybe
9
12
import Data.List (sortBy )
13
+ import qualified Data.Vector as V
10
14
import Data.Function (on )
11
15
import qualified Data.Aeson as A
12
16
import Data.Aeson ((.=) )
13
17
import qualified Data.Aeson.Types as A
14
- import qualified Data.ByteString.Lazy.Char8 as B
15
18
import qualified Data.HashMap.Strict as H
16
- import Control.Applicative
17
- import Control.Monad.Trans
18
- import Control.Monad.State
19
- import Control.Monad.Identity
19
+ import Control.Applicative ( (<$>) )
20
+ import Control.Monad.Trans ( liftIO )
21
+ import Control.Monad.State ( State , runState , lift , modify )
22
+ import Control.Monad.Identity ( Identity , runIdentity )
20
23
import Test.HUnit hiding (State , Test )
21
- import Test.Framework
22
- import Test.Framework.Providers.HUnit
24
+ import Test.Framework ( defaultMain , Test )
25
+ import Test.Framework.Providers.HUnit ( testCase )
23
26
import Prelude hiding (subtract )
24
27
25
28
main :: IO ()
@@ -67,15 +70,17 @@ errorHandlingTests = [ testCase "invalid JSON" $
67
70
68
71
otherTests :: [Test ]
69
72
otherTests = [ testCase " encode RPC error" $
70
- A. toJSON (rpcError (- 1 ) " error" ) @?= rpcErr Nothing (- 1 ) " error"
73
+ A. toJSON (S. rpcError (- 1 ) " error" ) @?= rpcErr Nothing (- 1 ) " error"
71
74
72
- , let err = rpcErrorWithData 1 " my message" errData
75
+ , let err = S. rpcErrorWithData 1 " my message" errData
73
76
testError = rpcErr (Just $ A. toJSON errData) 1 " my message"
74
77
errData = (' \x03BB ' , [True ], () )
75
78
in testCase " encode RPC error with data" $ A. toJSON err @?= testError
76
79
77
80
, testCase " batch request" testBatch
81
+
78
82
, testCase " batch notifications" testBatchNotifications
83
+
79
84
, testCase " allow missing version" testAllowMissingVersion
80
85
81
86
, testCase " no arguments" $ assertGetTimeResponse Nothing
@@ -84,7 +89,8 @@ otherTests = [ testCase "encode RPC error" $
84
89
85
90
, testCase " empty argument A.object" $ assertGetTimeResponse $ Just A. emptyObject
86
91
87
- , let req = defaultRq `params` (Just $ A. object [" x" .= A. Number 10 , " y" .= A. Number 20 , " z" .= A. String " extra" ])
92
+ , let req = defaultRq `params` Just args
93
+ args = A. object [" x" .= A. Number 10 , " y" .= A. Number 20 , " z" .= A. String " extra" ]
88
94
rsp = defaultRsp `result` A. Number (- 10 )
89
95
in testCase " allow extra named argument" $ assertSubtractResponse req rsp
90
96
@@ -113,32 +119,31 @@ assertInvalidParams :: A.Value -> Assertion
113
119
assertInvalidParams req = assertSubtractResponse req (defaultIdErrRsp (- 32602 ))
114
120
115
121
testBatch :: Assertion
116
- testBatch = sortBy (compare `on` idToString ) <$> response @?= Just expected
117
- where expected = [rsp i1 2 , rsp i2 4 ]
122
+ testBatch = sortBy (compare `on` rspToIdString ) <$> response @?= Just expected
123
+ where expected = [nullIdErrRsp ( - 32600 ), rsp i1 2 , rsp i2 4 ]
118
124
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 ]
125
+ response = fromArray =<< (removeErrMsg <$> callSubtractMethods (array requests) )
126
+ requests = [rq i1 10 8 , rq i2 24 20 , defaultRq `version` Just ( A. String " abc " ) ]
121
127
where rq i x y = defaultRq `id'` Just i `params` toArgs x y
122
128
toArgs :: Int -> Int -> Maybe A. Value
123
129
toArgs x y = Just $ A. object [" x" .= x, " y" .= y]
124
130
i1 = A. Number 1
125
131
i2 = A. Number 2
126
- idToString :: A. Value -> Maybe String
127
- idToString (A. Object rsp) = show <$> H. lookup idKey rsp
128
- idToString _ = Nothing
132
+ fromArray (A. Array v) = Just $ V. toList v
133
+ fromArray _ = Nothing
129
134
130
135
testBatchNotifications :: Assertion
131
136
testBatchNotifications = runState response 0 @?= (Nothing , 10 )
132
- where response = call (toMethods [incrementStateMethod]) $ A. encode rq
137
+ where response = S. call (S. toMethods [incrementStateMethod]) $ A. encode rq
133
138
rq = replicate 10 $ request Nothing " increment" Nothing
134
139
135
140
testAllowMissingVersion :: Assertion
136
141
testAllowMissingVersion = callSubtractMethods requestNoVersion @?= (Just $ defaultRsp `result` A. Number 1 )
137
142
where requestNoVersion = defaultRq `version` Nothing `params` Just (A. object [" x" .= A. Number 1 ])
138
143
139
- incrementStateMethod :: Method (State Int )
140
- incrementStateMethod = toMethod " increment" f ()
141
- where f :: RpcResult (State Int ) ()
144
+ incrementStateMethod :: S. Method (State Int )
145
+ incrementStateMethod = S. toMethod " increment" f ()
146
+ where f :: S. RpcResult (State Int ) ()
142
147
f = lift $ modify (+ 1 )
143
148
144
149
assertGetTimeResponse :: Maybe A. Value -> Assertion
@@ -149,40 +154,35 @@ assertGetTimeResponse args = passed @? "unexpected RPC response"
149
154
rsp = callGetTimeMethod req
150
155
151
156
callSubtractMethods :: A. Value -> Maybe A. Value
152
- callSubtractMethods req = let methods :: Methods Identity
153
- methods = toMethods [subtractMethod, flippedSubtractMethod]
154
- rsp = call methods $ A. encode req
155
- in fromByteString =<< runIdentity rsp
157
+ callSubtractMethods req = let methods :: S. Methods Identity
158
+ methods = S. toMethods [subtractMethod, flippedSubtractMethod]
159
+ rsp = S. call methods $ A. encode req
160
+ in A. decode =<< runIdentity rsp
156
161
157
162
callGetTimeMethod :: A. Value -> IO (Maybe A. Value )
158
- callGetTimeMethod req = let methods :: Methods IO
159
- methods = toMethods [getTimeMethod]
160
- rsp = call methods $ A. encode req
161
- in (fromByteString =<< ) <$> rsp
162
-
163
- fromByteString :: A. FromJSON a => B. ByteString -> Maybe a
164
- fromByteString str = case A. fromJSON <$> A. decode str of
165
- Just (A. Success x) -> Just x
166
- _ -> Nothing
163
+ callGetTimeMethod req = let methods :: S. Methods IO
164
+ methods = S. toMethods [getTimeMethod]
165
+ rsp = S. call methods $ A. encode req
166
+ in (A. decode =<< ) <$> rsp
167
167
168
- subtractMethod :: Method Identity
169
- subtractMethod = toMethod " subtract" subtract (Required " x" :+: Optional " y" 0 :+: () )
168
+ subtractMethod :: S. Method Identity
169
+ subtractMethod = S. toMethod " subtract" subtract (S. Required " x" :+: S. Optional " y" 0 :+: () )
170
170
171
- flippedSubtractMethod :: Method Identity
172
- flippedSubtractMethod = toMethod " flipped subtract" (flip subtract ) ps
173
- where ps = Optional " y" (- 1000 ) :+: Required " x" :+: ()
171
+ flippedSubtractMethod :: S. Method Identity
172
+ flippedSubtractMethod = S. toMethod " flipped subtract" (flip subtract ) ps
173
+ where ps = S. Optional " y" (- 1000 ) :+: S. Required " x" :+: ()
174
174
175
- subtract :: Int -> Int -> RpcResult Identity Int
175
+ subtract :: Int -> Int -> S. RpcResult Identity Int
176
176
subtract x y = return (x - y)
177
177
178
- getTimeMethod :: Method IO
179
- getTimeMethod = toMethod " get_time_seconds" getTestTime ()
180
- where getTestTime :: RpcResult IO Integer
178
+ getTimeMethod :: S. Method IO
179
+ getTimeMethod = S. toMethod " get_time_seconds" getTestTime ()
180
+ where getTestTime :: S. RpcResult IO Integer
181
181
getTestTime = liftIO $ return 100
182
182
183
183
removeErrMsg :: A. Value -> A. Value
184
- removeErrMsg (A. Object rsp) = A. Object $ H. adjust removeMsg errKey rsp
185
- where removeMsg (A. Object err) = A. Object $ H. insert msgKey " " $ H. delete dataKey err
184
+ removeErrMsg (A. Object rsp) = A. Object $ H. adjust removeMsg " error " rsp
185
+ where removeMsg (A. Object err) = A. Object $ H. insert " message " " " $ H. delete " data " err
186
186
removeMsg v = v
187
187
removeErrMsg (A. Array rsps) = A. Array $ removeErrMsg <$> rsps
188
188
removeErrMsg v = v
0 commit comments