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 {
88 // AnyObject compliance requires that the implementing object is a class and we can reference it weakly
99 func send< S> ( _ message: S ) -> Void where S: Collection , S. Element == Character
1010 func onRecieve( callback: @escaping ( String ) -> Void ) -> Void
11+ func onClose( callback: @escaping ( ) -> Void ) -> Void
1112 func close( ) -> Void
1213 func error( _ message: String , code: Int ) -> Void
1314}
Original file line number Diff line number Diff line change @@ -43,6 +43,35 @@ class GraphqlTransportWsTests: XCTestCase {
4343 )
4444 }
4545
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+
4675 /// Tests that throwing in the authorization callback forces an unauthorized error
4776 func testAuth( ) throws {
4877 server. auth { payload in
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ import Foundation
1010class TestMessenger : Messenger {
1111 weak var other : TestMessenger ?
1212 var onRecieve : ( String ) -> Void = { _ in }
13+ var onClose : ( ) -> Void = { }
1314 let queue : DispatchQueue = . init( label: " Test messenger " )
1415
1516 init ( ) { }
@@ -29,11 +30,16 @@ class TestMessenger: Messenger {
2930 self . onRecieve = callback
3031 }
3132
33+ func onClose( callback: @escaping ( ) -> Void ) {
34+ self . onClose = callback
35+ }
36+
3237 func error( _ message: String , code: Int ) {
3338 self . send ( " \( code) : \( message) " )
3439 }
3540
3641 func close( ) {
3742 // This is a testing no-op
43+ self . onClose ( )
3844 }
3945}
You can’t perform that action at this time.
0 commit comments