Merge branch 'development'

* development:
  add test for transport open calling connect
  Treat transport open as joining the default namespace
This commit is contained in:
Erik Little 2017-10-04 17:31:43 -04:00
commit c8a127a8ed
No known key found for this signature in database
GPG Key ID: 4930B7C5FBC1A69D
3 changed files with 92 additions and 18 deletions

View File

@ -8,6 +8,7 @@
import XCTest import XCTest
@testable import SocketIO @testable import SocketIO
import Starscream
class SocketSideEffectTest: XCTestCase { class SocketSideEffectTest: XCTestCase {
func testInitialCurrentAck() { func testInitialCurrentAck() {
@ -86,6 +87,8 @@ class SocketSideEffectTest: XCTestCase {
func testHandleOnceClientEvent() { func testHandleOnceClientEvent() {
let expect = expectation(description: "handled event") let expect = expectation(description: "handled event")
socket.setTestStatus(.connecting)
socket.once(clientEvent: .connect) {data, ack in socket.once(clientEvent: .connect) {data, ack in
XCTAssertEqual(self.socket.testHandlers.count, 0) XCTAssertEqual(self.socket.testHandlers.count, 0)
expect.fulfill() expect.fulfill()
@ -249,6 +252,8 @@ class SocketSideEffectTest: XCTestCase {
let expect = expectation(description: "The client should call the timeout function") let expect = expectation(description: "The client should call the timeout function")
socket.setTestStatus(.notConnected) socket.setTestStatus(.notConnected)
socket.nsp = "/someNamespace"
socket.engine = TestEngine(client: socket, url: socket.socketURL, options: nil)
socket.connect(timeoutAfter: 0.5, withHandler: { socket.connect(timeoutAfter: 0.5, withHandler: {
expect.fulfill() expect.fulfill()
@ -261,6 +266,7 @@ class SocketSideEffectTest: XCTestCase {
let expect = expectation(description: "The client should not call the timeout function") let expect = expectation(description: "The client should not call the timeout function")
socket.setTestStatus(.notConnected) socket.setTestStatus(.notConnected)
socket.engine = TestEngine(client: socket, url: socket.socketURL, options: nil)
socket.on(clientEvent: .connect) {data, ack in socket.on(clientEvent: .connect) {data, ack in
expect.fulfill() expect.fulfill()
@ -278,11 +284,30 @@ class SocketSideEffectTest: XCTestCase {
waitForExpectations(timeout: 2) waitForExpectations(timeout: 2)
} }
func testClientCallsConnectOnEngineOpen() {
let expect = expectation(description: "The client call the connect handler")
socket.setTestStatus(.notConnected)
socket.engine = TestEngine(client: socket, url: socket.socketURL, options: nil)
socket.on(clientEvent: .connect) {data, ack in
expect.fulfill()
}
socket.connect(timeoutAfter: 0.5, withHandler: {
XCTFail("Should not call timeout handler if status is connected")
})
waitForExpectations(timeout: 2)
}
func testConnectIsCalledWithNamespace() { func testConnectIsCalledWithNamespace() {
let expect = expectation(description: "The client should not call the timeout function") let expect = expectation(description: "The client should not call the timeout function")
let nspString = "/swift" let nspString = "/swift"
socket.setTestStatus(.notConnected) socket.setTestStatus(.notConnected)
socket.nsp = nspString
socket.engine = TestEngine(client: socket, url: socket.socketURL, options: nil)
socket.on(clientEvent: .connect) {data, ack in socket.on(clientEvent: .connect) {data, ack in
guard let nsp = data[0] as? String else { guard let nsp = data[0] as? String else {
@ -390,3 +415,40 @@ struct ThrowingData : SocketData {
} }
} }
class TestEngine : SocketEngineSpec {
weak var client: SocketEngineClient?
private(set) var closed = false
private(set) var connected = false
var connectParams: [String: Any]? = nil
private(set) var cookies: [HTTPCookie]? = nil
private(set) var engineQueue = DispatchQueue.main
private(set) var extraHeaders: [String: String]? = nil
private(set) var fastUpgrade = false
private(set) var forcePolling = false
private(set) var forceWebsockets = false
private(set) var polling = false
private(set) var probing = false
private(set) var sid = ""
private(set) var socketPath = ""
private(set) var urlPolling = URL(string: "http://localhost/")!
private(set) var urlWebSocket = URL(string: "http://localhost/")!
private(set) var websocket = false
private(set) var ws: WebSocket? = nil
required init(client: SocketEngineClient, url: URL, options: NSDictionary?) {
self.client = client
}
func connect() {
client?.engineDidOpen(reason: "Connect")
}
func didError(reason: String) { }
func disconnect(reason: String) { }
func doFastUpgrade() { }
func flushWaitingForPostToWebSocket() { }
func parseEngineData(_ data: Data) { }
func parseEngineMessage(_ message: String) { }
func write(_ msg: String, withType type: SocketEnginePacketType, withData data: [Data]) { }
}

View File

@ -110,7 +110,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
/// The engine for this client. /// The engine for this client.
@objc @objc
public private(set) var engine: SocketEngineSpec? public internal(set) var engine: SocketEngineSpec?
/// The array of handlers for this socket. /// The array of handlers for this socket.
public private(set) var handlers = [SocketEventHandler]() public private(set) var handlers = [SocketEventHandler]()
@ -244,6 +244,8 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
/// ///
/// - parameter toNamespace: The namespace that was connected to. /// - parameter toNamespace: The namespace that was connected to.
open func didConnect(toNamespace namespace: String) { open func didConnect(toNamespace namespace: String) {
guard status != .connected else { return }
DefaultSocketLogger.Logger.log("Socket connected", type: SocketIOClient.logType) DefaultSocketLogger.Logger.log("Socket connected", type: SocketIOClient.logType)
status = .connected status = .connected
@ -434,7 +436,21 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
/// ///
/// - parameter reason: The reason the engine opened. /// - parameter reason: The reason the engine opened.
open func engineDidOpen(reason: String) { open func engineDidOpen(reason: String) {
DefaultSocketLogger.Logger.log(reason, type: SocketIOClient.logType) handleQueue.async {
self._engineDidOpen(reason: reason)
}
}
private func _engineDidOpen(reason: String) {
DefaultSocketLogger.Logger.log("Engine opened \(reason)", type: SocketIOClient.logType)
guard nsp != "/" else {
didConnect(toNamespace: "/")
return
}
joinNamespace(nsp)
} }
/// Called when socket.io has acked one of our emits. Causes the corresponding ack callback to be called. /// 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
/// Call when you wish to leave a namespace and return to the default namespace. /// Call when you wish to leave a namespace and return to the default namespace.
@objc @objc
open func leaveNamespace() { open func leaveNamespace() {
if nsp != "/" { guard nsp != "/" else { return }
engine?.send("1\(nsp)", withData: [])
nsp = "/" engine?.send("1\(nsp)", withData: [])
} nsp = "/"
} }
/// Joins `namespace`. /// Joins `namespace`.
@ -493,12 +509,12 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
/// - parameter namespace: The namespace to join. /// - parameter namespace: The namespace to join.
@objc @objc
open func joinNamespace(_ namespace: String) { open func joinNamespace(_ namespace: String) {
nsp = namespace guard namespace != "/" else { return }
if nsp != "/" { DefaultSocketLogger.Logger.log("Joining namespace \(namespace)", type: SocketIOClient.logType)
DefaultSocketLogger.Logger.log("Joining namespace", type: SocketIOClient.logType)
engine?.send("0\(nsp)", withData: []) nsp = namespace
} engine?.send("0\(nsp)", withData: [])
} }
/// Removes handler(s) for a client event. /// Removes handler(s) for a client event.

View File

@ -71,13 +71,9 @@ public extension SocketParsable where Self: SocketIOClientSpec {
} }
private func handleConnect(_ packetNamespace: String) { private func handleConnect(_ packetNamespace: String) {
// If we connected with a namespace, check if we've joined the default namespace first, then switch to the guard packetNamespace == nsp else { return }
// other namespace
if packetNamespace == "/" && nsp != "/" { didConnect(toNamespace: packetNamespace)
joinNamespace(nsp)
} else {
didConnect(toNamespace: packetNamespace)
}
} }
private func handlePacket(_ pack: SocketPacket) { private func handlePacket(_ pack: SocketPacket) {