Skip to content

Commit e787ed2

Browse files
committed
Update tests to work with all changes to NativePusher, delegate, and
dependency changes.
1 parent 87c020e commit e787ed2

File tree

7 files changed

+89
-69
lines changed

7 files changed

+89
-69
lines changed

Tests/AuthenticationTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import PusherSwift
1010
import XCTest
1111

1212
class AuthenticationTests: XCTestCase {
13-
class DummyDelegate: PusherConnectionDelegate {
13+
class DummyDelegate: PusherDelegate {
1414
var ex: XCTestExpectation? = nil
1515
var testingChannelName: String? = nil
1616

17-
func subscriptionDidSucceed(channelName: String) {
18-
if let cName = testingChannelName, cName == channelName {
17+
func subscribedToChannel(name: String) {
18+
if let cName = testingChannelName, cName == name {
1919
ex!.fulfill()
2020
}
2121
}
@@ -43,7 +43,7 @@ class AuthenticationTests: XCTestCase {
4343
let dummyDelegate = DummyDelegate()
4444
dummyDelegate.ex = ex
4545
dummyDelegate.testingChannelName = channelName
46-
pusher.connection.delegate = dummyDelegate
46+
pusher.delegate = dummyDelegate
4747

4848
if case .endpoint(authEndpoint: let authEndpoint) = pusher.connection.options.authMethod {
4949
let jsonData = "{\"auth\":\"testKey123:12345678gfder78ikjbg\"}".data(using: String.Encoding.utf8, allowLossyConversion: false)!
@@ -131,7 +131,7 @@ class AuthenticationTests: XCTestCase {
131131
authMethod: AuthMethod.authRequestBuilder(authRequestBuilder: AuthRequestBuilder())
132132
)
133133
pusher = Pusher(key: "testKey123", options: options)
134-
pusher.connection.delegate = dummyDelegate
134+
pusher.delegate = dummyDelegate
135135
socket.delegate = pusher.connection
136136
pusher.connection.socket = socket
137137

Tests/Mocks.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@ open class MockWebSocket: WebSocket {
3232
args: nil,
3333
functionToCall: {
3434
if let delegate = self.delegate {
35-
delegate.websocketDidReceiveMessage(self, text: connectionEstablishedString)
35+
delegate.websocketDidReceiveMessage(socket: self, text: connectionEstablishedString)
3636
} else {
3737
print("Your socket delegate is nil")
3838
}
3939
}
4040
)
4141
}
4242

43-
open override func disconnect(forceTimeout: TimeInterval? = nil) {
43+
open override func disconnect(forceTimeout: TimeInterval? = nil, closeCode: UInt16 = CloseCode.normal.rawValue) {
4444
let _ = stubber.stub(
4545
functionName: "disconnect",
4646
args: nil,
4747
functionToCall: {
48-
self.delegate?.websocketDidDisconnect(self, error: nil)
48+
self.delegate?.websocketDidDisconnect(socket: self, error: nil)
4949
}
5050
)
5151
}
@@ -56,39 +56,39 @@ open class MockWebSocket: WebSocket {
5656
functionName: "writeString",
5757
args: [string],
5858
functionToCall: {
59-
self.delegate?.websocketDidReceiveMessage(self, text: "{\"event\":\"pusher_internal:subscription_succeeded\",\"channel\":\"test-channel\",\"data\":\"{}\"}")
59+
self.delegate?.websocketDidReceiveMessage(socket: self, text: "{\"event\":\"pusher_internal:subscription_succeeded\",\"channel\":\"test-channel\",\"data\":\"{}\"}")
6060
}
6161
)
6262
} else if stringContainsElements(string, elements: ["testkey123:6aae8814fabd5285245422096705abbed64ea59614648814ffb0bf2dc5d19168", "private-channel", "pusher:subscribe"]) {
6363
let _ = stubber.stub(
6464
functionName: "writeString",
6565
args: [string],
6666
functionToCall: {
67-
self.delegate?.websocketDidReceiveMessage(self, text: "{\"event\":\"pusher_internal:subscription_succeeded\",\"channel\":\"private-channel\",\"data\":\"{}\"}")
67+
self.delegate?.websocketDidReceiveMessage(socket: self, text: "{\"event\":\"pusher_internal:subscription_succeeded\",\"channel\":\"private-channel\",\"data\":\"{}\"}")
6868
}
6969
)
7070
} else if stringContainsElements(string, elements: ["key:5ce61ee2b8594e22b66323913d7c7af9d8e815659365be3627733993f4ce3824", "presence-channel", "user_id", "45481.3166671", "pusher:subscribe"]) {
7171
let _ = stubber.stub(
7272
functionName: "writeString",
7373
args: [string],
7474
functionToCall: {
75-
self.delegate?.websocketDidReceiveMessage(self, text: "{\"event\":\"pusher_internal:subscription_succeeded\",\"data\":\"{\\\"presence\\\":{\\\"count\\\":1,\\\"ids\\\":[\\\"46123.486095\\\"],\\\"hash\\\":{\\\"46123.486095\\\":null}}}\",\"channel\":\"presence-channel\"}")
75+
self.delegate?.websocketDidReceiveMessage(socket: self, text: "{\"event\":\"pusher_internal:subscription_succeeded\",\"data\":\"{\\\"presence\\\":{\\\"count\\\":1,\\\"ids\\\":[\\\"46123.486095\\\"],\\\"hash\\\":{\\\"46123.486095\\\":null}}}\",\"channel\":\"presence-channel\"}")
7676
}
7777
)
7878
} else if stringContainsElements(string, elements: ["testkey123:5ce61ee2b8594e22b66323913d7c7af9d8e815659365be3627733993f4ce3824", "presence-channel", "user_id", "45481.3166671", "pusher:subscribe"]) {
7979
let _ = stubber.stub(
8080
functionName: "writeString",
8181
args: [string],
8282
functionToCall: {
83-
self.delegate?.websocketDidReceiveMessage(self, text: "{\"event\":\"pusher_internal:subscription_succeeded\",\"data\":\"{\\\"presence\\\":{\\\"count\\\":1,\\\"ids\\\":[\\\"46123.486095\\\"],\\\"hash\\\":{\\\"46123.486095\\\":null}}}\",\"channel\":\"presence-channel\"}")
83+
self.delegate?.websocketDidReceiveMessage(socket: self, text: "{\"event\":\"pusher_internal:subscription_succeeded\",\"data\":\"{\\\"presence\\\":{\\\"count\\\":1,\\\"ids\\\":[\\\"46123.486095\\\"],\\\"hash\\\":{\\\"46123.486095\\\":null}}}\",\"channel\":\"presence-channel\"}")
8484
}
8585
)
8686
} else if stringContainsElements(string, elements: ["key:e1d0947a10d6ff1a25990798910b2505687bb096e3e8b6c97eef02c6b1abb4c7", "private-channel", "pusher:subscribe"]) {
8787
let _ = stubber.stub(
8888
functionName: "writeString",
8989
args: [string],
9090
functionToCall: {
91-
self.delegate?.websocketDidReceiveMessage(self, text: "{\"event\":\"pusher_internal:subscription_succeeded\",\"channel\":\"private-channel\",\"data\":\"{}\"}")
91+
self.delegate?.websocketDidReceiveMessage(socket: self, text: "{\"event\":\"pusher_internal:subscription_succeeded\",\"channel\":\"private-channel\",\"data\":\"{}\"}")
9292
}
9393
)
9494
} else if stringContainsElements(string, elements: ["data", "testing client events", "private-channel", "client-test-event"]) {
@@ -102,15 +102,15 @@ open class MockWebSocket: WebSocket {
102102
functionName: "writeString",
103103
args: [string],
104104
functionToCall: {
105-
self.delegate?.websocketDidReceiveMessage(self, text: "{\"event\":\"pusher_internal:subscription_succeeded\",\"channel\":\"private-test-channel\",\"data\":\"{}\"}")
105+
self.delegate?.websocketDidReceiveMessage(socket: self, text: "{\"event\":\"pusher_internal:subscription_succeeded\",\"channel\":\"private-test-channel\",\"data\":\"{}\"}")
106106
}
107107
)
108108
} else if stringContainsElements(string, elements: ["key:0d0d2e7c2cd967246d808180ef0f115dad51979e48cac9ad203928141f9e6a6f", "private-test-channel", "pusher:subscribe"]) {
109109
let _ = stubber.stub(
110110
functionName: "writeString",
111111
args: [string],
112112
functionToCall: {
113-
self.delegate?.websocketDidReceiveMessage(self, text: "{\"event\":\"pusher_internal:subscription_succeeded\",\"channel\":\"private-test-channel\",\"data\":\"{}\"}")
113+
self.delegate?.websocketDidReceiveMessage(socket: self, text: "{\"event\":\"pusher_internal:subscription_succeeded\",\"channel\":\"private-test-channel\",\"data\":\"{}\"}")
114114
}
115115
)
116116
} else if stringContainsElements(string, elements: ["test-channel", "pusher:unsubscribe"]) {
@@ -124,23 +124,23 @@ open class MockWebSocket: WebSocket {
124124
functionName: "writeString",
125125
args: [string],
126126
functionToCall: {
127-
self.delegate?.websocketDidReceiveMessage(self, text: "{\"event\":\"pusher_internal:subscription_succeeded\",\"data\":\"{\\\"presence\\\":{\\\"count\\\":1,\\\"ids\\\":[\\\"123\\\"],\\\"hash\\\":{\\\"123\\\":{\\\"twitter\\\":\\\"hamchapman\\\"}}}}\",\"channel\":\"presence-test\"}")
127+
self.delegate?.websocketDidReceiveMessage(socket: self, text: "{\"event\":\"pusher_internal:subscription_succeeded\",\"data\":\"{\\\"presence\\\":{\\\"count\\\":1,\\\"ids\\\":[\\\"123\\\"],\\\"hash\\\":{\\\"123\\\":{\\\"twitter\\\":\\\"hamchapman\\\"}}}}\",\"channel\":\"presence-test\"}")
128128
}
129129
)
130130
} else if stringContainsElements(string, elements: ["key:c2b53f001321bc088814f210fb63c259b464f590890eee2dde6387ea9b469a30", "presence-channel", "user_id", "123", "pusher:subscribe"]) {
131131
let _ = stubber.stub(
132132
functionName: "writeString",
133133
args: [string],
134134
functionToCall: {
135-
self.delegate?.websocketDidReceiveMessage(self, text: "{\"event\":\"pusher_internal:subscription_succeeded\",\"data\":\"{\\\"presence\\\":{\\\"count\\\":1,\\\"ids\\\":[\\\"123\\\"],\\\"hash\\\":{\\\"123\\\":{}}}}\",\"channel\":\"presence-channel\"}")
135+
self.delegate?.websocketDidReceiveMessage(socket: self, text: "{\"event\":\"pusher_internal:subscription_succeeded\",\"data\":\"{\\\"presence\\\":{\\\"count\\\":1,\\\"ids\\\":[\\\"123\\\"],\\\"hash\\\":{\\\"123\\\":{}}}}\",\"channel\":\"presence-channel\"}")
136136
}
137137
)
138138
} else if stringContainsElements(string, elements: ["pusher:subscribe", "key:dd2885ee6dc6f5c964d8e3c720980397db50bf8f528e0630d4208bff80ee23f0", "presence-channel", "friends", "0", "user_id", "123"]) {
139139
let _ = stubber.stub(
140140
functionName: "writeString",
141141
args: [string],
142142
functionToCall: {
143-
self.delegate?.websocketDidReceiveMessage(self, text: "{\"event\":\"pusher_internal:subscription_succeeded\",\"data\":\"{\\\"presence\\\":{\\\"count\\\":1,\\\"ids\\\":[\\\"123\\\"],\\\"hash\\\":{\\\"123\\\":{\\\"friends\\\":0}}}}\",\"channel\":\"presence-channel\"}")
143+
self.delegate?.websocketDidReceiveMessage(socket: self, text: "{\"event\":\"pusher_internal:subscription_succeeded\",\"data\":\"{\\\"presence\\\":{\\\"count\\\":1,\\\"ids\\\":[\\\"123\\\"],\\\"hash\\\":{\\\"123\\\":{\\\"friends\\\":0}}}}\",\"channel\":\"presence-channel\"}")
144144
}
145145
)
146146
} else {

Tests/NativePusherTests.swift

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,26 @@
88

99
#if os(iOS)
1010

11-
import PusherSwift
11+
@testable import PusherSwift
1212
import XCTest
1313

14+
func setUpDefaultMockResponses(pusherClient: Pusher, deviceClientId: String) {
15+
let jsonData = "{\"id\":\"\(deviceClientId)\"}".data(using: String.Encoding.utf8, allowLossyConversion: false)!
16+
let url = URL(string: "https://nativepushclient-cluster1.pusher.com/client_api/v1/clients")!
17+
let urlResponse = HTTPURLResponse(url: url, statusCode: 201, httpVersion: nil, headerFields: nil)
18+
MockSession.addMockResponse(for: url, httpMethod: "POST", data: jsonData, urlResponse: urlResponse, error: nil)
19+
20+
let emptyJsonData = "".data(using: String.Encoding.utf8)!
21+
let subscriptionModificationUrl = URL(string: "https://nativepushclient-cluster1.pusher.com/client_api/v1/clients/\(deviceClientId)/interests/donuts")!
22+
let susbcriptionModificationResponse = HTTPURLResponse(url: subscriptionModificationUrl, statusCode: 204, httpVersion: nil, headerFields: nil)
23+
let httpMethodForSubscribe = "POST"
24+
MockSession.addMockResponse(for: subscriptionModificationUrl, httpMethod: httpMethodForSubscribe, data: emptyJsonData, urlResponse: susbcriptionModificationResponse, error: nil)
25+
let httpMethodForUnsubscribe = "DELETE"
26+
MockSession.addMockResponse(for: subscriptionModificationUrl, httpMethod: httpMethodForUnsubscribe, data: emptyJsonData, urlResponse: susbcriptionModificationResponse, error: nil)
27+
28+
pusherClient.nativePusher.URLSession = MockSession.shared
29+
}
30+
1431
class NativePusherTests: XCTestCase {
1532
public class DummyDelegate: PusherDelegate {
1633
public var testClientId: String? = nil
@@ -19,19 +36,19 @@ class NativePusherTests: XCTestCase {
1936
public var unsubscribeEx: XCTestExpectation? = nil
2037
public var interestName: String? = nil
2138

22-
public func didSubscribeToInterest(named name: String) {
39+
public func subscribedToInterest(name: String) {
2340
if interestName == name {
2441
subscribeEx!.fulfill()
2542
}
2643
}
2744

28-
public func didUnsubscribeFromInterest(named name: String) {
45+
public func unsubscribedFromInterest(name: String) {
2946
if interestName == name {
3047
unsubscribeEx!.fulfill()
3148
}
3249
}
3350

34-
public func didRegisterForPushNotifications(clientId: String) {
51+
public func registeredForPushNotifications(clientId: String) {
3552
XCTAssertEqual(clientId, testClientId)
3653
registerEx!.fulfill()
3754
}
@@ -59,33 +76,22 @@ class NativePusherTests: XCTestCase {
5976
pusher.connection.socket = socket
6077
dummyDelegate = DummyDelegate()
6178
pusher.delegate = dummyDelegate
62-
63-
let jsonData = "{\"id\":\"\(testClientId!)\"}".data(using: String.Encoding.utf8, allowLossyConversion: false)!
64-
let url = URL(string: "https://nativepushclient-cluster1.pusher.com/client_api/v1/clients")!
65-
let urlResponse = HTTPURLResponse(url: url, statusCode: 201, httpVersion: nil, headerFields: nil)
66-
MockSession.addMockResponse(for: url, httpMethod: "POST", data: jsonData, urlResponse: urlResponse, error: nil)
67-
68-
let emptyJsonData = "".data(using: String.Encoding.utf8)!
69-
let subscriptionModificationUrl = URL(string: "https://nativepushclient-cluster1.pusher.com/client_api/v1/clients/\(testClientId!)/interests/donuts")!
70-
let susbcriptionModificationResponse = HTTPURLResponse(url: subscriptionModificationUrl, statusCode: 204, httpVersion: nil, headerFields: nil)
71-
let httpMethodForSubscribe = "POST"
72-
MockSession.addMockResponse(for: subscriptionModificationUrl, httpMethod: httpMethodForSubscribe, data: emptyJsonData, urlResponse: susbcriptionModificationResponse, error: nil)
73-
let httpMethodForUnsubscribe = "DELETE"
74-
MockSession.addMockResponse(for: subscriptionModificationUrl, httpMethod: httpMethodForUnsubscribe, data: emptyJsonData, urlResponse: susbcriptionModificationResponse, error: nil)
75-
76-
pusher.nativePusher().URLSession = MockSession.shared
7779
}
7880

7981
func testReceivingAClientIdAfterRegisterIsCalled() {
82+
setUpDefaultMockResponses(pusherClient: pusher, deviceClientId: testClientId)
83+
8084
let ex = expectation(description: "the clientId should be received when registration succeeds")
8185
dummyDelegate.testClientId = testClientId
8286
dummyDelegate.registerEx = ex
8387

84-
pusher.nativePusher().register(deviceToken: "SOME_DEVICE_TOKEN".data(using: String.Encoding.utf8)!)
88+
pusher.nativePusher.register(deviceToken: "SOME_DEVICE_TOKEN".data(using: String.Encoding.utf8)!)
8589
waitForExpectations(timeout: 0.5)
8690
}
8791

8892
func testSubscribingToAnInterest() {
93+
setUpDefaultMockResponses(pusherClient: pusher, deviceClientId: testClientId)
94+
8995
let registerEx = expectation(description: "the clientId should be received when registration succeeds")
9096
let subscribeEx = expectation(description: "the client should successfully subscribe to an interest")
9197

@@ -94,13 +100,15 @@ class NativePusherTests: XCTestCase {
94100
dummyDelegate.registerEx = registerEx
95101
dummyDelegate.subscribeEx = subscribeEx
96102

97-
pusher.nativePusher().subscribe(interestName: "donuts")
98-
pusher.nativePusher().register(deviceToken: "SOME_DEVICE_TOKEN".data(using: String.Encoding.utf8)!)
103+
pusher.nativePusher.subscribe(interestName: "donuts")
104+
pusher.nativePusher.register(deviceToken: "SOME_DEVICE_TOKEN".data(using: String.Encoding.utf8)!)
99105

100106
waitForExpectations(timeout: 0.5)
101107
}
102108

103109
func testUnsubscribingFromAnInterest() {
110+
setUpDefaultMockResponses(pusherClient: pusher, deviceClientId: testClientId)
111+
104112
let registerEx = expectation(description: "the clientId should be received when registration succeeds")
105113
let subscribeEx = expectation(description: "the client should successfully subscribe to an interest")
106114
let unsubscribeEx = expectation(description: "the client should successfully unsubscribe from an interest")
@@ -110,12 +118,24 @@ class NativePusherTests: XCTestCase {
110118
dummyDelegate.subscribeEx = subscribeEx
111119
dummyDelegate.unsubscribeEx = unsubscribeEx
112120

113-
pusher.nativePusher().subscribe(interestName: "donuts")
114-
pusher.nativePusher().register(deviceToken: "SOME_DEVICE_TOKEN".data(using: String.Encoding.utf8)!)
115-
pusher.nativePusher().unsubscribe(interestName: "donuts")
121+
pusher.nativePusher.subscribe(interestName: "donuts")
122+
pusher.nativePusher.register(deviceToken: "SOME_DEVICE_TOKEN".data(using: String.Encoding.utf8)!)
123+
pusher.nativePusher.unsubscribe(interestName: "donuts")
116124

117125
waitForExpectations(timeout: 0.5)
118126
}
127+
128+
func testSubscribingWhenClientIdIsNotSetQueuesSubscriptionModificationRequest() {
129+
pusher.nativePusher.clientId = nil
130+
131+
XCTAssertEqual(pusher.nativePusher.requestQueue.count, 0, "the nativePusher request queue should be empty")
132+
pusher.nativePusher.subscribe(interestName: "donuts")
133+
134+
Thread.sleep(forTimeInterval: 0.5)
135+
136+
XCTAssertEqual(pusher.nativePusher.requestQueue.count, 1, "the nativePusher request queue should contain the subscribe request")
137+
XCTAssertEqual(pusher.nativePusher.requestQueue.paused, true, "the nativePusher request queue should be paused")
138+
}
119139
}
120140

121141
#endif

0 commit comments

Comments
 (0)