@@ -11,23 +11,7 @@ import XCTest
1111
1212class IMClientTestCase : RTMBaseTestCase {
1313
14- func testInitWithUser( ) {
15- let user = LCUser ( )
16- user. username = UUID ( ) . uuidString. lcString
17- user. password = UUID ( ) . uuidString. lcString
18-
19- XCTAssertTrue ( user. signUp ( ) . isSuccess)
20-
21- do {
22- let client = try IMClient ( user: user)
23- XCTAssertNotNil ( client. user)
24- XCTAssertEqual ( client. ID, user. objectId? . stringValue)
25- } catch {
26- XCTFail ( " \( error) " )
27- }
28- }
29-
30- func testDeinit( ) {
14+ func testInit( ) {
3115 do {
3216 let invalidID : String = Array < String > . init ( repeating: " a " , count: 65 ) . joined ( )
3317 let _ = try IMClient ( ID: invalidID)
@@ -45,23 +29,42 @@ class IMClientTestCase: RTMBaseTestCase {
4529 }
4630
4731 do {
48- var client : IMClient ? = try IMClient ( ID: " qweasd " , tag: " mobile " )
49- XCTAssertNotNil ( client? . deviceTokenObservation)
50- XCTAssertNotNil ( client? . fallbackUDID)
51- client = nil
52- XCTAssertNil ( client)
32+ let _ = try IMClient ( ID: uuid)
33+ let _ = try IMClient ( ID: uuid, tag: uuid)
5334 } catch {
54- XCTFail ( )
35+ XCTFail ( " \( error ) " )
5536 }
5637 }
38+
39+ func testInitWithUser( ) {
40+ let user = LCUser ( )
41+ user. username = UUID ( ) . uuidString. lcString
42+ user. password = UUID ( ) . uuidString. lcString
43+
44+ XCTAssertTrue ( user. signUp ( ) . isSuccess)
45+
46+ do {
47+ let client = try IMClient ( user: user)
48+ XCTAssertNotNil ( client. user)
49+ XCTAssertEqual ( client. ID, user. objectId? . stringValue)
50+ } catch {
51+ XCTFail ( " \( error) " )
52+ }
53+ }
54+
55+ func testDeinit( ) {
56+ var client : IMClient ? = try ! IMClient ( ID: uuid, tag: uuid)
57+ weak var wClient : IMClient ? = client
58+ client = nil
59+ delay ( )
60+ XCTAssertNil ( wClient)
61+ }
5762
5863 func testOpenAndClose( ) {
5964 let client : IMClient = try ! IMClient ( ID: uuid)
6065
61- for _ in 0 ..< 3 {
62- let exp = expectation ( description: " open and close " )
63- exp. expectedFulfillmentCount = 3
64- client. open { ( result) in
66+ expecting { ( exp) in
67+ client. open ( completion: { ( result) in
6568 XCTAssertTrue ( Thread . isMainThread)
6669 XCTAssertTrue ( result. isSuccess)
6770 XCTAssertNil ( result. error)
@@ -70,24 +73,66 @@ class IMClientTestCase: RTMBaseTestCase {
7073 XCTAssertNil ( client. openingOptions)
7174 XCTAssertNil ( client. openingCompletion)
7275 XCTAssertEqual ( client. sessionState, . opened)
76+ XCTAssertNotNil ( client. connectionDelegator. delegate)
77+ exp. fulfill ( )
78+ } )
79+ }
80+
81+ expecting { ( exp) in
82+ client. open { ( result) in
83+ XCTAssertTrue ( result. isFailure)
84+ XCTAssertNotNil ( result. error)
85+ exp. fulfill ( )
86+ }
87+ }
88+
89+ expecting { ( exp) in
90+ client. close ( ) { ( result) in
91+ XCTAssertTrue ( Thread . isMainThread)
92+ XCTAssertTrue ( result. isSuccess)
93+ XCTAssertNil ( result. error)
94+ XCTAssertNil ( client. sessionToken)
95+ XCTAssertNil ( client. sessionTokenExpiration)
96+ XCTAssertNil ( client. openingOptions)
97+ XCTAssertNil ( client. openingCompletion)
98+ XCTAssertEqual ( client. sessionState, . closed)
99+ XCTAssertNil ( client. connectionDelegator. delegate)
73100 exp. fulfill ( )
74- client. open { ( result) in
75- XCTAssertNotNil ( result. error)
101+ }
102+ }
103+ }
104+
105+ func testOpenWithSignature( ) {
106+ let user = LCUser ( )
107+ user. username = UUID ( ) . uuidString. lcString
108+ user. password = UUID ( ) . uuidString. lcString
109+
110+ XCTAssertTrue ( user. signUp ( ) . isSuccess)
111+
112+ if let objectID = user. objectId? . value, let sessionToken = user. sessionToken? . value {
113+
114+ var clientWithUser : IMClient ! = try ! IMClient ( user: user)
115+ expecting { ( exp) in
116+ clientWithUser. open ( completion: { ( result) in
117+ XCTAssertTrue ( result. isSuccess)
118+ XCTAssertNil ( result. error)
76119 exp. fulfill ( )
77- client. close ( ) { ( result) in
78- XCTAssertTrue ( Thread . isMainThread)
79- XCTAssertTrue ( result. isSuccess)
80- XCTAssertNil ( result. error)
81- XCTAssertNil ( client. sessionToken)
82- XCTAssertNil ( client. sessionTokenExpiration)
83- XCTAssertNil ( client. openingOptions)
84- XCTAssertNil ( client. openingCompletion)
85- XCTAssertEqual ( client. sessionState, . closed)
86- exp. fulfill ( )
87- }
88- }
120+ } )
121+ }
122+
123+ clientWithUser = nil
124+ delay ( )
125+
126+ let signatureDelegator = SignatureDelegator ( )
127+ signatureDelegator. sessionToken = sessionToken
128+ let clientWithID = try ! IMClient ( ID: objectID, signatureDelegate: signatureDelegator)
129+ expecting { ( exp) in
130+ clientWithID. open ( completion: { ( result) in
131+ XCTAssertTrue ( result. isSuccess)
132+ XCTAssertNil ( result. error)
133+ exp. fulfill ( )
134+ } )
89135 }
90- waitForExpectations ( timeout: timeout, handler: nil )
91136 }
92137 }
93138
@@ -166,8 +211,8 @@ class IMClientTestCase: RTMBaseTestCase {
166211 }
167212 wait ( for: [ exp1] , timeout: timeout)
168213
169- RTMConnectionRefMap_protobuf1 . removeAll ( )
170- RTMConnectionRefMap_protobuf3 . removeAll ( )
214+ RTMConnectionManager . default . protobuf1Map . removeAll ( )
215+ RTMConnectionManager . default . protobuf3Map . removeAll ( )
171216
172217 applicationRegistry. removeAll ( )
173218 let application2 : LCApplication = try ! LCApplication (
@@ -501,30 +546,6 @@ class IMClientTestCase: RTMBaseTestCase {
501546
502547 XCTAssertEqual ( client. convCollection. count, 2 )
503548 }
504-
505- func testInitWithUserAndOpenWithSignature( ) {
506- let user = LCUser ( )
507- user. username = UUID ( ) . uuidString. lcString
508- user. password = UUID ( ) . uuidString. lcString
509-
510- XCTAssertTrue ( user. signUp ( ) . isSuccess)
511-
512- if let sessionToken = user. sessionToken? . stringValue {
513-
514- let signatureDelegator = SignatureDelegator ( )
515- signatureDelegator. sessionToken = sessionToken
516-
517- let client = try ! IMClient ( user: user, signatureDelegate: signatureDelegator)
518-
519- expecting { ( exp) in
520- client. open ( completion: { ( result) in
521- XCTAssertTrue ( result. isSuccess)
522- XCTAssertNil ( result. error)
523- exp. fulfill ( )
524- } )
525- }
526- }
527- }
528549
529550}
530551
@@ -564,37 +585,40 @@ extension IMClientTestCase {
564585
565586 var sessionToken : String ?
566587
588+ func getOpenSignature( client: IMClient , completion: @escaping ( IMSignature ) -> Void ) {
589+ guard let sessionToken = self . sessionToken else {
590+ return
591+ }
592+ let application = client. application
593+ let httpClient : HTTPClient = application. httpClient
594+ let url = application. v2router. route ( path: " rtm/clients/sign " , module: . api) !
595+ let parameters : [ String : Any ] = [ " session_token " : sessionToken]
596+ _ = httpClient. request ( url: url, method: . get, parameters: parameters) { ( response) in
597+ guard
598+ let value = response. value as? [ String : Any ] ,
599+ let client_id = value [ " client_id " ] as? String ,
600+ client_id == client. ID,
601+ let signature = value [ " signature " ] as? String ,
602+ let timestamp = value [ " timestamp " ] as? Int64 ,
603+ let nonce = value [ " nonce " ] as? String else
604+ {
605+ return
606+ }
607+ completion ( IMSignature ( signature: signature, timestamp: timestamp, nonce: nonce) )
608+ }
609+ }
610+
567611 func client( _ client: IMClient , action: IMSignature . Action , signatureHandler: @escaping ( IMClient , IMSignature ? ) -> Void ) {
568612 XCTAssertTrue ( Thread . isMainThread)
569613 switch action {
570614 case . open:
571- guard let sessionToken = self . sessionToken else {
572- break
573- }
574-
575- let application = client. application
576- let httpClient : HTTPClient = application. httpClient
577- let url = application. v2router. route ( path: " rtm/clients/sign " , module: . api) !
578- let parameters : [ String : Any ] = [ " session_token " : sessionToken]
579-
580- let _ = httpClient. request ( url: url, method: . get, parameters: parameters) { ( response) in
581- guard
582- let value = response. value as? [ String : Any ] ,
583- let client_id = value [ " client_id " ] as? String ,
584- client_id == client. ID,
585- let signature = value [ " signature " ] as? String ,
586- let timestamp = value [ " timestamp " ] as? Int64 ,
587- let nonce = value [ " nonce " ] as? String else
588- {
589- return
590- }
591- signatureHandler ( client, IMSignature ( signature: signature, timestamp: timestamp, nonce: nonce) )
615+ self . getOpenSignature ( client: client) { ( signature) in
616+ signatureHandler ( client, signature)
592617 }
593618 default :
594619 break
595620 }
596621 }
597-
598622 }
599623
600624}
0 commit comments