update tests, add test for base64
This commit is contained in:
parent
777fa30c76
commit
4ad5025c77
@ -13,14 +13,14 @@ class SocketAckManagerTest: XCTestCase {
|
|||||||
var ackManager = SocketAckManager()
|
var ackManager = SocketAckManager()
|
||||||
|
|
||||||
func testAddAcks() {
|
func testAddAcks() {
|
||||||
let callbackExpection = self.expectation(withDescription: "callbackExpection")
|
let callbackExpection = self.expectation(description: "callbackExpection")
|
||||||
let itemsArray = ["Hi", "ho"]
|
let itemsArray = ["Hi", "ho"]
|
||||||
func callback(_ items: [AnyObject]) {
|
func callback(_ items: [AnyObject]) {
|
||||||
callbackExpection.fulfill()
|
callbackExpection.fulfill()
|
||||||
}
|
}
|
||||||
ackManager.addAck(1, callback: callback)
|
ackManager.addAck(1, callback: callback)
|
||||||
ackManager.executeAck(1, with: itemsArray, onQueue: DispatchQueue.main)
|
ackManager.executeAck(1, with: itemsArray, onQueue: DispatchQueue.main)
|
||||||
waitForExpectations(withTimeout: 3.0, handler: nil)
|
waitForExpectations(timeout: 3.0, handler: nil)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -154,8 +154,8 @@ class SocketBasicPacketTest: XCTestCase {
|
|||||||
if case let .right(packet) = socket.parseString(engineString) {
|
if case let .right(packet) = socket.parseString(engineString) {
|
||||||
var packet = packet
|
var packet = packet
|
||||||
XCTAssertEqual(packet.event, "test")
|
XCTAssertEqual(packet.event, "test")
|
||||||
packet.addData(data)
|
_ = packet.addData(data)
|
||||||
packet.addData(data2)
|
_ = packet.addData(data2)
|
||||||
XCTAssertEqual(packet.args[0] as? String, "~~0")
|
XCTAssertEqual(packet.args[0] as? String, "~~0")
|
||||||
} else {
|
} else {
|
||||||
XCTFail()
|
XCTFail()
|
||||||
|
|||||||
@ -22,17 +22,17 @@ class SocketEngineTest: XCTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testBasicPollingMessage() {
|
func testBasicPollingMessage() {
|
||||||
let expect = expectation(withDescription: "Basic polling test")
|
let expect = expectation(description: "Basic polling test")
|
||||||
client.on("blankTest") {data, ack in
|
client.on("blankTest") {data, ack in
|
||||||
expect.fulfill()
|
expect.fulfill()
|
||||||
}
|
}
|
||||||
|
|
||||||
engine.parsePollingMessage("15:42[\"blankTest\"]")
|
engine.parsePollingMessage("15:42[\"blankTest\"]")
|
||||||
waitForExpectations(withTimeout: 3, handler: nil)
|
waitForExpectations(timeout: 3, handler: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testTwoPacketsInOnePollTest() {
|
func testTwoPacketsInOnePollTest() {
|
||||||
let finalExpectation = expectation(withDescription: "Final packet in poll test")
|
let finalExpectation = expectation(description: "Final packet in poll test")
|
||||||
var gotBlank = false
|
var gotBlank = false
|
||||||
|
|
||||||
client.on("blankTest") {data, ack in
|
client.on("blankTest") {data, ack in
|
||||||
@ -40,7 +40,7 @@ class SocketEngineTest: XCTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
client.on("stringTest") {data, ack in
|
client.on("stringTest") {data, ack in
|
||||||
if let str = data[0] as? String where gotBlank {
|
if let str = data[0] as? String, gotBlank {
|
||||||
if str == "hello" {
|
if str == "hello" {
|
||||||
finalExpectation.fulfill()
|
finalExpectation.fulfill()
|
||||||
}
|
}
|
||||||
@ -48,35 +48,35 @@ class SocketEngineTest: XCTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
engine.parsePollingMessage("15:42[\"blankTest\"]24:42[\"stringTest\",\"hello\"]")
|
engine.parsePollingMessage("15:42[\"blankTest\"]24:42[\"stringTest\",\"hello\"]")
|
||||||
waitForExpectations(withTimeout: 3, handler: nil)
|
waitForExpectations(timeout: 3, handler: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testEngineDoesErrorOnUnknownTransport() {
|
func testEngineDoesErrorOnUnknownTransport() {
|
||||||
let finalExpectation = expectation(withDescription: "Unknown Transport")
|
let finalExpectation = expectation(description: "Unknown Transport")
|
||||||
|
|
||||||
client.on("error") {data, ack in
|
client.on("error") {data, ack in
|
||||||
if let error = data[0] as? String where error == "Unknown transport" {
|
if let error = data[0] as? String, error == "Unknown transport" {
|
||||||
finalExpectation.fulfill()
|
finalExpectation.fulfill()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
engine.parseEngineMessage("{\"code\": 0, \"message\": \"Unknown transport\"}", fromPolling: false)
|
engine.parseEngineMessage("{\"code\": 0, \"message\": \"Unknown transport\"}", fromPolling: false)
|
||||||
waitForExpectations(withTimeout: 3, handler: nil)
|
waitForExpectations(timeout: 3, handler: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testEngineDoesErrorOnUnknownMessage() {
|
func testEngineDoesErrorOnUnknownMessage() {
|
||||||
let finalExpectation = expectation(withDescription: "Engine Errors")
|
let finalExpectation = expectation(description: "Engine Errors")
|
||||||
|
|
||||||
client.on("error") {data, ack in
|
client.on("error") {data, ack in
|
||||||
finalExpectation.fulfill()
|
finalExpectation.fulfill()
|
||||||
}
|
}
|
||||||
|
|
||||||
engine.parseEngineMessage("afafafda", fromPolling: false)
|
engine.parseEngineMessage("afafafda", fromPolling: false)
|
||||||
waitForExpectations(withTimeout: 3, handler: nil)
|
waitForExpectations(timeout: 3, handler: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testEngineDecodesUTF8Properly() {
|
func testEngineDecodesUTF8Properly() {
|
||||||
let expect = expectation(withDescription: "Engine Decodes utf8")
|
let expect = expectation(description: "Engine Decodes utf8")
|
||||||
|
|
||||||
client.on("stringTest") {data, ack in
|
client.on("stringTest") {data, ack in
|
||||||
XCTAssertEqual(data[0] as? String, "lïne one\nlīne \rtwo", "Failed string test")
|
XCTAssertEqual(data[0] as? String, "lïne one\nlīne \rtwo", "Failed string test")
|
||||||
@ -84,7 +84,7 @@ class SocketEngineTest: XCTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
engine.parsePollingMessage("41:42[\"stringTest\",\"lïne one\\nlīne \\rtwo\"]")
|
engine.parsePollingMessage("41:42[\"stringTest\",\"lïne one\\nlīne \\rtwo\"]")
|
||||||
waitForExpectations(withTimeout: 3, handler: nil)
|
waitForExpectations(timeout: 3, handler: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testEncodeURLProperly() {
|
func testEncodeURLProperly() {
|
||||||
@ -102,4 +102,23 @@ class SocketEngineTest: XCTestCase {
|
|||||||
XCTAssertEqual(engine.urlPolling.query, "transport=polling&b64=1&forbidden=%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%25%23%5B%5D%22%20%7B%7D")
|
XCTAssertEqual(engine.urlPolling.query, "transport=polling&b64=1&forbidden=%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%25%23%5B%5D%22%20%7B%7D")
|
||||||
XCTAssertEqual(engine.urlWebSocket.query, "transport=websocket&forbidden=%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%25%23%5B%5D%22%20%7B%7D")
|
XCTAssertEqual(engine.urlWebSocket.query, "transport=websocket&forbidden=%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%25%23%5B%5D%22%20%7B%7D")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testBase64Data() {
|
||||||
|
let expect = expectation(description: "Engine Decodes base64 data")
|
||||||
|
let b64String = "b4aGVsbG8NCg=="
|
||||||
|
let packetString = "451-[\"test\",{\"test\":{\"_placeholder\":true,\"num\":0}}]"
|
||||||
|
|
||||||
|
client.on("test") {data, ack in
|
||||||
|
if let data = data[0] as? Data, let string = String(data: data, encoding: .utf8) {
|
||||||
|
XCTAssertEqual(string, "hello")
|
||||||
|
}
|
||||||
|
|
||||||
|
expect.fulfill()
|
||||||
|
}
|
||||||
|
|
||||||
|
engine.parseEngineMessage(packetString, fromPolling: false)
|
||||||
|
engine.parseEngineMessage(b64String, fromPolling: false)
|
||||||
|
|
||||||
|
waitForExpectations(timeout: 3, handler: nil)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -136,7 +136,7 @@ class SocketParserTest: XCTestCase {
|
|||||||
let keys = Array(SocketParserTest.packetTypes.keys)
|
let keys = Array(SocketParserTest.packetTypes.keys)
|
||||||
measure {
|
measure {
|
||||||
for item in keys.enumerated() {
|
for item in keys.enumerated() {
|
||||||
self.testSocket.parseString(item.element)
|
_ = self.testSocket.parseString(item.element)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -37,18 +37,18 @@ class SocketSideEffectTest: XCTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testHandleAck() {
|
func testHandleAck() {
|
||||||
let expect = expectation(withDescription: "handled ack")
|
let expect = expectation(description: "handled ack")
|
||||||
socket.emitWithAck("test")(timeoutAfter: 0) {data in
|
socket.emitWithAck("test")(timeoutAfter: 0) {data in
|
||||||
XCTAssertEqual(data[0] as? String, "hello world")
|
XCTAssertEqual(data[0] as? String, "hello world")
|
||||||
expect.fulfill()
|
expect.fulfill()
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.parseSocketMessage("30[\"hello world\"]")
|
socket.parseSocketMessage("30[\"hello world\"]")
|
||||||
waitForExpectations(withTimeout: 3, handler: nil)
|
waitForExpectations(timeout: 3, handler: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testHandleAck2() {
|
func testHandleAck2() {
|
||||||
let expect = expectation(withDescription: "handled ack2")
|
let expect = expectation(description: "handled ack2")
|
||||||
socket.emitWithAck("test")(timeoutAfter: 0) {data in
|
socket.emitWithAck("test")(timeoutAfter: 0) {data in
|
||||||
XCTAssertTrue(data.count == 2, "Wrong number of ack items")
|
XCTAssertTrue(data.count == 2, "Wrong number of ack items")
|
||||||
expect.fulfill()
|
expect.fulfill()
|
||||||
@ -56,33 +56,33 @@ class SocketSideEffectTest: XCTestCase {
|
|||||||
|
|
||||||
socket.parseSocketMessage("61-0[{\"_placeholder\":true,\"num\":0},{\"test\":true}]")
|
socket.parseSocketMessage("61-0[{\"_placeholder\":true,\"num\":0},{\"test\":true}]")
|
||||||
socket.parseBinaryData(Data())
|
socket.parseBinaryData(Data())
|
||||||
waitForExpectations(withTimeout: 3, handler: nil)
|
waitForExpectations(timeout: 3, handler: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testHandleEvent() {
|
func testHandleEvent() {
|
||||||
let expect = expectation(withDescription: "handled event")
|
let expect = expectation(description: "handled event")
|
||||||
socket.on("test") {data, ack in
|
socket.on("test") {data, ack in
|
||||||
XCTAssertEqual(data[0] as? String, "hello world")
|
XCTAssertEqual(data[0] as? String, "hello world")
|
||||||
expect.fulfill()
|
expect.fulfill()
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.parseSocketMessage("2[\"test\",\"hello world\"]")
|
socket.parseSocketMessage("2[\"test\",\"hello world\"]")
|
||||||
waitForExpectations(withTimeout: 3, handler: nil)
|
waitForExpectations(timeout: 3, handler: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testHandleStringEventWithQuotes() {
|
func testHandleStringEventWithQuotes() {
|
||||||
let expect = expectation(withDescription: "handled event")
|
let expect = expectation(description: "handled event")
|
||||||
socket.on("test") {data, ack in
|
socket.on("test") {data, ack in
|
||||||
XCTAssertEqual(data[0] as? String, "\"hello world\"")
|
XCTAssertEqual(data[0] as? String, "\"hello world\"")
|
||||||
expect.fulfill()
|
expect.fulfill()
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.parseSocketMessage("2[\"test\",\"\\\"hello world\\\"\"]")
|
socket.parseSocketMessage("2[\"test\",\"\\\"hello world\\\"\"]")
|
||||||
waitForExpectations(withTimeout: 3, handler: nil)
|
waitForExpectations(timeout: 3, handler: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testHandleOnceEvent() {
|
func testHandleOnceEvent() {
|
||||||
let expect = expectation(withDescription: "handled event")
|
let expect = expectation(description: "handled event")
|
||||||
socket.once("test") {data, ack in
|
socket.once("test") {data, ack in
|
||||||
XCTAssertEqual(data[0] as? String, "hello world")
|
XCTAssertEqual(data[0] as? String, "hello world")
|
||||||
XCTAssertEqual(self.socket.testHandlers.count, 0)
|
XCTAssertEqual(self.socket.testHandlers.count, 0)
|
||||||
@ -90,7 +90,7 @@ class SocketSideEffectTest: XCTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
socket.parseSocketMessage("2[\"test\",\"hello world\"]")
|
socket.parseSocketMessage("2[\"test\",\"hello world\"]")
|
||||||
waitForExpectations(withTimeout: 3, handler: nil)
|
waitForExpectations(timeout: 3, handler: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testOffWithEvent() {
|
func testOffWithEvent() {
|
||||||
@ -112,21 +112,21 @@ class SocketSideEffectTest: XCTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testHandlesErrorPacket() {
|
func testHandlesErrorPacket() {
|
||||||
let expect = expectation(withDescription: "Handled error")
|
let expect = expectation(description: "Handled error")
|
||||||
socket.on("error") {data, ack in
|
socket.on("error") {data, ack in
|
||||||
if let error = data[0] as? String where error == "test error" {
|
if let error = data[0] as? String, error == "test error" {
|
||||||
expect.fulfill()
|
expect.fulfill()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.parseSocketMessage("4\"test error\"")
|
socket.parseSocketMessage("4\"test error\"")
|
||||||
waitForExpectations(withTimeout: 3, handler: nil)
|
waitForExpectations(timeout: 3, handler: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testHandleBinaryEvent() {
|
func testHandleBinaryEvent() {
|
||||||
let expect = expectation(withDescription: "handled binary event")
|
let expect = expectation(description: "handled binary event")
|
||||||
socket.on("test") {data, ack in
|
socket.on("test") {data, ack in
|
||||||
if let dict = data[0] as? NSDictionary, data = dict["test"] as? NSData {
|
if let dict = data[0] as? NSDictionary, let data = dict["test"] as? NSData {
|
||||||
XCTAssertEqual(data, self.data)
|
XCTAssertEqual(data, self.data)
|
||||||
expect.fulfill()
|
expect.fulfill()
|
||||||
}
|
}
|
||||||
@ -134,7 +134,7 @@ class SocketSideEffectTest: XCTestCase {
|
|||||||
|
|
||||||
socket.parseSocketMessage("51-[\"test\",{\"test\":{\"_placeholder\":true,\"num\":0}}]")
|
socket.parseSocketMessage("51-[\"test\",{\"test\":{\"_placeholder\":true,\"num\":0}}]")
|
||||||
socket.parseBinaryData(data)
|
socket.parseBinaryData(data)
|
||||||
waitForExpectations(withTimeout: 3, handler: nil)
|
waitForExpectations(timeout: 3, handler: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testSocketDataToAnyObject() {
|
func testSocketDataToAnyObject() {
|
||||||
@ -144,10 +144,10 @@ class SocketSideEffectTest: XCTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testHandleMultipleBinaryEvent() {
|
func testHandleMultipleBinaryEvent() {
|
||||||
let expect = expectation(withDescription: "handled multiple binary event")
|
let expect = expectation(description: "handled multiple binary event")
|
||||||
socket.on("test") {data, ack in
|
socket.on("test") {data, ack in
|
||||||
if let dict = data[0] as? NSDictionary, data = dict["test"] as? NSData,
|
if let dict = data[0] as? NSDictionary, let data = dict["test"] as? NSData,
|
||||||
data2 = dict["test2"] as? NSData {
|
let data2 = dict["test2"] as? NSData {
|
||||||
XCTAssertEqual(data, self.data)
|
XCTAssertEqual(data, self.data)
|
||||||
XCTAssertEqual(data2, self.data2)
|
XCTAssertEqual(data2, self.data2)
|
||||||
expect.fulfill()
|
expect.fulfill()
|
||||||
@ -157,7 +157,7 @@ class SocketSideEffectTest: XCTestCase {
|
|||||||
socket.parseSocketMessage("52-[\"test\",{\"test\":{\"_placeholder\":true,\"num\":0},\"test2\":{\"_placeholder\":true,\"num\":1}}]")
|
socket.parseSocketMessage("52-[\"test\",{\"test\":{\"_placeholder\":true,\"num\":0},\"test2\":{\"_placeholder\":true,\"num\":1}}]")
|
||||||
socket.parseBinaryData(data)
|
socket.parseBinaryData(data)
|
||||||
socket.parseBinaryData(data2)
|
socket.parseBinaryData(data2)
|
||||||
waitForExpectations(withTimeout: 3, handler: nil)
|
waitForExpectations(timeout: 3, handler: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testSocketManager() {
|
func testSocketManager() {
|
||||||
|
|||||||
@ -810,7 +810,7 @@ public class WebSocket : NSObject, StreamDelegate {
|
|||||||
}
|
}
|
||||||
if total >= offset {
|
if total >= offset {
|
||||||
if let queue = self?.queue, let callback = writeCompletion {
|
if let queue = self?.queue, let callback = writeCompletion {
|
||||||
queue.asynchronously() {
|
queue.async {
|
||||||
callback()
|
callback()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user