Skip to content

Commit c8a127a

Browse files
committed
Merge branch 'development'
* development: add test for transport open calling connect Treat transport open as joining the default namespace
2 parents 84218d5 + 5c0bbde commit c8a127a

File tree

3 files changed

+92
-18
lines changed

3 files changed

+92
-18
lines changed

SocketIO-MacTests/SocketSideEffectTest.swift

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import XCTest
1010
@testable import SocketIO
11+
import Starscream
1112

1213
class SocketSideEffectTest: XCTestCase {
1314
func testInitialCurrentAck() {
@@ -86,6 +87,8 @@ class SocketSideEffectTest: XCTestCase {
8687
func testHandleOnceClientEvent() {
8788
let expect = expectation(description: "handled event")
8889

90+
socket.setTestStatus(.connecting)
91+
8992
socket.once(clientEvent: .connect) {data, ack in
9093
XCTAssertEqual(self.socket.testHandlers.count, 0)
9194
expect.fulfill()
@@ -249,6 +252,8 @@ class SocketSideEffectTest: XCTestCase {
249252
let expect = expectation(description: "The client should call the timeout function")
250253

251254
socket.setTestStatus(.notConnected)
255+
socket.nsp = "/someNamespace"
256+
socket.engine = TestEngine(client: socket, url: socket.socketURL, options: nil)
252257

253258
socket.connect(timeoutAfter: 0.5, withHandler: {
254259
expect.fulfill()
@@ -261,6 +266,7 @@ class SocketSideEffectTest: XCTestCase {
261266
let expect = expectation(description: "The client should not call the timeout function")
262267

263268
socket.setTestStatus(.notConnected)
269+
socket.engine = TestEngine(client: socket, url: socket.socketURL, options: nil)
264270

265271
socket.on(clientEvent: .connect) {data, ack in
266272
expect.fulfill()
@@ -278,11 +284,30 @@ class SocketSideEffectTest: XCTestCase {
278284
waitForExpectations(timeout: 2)
279285
}
280286

287+
func testClientCallsConnectOnEngineOpen() {
288+
let expect = expectation(description: "The client call the connect handler")
289+
290+
socket.setTestStatus(.notConnected)
291+
socket.engine = TestEngine(client: socket, url: socket.socketURL, options: nil)
292+
293+
socket.on(clientEvent: .connect) {data, ack in
294+
expect.fulfill()
295+
}
296+
297+
socket.connect(timeoutAfter: 0.5, withHandler: {
298+
XCTFail("Should not call timeout handler if status is connected")
299+
})
300+
301+
waitForExpectations(timeout: 2)
302+
}
303+
281304
func testConnectIsCalledWithNamespace() {
282305
let expect = expectation(description: "The client should not call the timeout function")
283306
let nspString = "/swift"
284307

285308
socket.setTestStatus(.notConnected)
309+
socket.nsp = nspString
310+
socket.engine = TestEngine(client: socket, url: socket.socketURL, options: nil)
286311

287312
socket.on(clientEvent: .connect) {data, ack in
288313
guard let nsp = data[0] as? String else {
@@ -390,3 +415,40 @@ struct ThrowingData : SocketData {
390415
}
391416

392417
}
418+
419+
class TestEngine : SocketEngineSpec {
420+
weak var client: SocketEngineClient?
421+
private(set) var closed = false
422+
private(set) var connected = false
423+
var connectParams: [String: Any]? = nil
424+
private(set) var cookies: [HTTPCookie]? = nil
425+
private(set) var engineQueue = DispatchQueue.main
426+
private(set) var extraHeaders: [String: String]? = nil
427+
private(set) var fastUpgrade = false
428+
private(set) var forcePolling = false
429+
private(set) var forceWebsockets = false
430+
private(set) var polling = false
431+
private(set) var probing = false
432+
private(set) var sid = ""
433+
private(set) var socketPath = ""
434+
private(set) var urlPolling = URL(string: "http://localhost/")!
435+
private(set) var urlWebSocket = URL(string: "http://localhost/")!
436+
private(set) var websocket = false
437+
private(set) var ws: WebSocket? = nil
438+
439+
required init(client: SocketEngineClient, url: URL, options: NSDictionary?) {
440+
self.client = client
441+
}
442+
443+
func connect() {
444+
client?.engineDidOpen(reason: "Connect")
445+
}
446+
447+
func didError(reason: String) { }
448+
func disconnect(reason: String) { }
449+
func doFastUpgrade() { }
450+
func flushWaitingForPostToWebSocket() { }
451+
func parseEngineData(_ data: Data) { }
452+
func parseEngineMessage(_ message: String) { }
453+
func write(_ msg: String, withType type: SocketEnginePacketType, withData data: [Data]) { }
454+
}

Source/SocketIO/Client/SocketIOClient.swift

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
110110

111111
/// The engine for this client.
112112
@objc
113-
public private(set) var engine: SocketEngineSpec?
113+
public internal(set) var engine: SocketEngineSpec?
114114

115115
/// The array of handlers for this socket.
116116
public private(set) var handlers = [SocketEventHandler]()
@@ -244,6 +244,8 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
244244
///
245245
/// - parameter toNamespace: The namespace that was connected to.
246246
open func didConnect(toNamespace namespace: String) {
247+
guard status != .connected else { return }
248+
247249
DefaultSocketLogger.Logger.log("Socket connected", type: SocketIOClient.logType)
248250

249251
status = .connected
@@ -434,7 +436,21 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
434436
///
435437
/// - parameter reason: The reason the engine opened.
436438
open func engineDidOpen(reason: String) {
437-
DefaultSocketLogger.Logger.log(reason, type: SocketIOClient.logType)
439+
handleQueue.async {
440+
self._engineDidOpen(reason: reason)
441+
}
442+
}
443+
444+
private func _engineDidOpen(reason: String) {
445+
DefaultSocketLogger.Logger.log("Engine opened \(reason)", type: SocketIOClient.logType)
446+
447+
guard nsp != "/" else {
448+
didConnect(toNamespace: "/")
449+
450+
return
451+
}
452+
453+
joinNamespace(nsp)
438454
}
439455

440456
/// Called when socket.io has acked one of our emits. Causes the corresponding ack callback to be called.
@@ -480,10 +496,10 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
480496
/// Call when you wish to leave a namespace and return to the default namespace.
481497
@objc
482498
open func leaveNamespace() {
483-
if nsp != "/" {
484-
engine?.send("1\(nsp)", withData: [])
485-
nsp = "/"
486-
}
499+
guard nsp != "/" else { return }
500+
501+
engine?.send("1\(nsp)", withData: [])
502+
nsp = "/"
487503
}
488504

489505
/// Joins `namespace`.
@@ -493,12 +509,12 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
493509
/// - parameter namespace: The namespace to join.
494510
@objc
495511
open func joinNamespace(_ namespace: String) {
496-
nsp = namespace
512+
guard namespace != "/" else { return }
497513

498-
if nsp != "/" {
499-
DefaultSocketLogger.Logger.log("Joining namespace", type: SocketIOClient.logType)
500-
engine?.send("0\(nsp)", withData: [])
501-
}
514+
DefaultSocketLogger.Logger.log("Joining namespace \(namespace)", type: SocketIOClient.logType)
515+
516+
nsp = namespace
517+
engine?.send("0\(nsp)", withData: [])
502518
}
503519

504520
/// Removes handler(s) for a client event.

Source/SocketIO/Parse/SocketParsable.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,9 @@ public extension SocketParsable where Self: SocketIOClientSpec {
7171
}
7272

7373
private func handleConnect(_ packetNamespace: String) {
74-
// If we connected with a namespace, check if we've joined the default namespace first, then switch to the
75-
// other namespace
76-
if packetNamespace == "/" && nsp != "/" {
77-
joinNamespace(nsp)
78-
} else {
79-
didConnect(toNamespace: packetNamespace)
80-
}
74+
guard packetNamespace == nsp else { return }
75+
76+
didConnect(toNamespace: packetNamespace)
8177
}
8278

8379
private func handlePacket(_ pack: SocketPacket) {

0 commit comments

Comments
 (0)