74 lines
2.2 KiB
Swift
74 lines
2.2 KiB
Swift
//
|
|
// SocketSideEffectTest.swift
|
|
// Socket.IO-Client-Swift
|
|
//
|
|
// Created by Erik Little on 10/11/15.
|
|
//
|
|
//
|
|
|
|
import XCTest
|
|
|
|
class SocketSideEffectTest: XCTestCase {
|
|
let data = "test".dataUsingEncoding(NSUTF8StringEncoding)!
|
|
let data2 = "test2".dataUsingEncoding(NSUTF8StringEncoding)!
|
|
private var socket: SocketIOClient!
|
|
|
|
override func setUp() {
|
|
super.setUp()
|
|
socket = SocketIOClient(socketURL: "")
|
|
socket.setTestable()
|
|
}
|
|
|
|
func testInitialCurrentAck() {
|
|
XCTAssertEqual(socket.currentAck, -1)
|
|
}
|
|
|
|
func testFirstAck() {
|
|
socket.emitWithAck("test")(timeoutAfter: 0) {data in}
|
|
XCTAssertEqual(socket.currentAck, 0)
|
|
}
|
|
|
|
func testSecondAck() {
|
|
socket.emitWithAck("test")(timeoutAfter: 0) {data in}
|
|
socket.emitWithAck("test")(timeoutAfter: 0) {data in}
|
|
|
|
XCTAssertEqual(socket.currentAck, 1)
|
|
}
|
|
|
|
func testHandleAck() {
|
|
let expectation = expectationWithDescription("handled ack")
|
|
socket.emitWithAck("test")(timeoutAfter: 0) {data in
|
|
XCTAssertEqual(data[0] as? String, "hello world")
|
|
expectation.fulfill()
|
|
}
|
|
|
|
socket.parseSocketMessage("30[\"hello world\"]")
|
|
waitForExpectationsWithTimeout(3, handler: nil)
|
|
}
|
|
|
|
func testHandleEvent() {
|
|
let expectation = expectationWithDescription("handled event")
|
|
socket.on("test") {data, ack in
|
|
XCTAssertEqual(data[0] as? String, "hello world")
|
|
expectation.fulfill()
|
|
}
|
|
|
|
socket.parseSocketMessage("2[\"test\",\"hello world\"]")
|
|
waitForExpectationsWithTimeout(3, handler: nil)
|
|
}
|
|
|
|
func testHandleBinaryEvent() {
|
|
let expectation = expectationWithDescription("handled binary event")
|
|
socket.on("test") {data, ack in
|
|
if let dict = data[0] as? NSDictionary, data = dict["test"] as? NSData {
|
|
XCTAssertEqual(data, self.data)
|
|
expectation.fulfill()
|
|
}
|
|
}
|
|
|
|
socket.parseSocketMessage("51-[\"test\",{\"test\":{\"_placeholder\":true,\"num\":0}}]")
|
|
socket.parseBinaryData(data)
|
|
waitForExpectationsWithTimeout(3, handler: nil)
|
|
}
|
|
}
|