/
PrivateChannelsSpec.m
126 lines (92 loc) · 3.37 KB
/
PrivateChannelsSpec.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
// PrivateChannelsSpec.m
// libPusher
//
// Created by Luke Redpath on 29/02/2012.
// Copyright 2012 LJR Software Limited. All rights reserved.
//
#import "SpecHelper.h"
#import "NSMutableURLRequest+BasicAuth.h"
SPEC_BEGIN(PrivateChannelsSpec)
describe(@"Subscribing to a private channel", ^{
__block PTPusher *client = nil;
registerMatchers(@"PT");
enableClientDebugging();
beforeAll(^{
client = newTestClient();
// this requires the auth server to be running (rake authserver:start)
client.authorizationURL = [NSURL URLWithString:@"http://localhost:9292/private/auth"];
});
afterAll(^{
[client disconnect];
waitForClientToDisconnect(client);
});
it(@"succeeds when authorization is successful", ^{
onAuthorizationRequired(^(NSMutableURLRequest *authRequest) {
[authRequest setHTTPBasicAuthUsername:@"admin" password:@"letmein"];
});
__block PTPusherPrivateChannel *channel = nil;
onConnect(^{
channel = [client subscribeToPrivateChannelNamed:@"secret-channel"];
});
__block BOOL subscribed = NO;
onSubscribe(^(PTPusherChannel *subscribedChannel) {
if (subscribedChannel == (PTPusherChannel *)channel) {
subscribed = YES;
}
});
[[expectFutureValue(theValue(subscribed)) shouldEventuallyBeforeTimingOutAfter(3)] beYes];
});
it(@"fails when authorization fails", ^{
onAuthorizationRequired(^(NSMutableURLRequest *authRequest) {
[authRequest setHTTPBasicAuthUsername:@"admin" password:@"wrongpassword"];
});
__block PTPusherPrivateChannel *channel = nil;
onConnect(^{
channel = [client subscribeToPrivateChannelNamed:@"secret-channel"];
});
__block BOOL failedToSubscribe = NO;
onFailedToSubscribe(^(PTPusherChannel *failedChannel) {
if (failedChannel == (PTPusherChannel *)channel) {
failedToSubscribe = YES;
}
});
[[expectFutureValue(theValue(failedToSubscribe)) shouldEventuallyBeforeTimingOutAfter(3)] beYes];
});
});
describe(@"Subscribing to multiple private channels with delays (see issue #26)", ^{
__block PTPusher *client = nil;
registerMatchers(@"PT");
enableClientDebugging();
beforeAll(^{
client = newTestClient();
// this requires the auth server to be running (rake authserver:start)
client.authorizationURL = [NSURL URLWithString:@"http://localhost:9292/private/slowauth"];
});
afterAll(^{
[client disconnect];
waitForClientToDisconnect(client);
});
it(@"should be successful", ^{
onAuthorizationRequired(^(NSMutableURLRequest *authRequest) {
NSLog(@"AUTHORIZING REQUEST");
[authRequest setHTTPBasicAuthUsername:@"admin" password:@"letmein"];
});
NSInteger numberToConnect = 5;
onConnect(^{
for (int i = 0; i < numberToConnect; i++) {
[client subscribeToPrivateChannelNamed:[NSString stringWithFormat:@"secret-channel-%d", i]];
}
});
__block NSInteger numberOfSubscribedChannels = 0;
__block BOOL allSubscribed = NO;
onSubscribe(^(PTPusherChannel *subscribedChannel) {
numberOfSubscribedChannels++;
if (numberOfSubscribedChannels == numberToConnect) {
allSubscribed = YES;
}
});
[[expectFutureValue(theValue(allSubscribed)) shouldEventuallyBeforeTimingOutAfter(10)] beYes];
});
});
SPEC_END