|
| 1 | +// |
| 2 | +// XMPPServiceDiscoveryTests.m |
| 3 | +// XMPPFrameworkTests |
| 4 | +// |
| 5 | +// Created by Andres Canal on 5/27/16. |
| 6 | +// |
| 7 | +// |
| 8 | + |
| 9 | +#import <XCTest/XCTest.h> |
| 10 | +#import "XMPPFramework/XMPPServiceDiscovery.h" |
| 11 | +#import "XMPPMockStream.h" |
| 12 | + |
| 13 | +@interface XMPPServiceDiscoveryTests : XCTestCase <XMPPServiceDiscoveryDelegate> |
| 14 | +@property (nonatomic, strong) XCTestExpectation *delegateExpectation; |
| 15 | +@end |
| 16 | + |
| 17 | +@implementation XMPPServiceDiscoveryTests |
| 18 | + |
| 19 | +- (void) testDiscoverInformationAbout{ |
| 20 | + self.delegateExpectation = [self expectationWithDescription:@"Information Response"]; |
| 21 | + |
| 22 | + XMPPMockStream *streamTest = [[XMPPMockStream alloc] init]; |
| 23 | + XMPPServiceDiscovery *serviceDiscovery = [[XMPPServiceDiscovery alloc] init]; |
| 24 | + [serviceDiscovery activate:streamTest]; |
| 25 | + [serviceDiscovery addDelegate:self delegateQueue:dispatch_get_main_queue()]; |
| 26 | + |
| 27 | + __weak typeof(XMPPMockStream) *weakStreamTest = streamTest; |
| 28 | + streamTest.elementReceived = ^void(NSXMLElement *element) { |
| 29 | + NSString *elementID = [element attributeForName:@"id"].stringValue; |
| 30 | + XMPPIQ *iq = [self fakeInfoIQWithID:elementID]; |
| 31 | + [weakStreamTest fakeIQResponse:iq]; |
| 32 | + }; |
| 33 | + |
| 34 | + [serviceDiscovery discoverInformationAboutJID:[XMPPJID jidWithString:@"test.com"]]; |
| 35 | + |
| 36 | + [self waitForExpectationsWithTimeout:10 handler:^(NSError * _Nullable error) { |
| 37 | + if(error){ |
| 38 | + XCTFail(@"Expectation Failed with error: %@", error); |
| 39 | + } |
| 40 | + }]; |
| 41 | +} |
| 42 | + |
| 43 | +- (void)xmppServiceDiscovery:(XMPPServiceDiscovery *)sender didDiscoverInformation:(NSArray *)items{ |
| 44 | + XCTAssertEqual(items.count, 9); |
| 45 | + [self.delegateExpectation fulfill]; |
| 46 | +} |
| 47 | + |
| 48 | +- (void) testDiscoverItems{ |
| 49 | + self.delegateExpectation = [self expectationWithDescription:@"Items Response"]; |
| 50 | + |
| 51 | + XMPPMockStream *streamTest = [[XMPPMockStream alloc] init]; |
| 52 | + XMPPServiceDiscovery *serviceDiscovery = [[XMPPServiceDiscovery alloc] init]; |
| 53 | + [serviceDiscovery activate:streamTest]; |
| 54 | + [serviceDiscovery addDelegate:self delegateQueue:dispatch_get_main_queue()]; |
| 55 | + |
| 56 | + __weak typeof(XMPPMockStream) *weakStreamTest = streamTest; |
| 57 | + streamTest.elementReceived = ^void(NSXMLElement *element) { |
| 58 | + NSString *elementID = [element attributeForName:@"id"].stringValue; |
| 59 | + XMPPIQ *iq = [self fakeItemIQWithID:elementID]; |
| 60 | + [weakStreamTest fakeIQResponse:iq]; |
| 61 | + }; |
| 62 | + |
| 63 | + [serviceDiscovery discoverItemsAssociatedWithJID:[XMPPJID jidWithString:@"test.com"]]; |
| 64 | + |
| 65 | + [self waitForExpectationsWithTimeout:10 handler:^(NSError * _Nullable error) { |
| 66 | + if(error){ |
| 67 | + XCTFail(@"Expectation Failed with error: %@", error); |
| 68 | + } |
| 69 | + }]; |
| 70 | +} |
| 71 | + |
| 72 | +- (void)xmppServiceDiscovery:(XMPPServiceDiscovery *)sender didDiscoverItems:(NSArray *)items{ |
| 73 | + XCTAssertEqual(items.count, 8); |
| 74 | + [self.delegateExpectation fulfill]; |
| 75 | +} |
| 76 | + |
| 77 | +- (void) testError{ |
| 78 | + self.delegateExpectation = [self expectationWithDescription:@"Error Response"]; |
| 79 | + |
| 80 | + XMPPMockStream *streamTest = [[XMPPMockStream alloc] init]; |
| 81 | + XMPPServiceDiscovery *serviceDiscovery = [[XMPPServiceDiscovery alloc] init]; |
| 82 | + [serviceDiscovery activate:streamTest]; |
| 83 | + [serviceDiscovery addDelegate:self delegateQueue:dispatch_get_main_queue()]; |
| 84 | + |
| 85 | + __weak typeof(XMPPMockStream) *weakStreamTest = streamTest; |
| 86 | + streamTest.elementReceived = ^void(NSXMLElement *element) { |
| 87 | + NSString *elementID = [element attributeForName:@"id"].stringValue; |
| 88 | + XMPPIQ *iq = [self fakeErrorWithID:elementID]; |
| 89 | + [weakStreamTest fakeIQResponse:iq]; |
| 90 | + }; |
| 91 | + |
| 92 | + [serviceDiscovery discoverItemsAssociatedWithJID:[XMPPJID jidWithString:@"test.com"]]; |
| 93 | + |
| 94 | + [self waitForExpectationsWithTimeout:10 handler:^(NSError * _Nullable error) { |
| 95 | + if(error){ |
| 96 | + XCTFail(@"Expectation Failed with error: %@", error); |
| 97 | + } |
| 98 | + }]; |
| 99 | +} |
| 100 | + |
| 101 | +- (XMPPIQ *)fakeErrorWithID:(NSString *) elementID{ |
| 102 | + NSMutableString *s = [NSMutableString string]; |
| 103 | + [s appendString:@"<iq type='error'"]; |
| 104 | + [s appendString:@" from='mim.shakespeare.lit'"]; |
| 105 | + [s appendString:@" to='[email protected]/orchard'"]; |
| 106 | + [s appendString:@" id='info3'>"]; |
| 107 | + [s appendString:@" <query xmlns='http://jabber.org/protocol/disco#info' "]; |
| 108 | + [s appendString:@" node='http://jabber.org/protocol/commands'/>"]; |
| 109 | + [s appendString:@" <error type='cancel'>"]; |
| 110 | + [s appendString:@" <not-allowed xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>"]; |
| 111 | + [s appendString:@" </error>"]; |
| 112 | + [s appendString:@"</iq>"]; |
| 113 | + |
| 114 | + NSError *error; |
| 115 | + NSXMLDocument *doc = [[NSXMLDocument alloc] initWithXMLString:s options:0 error:&error]; |
| 116 | + XMPPIQ *iq = [XMPPIQ iqFromElement:[doc rootElement]]; |
| 117 | + [iq addAttributeWithName:@"id" stringValue:elementID]; |
| 118 | + |
| 119 | + return iq; |
| 120 | +} |
| 121 | + |
| 122 | +- (void)xmppServiceDiscovery:(XMPPServiceDiscovery *)sender didFailToDiscover:(XMPPIQ *)iq{ |
| 123 | + XCTAssertNotNil([iq elementForName:@"error"]); |
| 124 | + [self.delegateExpectation fulfill]; |
| 125 | +} |
| 126 | + |
| 127 | + |
| 128 | +- (XMPPIQ *)fakeItemIQWithID:(NSString *) elementID{ |
| 129 | + NSMutableString *s = [NSMutableString string]; |
| 130 | + [s appendString:@"<iq type='result'"]; |
| 131 | + [s appendString:@" from='shakespeare.lit'"]; |
| 132 | + [s appendString:@" to='[email protected]/orchard'"]; |
| 133 | + [s appendString:@" id='items1'>"]; |
| 134 | + [s appendString:@" <query xmlns='http://jabber.org/protocol/disco#items'>"]; |
| 135 | + [s appendString:@" <item jid='people.shakespeare.lit'"]; |
| 136 | + [s appendString:@" name='Directory of Characters'/>"]; |
| 137 | + [s appendString:@" <item jid='plays.shakespeare.lit'"]; |
| 138 | + [s appendString:@" name='Play-Specific Chatrooms'/>"]; |
| 139 | + [s appendString:@" <item jid='mim.shakespeare.lit'"]; |
| 140 | + [s appendString:@" name='Gateway to Marlowe IM'/>"]; |
| 141 | + [s appendString:@" <item jid='words.shakespeare.lit'"]; |
| 142 | + [s appendString:@" name='Shakespearean Lexicon'/>"]; |
| 143 | + [s appendString:@" <item jid='globe.shakespeare.lit'"]; |
| 144 | + [s appendString:@" name='Calendar of Performances'/>"]; |
| 145 | + [s appendString:@" <item jid='headlines.shakespeare.lit'"]; |
| 146 | + [s appendString:@" name='Latest Shakespearean News'/>"]; |
| 147 | + [s appendString:@" <item jid='catalog.shakespeare.lit'"]; |
| 148 | + [s appendString:@" name='Buy Shakespeare Stuff!'/>"]; |
| 149 | + [s appendString:@" <item jid='en2fr.shakespeare.lit'"]; |
| 150 | + [s appendString:@" name='French Translation Service'/>"]; |
| 151 | + [s appendString:@" </query>"]; |
| 152 | + [s appendString:@"</iq>"]; |
| 153 | + |
| 154 | + NSError *error; |
| 155 | + NSXMLDocument *doc = [[NSXMLDocument alloc] initWithXMLString:s options:0 error:&error]; |
| 156 | + XMPPIQ *iq = [XMPPIQ iqFromElement:[doc rootElement]]; |
| 157 | + [iq addAttributeWithName:@"id" stringValue:elementID]; |
| 158 | + |
| 159 | + return iq; |
| 160 | +} |
| 161 | + |
| 162 | +- (XMPPIQ *)fakeInfoIQWithID:(NSString *) elementID{ |
| 163 | + |
| 164 | + NSMutableString *s = [NSMutableString string]; |
| 165 | + [s appendString: @"<iq type='result'"]; |
| 166 | + [s appendString: @" from='plays.shakespeare.lit'"]; |
| 167 | + [s appendString: @" to='[email protected]/orchard'"]; |
| 168 | + [s appendString: @" id='info1'>"]; |
| 169 | + [s appendString: @" <query xmlns='http://jabber.org/protocol/disco#info'>"]; |
| 170 | + [s appendString: @" <identity"]; |
| 171 | + [s appendString: @" category='conference'"]; |
| 172 | + [s appendString: @" type='text'"]; |
| 173 | + [s appendString: @" name='Play-Specific Chatrooms'/>"]; |
| 174 | + [s appendString: @" <identity"]; |
| 175 | + [s appendString: @" category='directory'"]; |
| 176 | + [s appendString: @" type='chatroom'"]; |
| 177 | + [s appendString: @" name='Play-Specific Chatrooms'/>"]; |
| 178 | + [s appendString: @" <feature var='http://jabber.org/protocol/disco#info'/>"]; |
| 179 | + [s appendString: @" <feature var='http://jabber.org/protocol/disco#items'/>"]; |
| 180 | + [s appendString: @" <feature var='http://jabber.org/protocol/muc'/>"]; |
| 181 | + [s appendString: @" <feature var='jabber:iq:register'/>"]; |
| 182 | + [s appendString: @" <feature var='jabber:iq:search'/>"]; |
| 183 | + [s appendString: @" <feature var='jabber:iq:time'/>"]; |
| 184 | + [s appendString: @" <feature var='jabber:iq:version'/>"]; |
| 185 | + [s appendString: @" </query>"]; |
| 186 | + [s appendString: @"</iq>"]; |
| 187 | + |
| 188 | + NSError *error; |
| 189 | + NSXMLDocument *doc = [[NSXMLDocument alloc] initWithXMLString:s options:0 error:&error]; |
| 190 | + XMPPIQ *iq = [XMPPIQ iqFromElement:[doc rootElement]]; |
| 191 | + [iq addAttributeWithName:@"id" stringValue:elementID]; |
| 192 | + |
| 193 | + return iq; |
| 194 | +} |
| 195 | + |
| 196 | +@end |
0 commit comments