File tree Expand file tree Collapse file tree 3 files changed +36
-0
lines changed
Sources/GraphQLTransportWS
Tests/GraphQLTransportWSTests Expand file tree Collapse file tree 3 files changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ public protocol Messenger: AnyObject {
8
8
// AnyObject compliance requires that the implementing object is a class and we can reference it weakly
9
9
func send< S> ( _ message: S ) -> Void where S: Collection , S. Element == Character
10
10
func onRecieve( callback: @escaping ( String ) -> Void ) -> Void
11
+ func onClose( callback: @escaping ( ) -> Void ) -> Void
11
12
func close( ) -> Void
12
13
func error( _ message: String , code: Int ) -> Void
13
14
}
Original file line number Diff line number Diff line change @@ -43,6 +43,35 @@ class GraphqlTransportWsTests: XCTestCase {
43
43
)
44
44
}
45
45
46
+ /// Tests that trying to run methods before `connection_init` is not allowed
47
+ func testInitialize( ) throws {
48
+ var messages = [ String] ( )
49
+ let completeExpectation = XCTestExpectation ( )
50
+
51
+ let client = Client ( messenger: clientMessenger)
52
+ client. onMessage { message, _ in
53
+ messages. append ( message)
54
+ completeExpectation. fulfill ( )
55
+ }
56
+
57
+ client. sendStart (
58
+ payload: GraphQLRequest (
59
+ query: """
60
+ query {
61
+ hello
62
+ }
63
+ """
64
+ ) ,
65
+ id: UUID ( ) . uuidString
66
+ )
67
+
68
+ wait ( for: [ completeExpectation] , timeout: 2 )
69
+ XCTAssertEqual (
70
+ messages,
71
+ [ " 4407: Connection not initialized " ]
72
+ )
73
+ }
74
+
46
75
/// Tests that throwing in the authorization callback forces an unauthorized error
47
76
func testAuth( ) throws {
48
77
server. auth { payload in
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ import Foundation
10
10
class TestMessenger : Messenger {
11
11
weak var other : TestMessenger ?
12
12
var onRecieve : ( String ) -> Void = { _ in }
13
+ var onClose : ( ) -> Void = { }
13
14
let queue : DispatchQueue = . init( label: " Test messenger " )
14
15
15
16
init ( ) { }
@@ -29,11 +30,16 @@ class TestMessenger: Messenger {
29
30
self . onRecieve = callback
30
31
}
31
32
33
+ func onClose( callback: @escaping ( ) -> Void ) {
34
+ self . onClose = callback
35
+ }
36
+
32
37
func error( _ message: String , code: Int ) {
33
38
self . send ( " \( code) : \( message) " )
34
39
}
35
40
36
41
func close( ) {
37
42
// This is a testing no-op
43
+ self . onClose ( )
38
44
}
39
45
}
You can’t perform that action at this time.
0 commit comments