diff --git a/README.md b/README.md index 2da839bc..feef033c 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Test-driven from Swift and implemented in Objective-C, to avoid burdening Object - [x] PKCS12 client certificates using the [TLS auth mechanism plugin](https://github.com/rabbitmq/rabbitmq-auth-mechanism-ssl) - [ ] [PKCS12 client certificates using chained CAs](https://github.com/rabbitmq/rabbitmq-objc-client/issues/74) - [ ] [Publisher confirmations](https://github.com/rabbitmq/rabbitmq-objc-client/issues/68) -- [ ] [Publish and consume messages as data](https://github.com/rabbitmq/rabbitmq-objc-client/issues/46) +- [x] [Publish and consume messages as data](https://github.com/rabbitmq/rabbitmq-objc-client/issues/46) - [ ] [Connection closure when broker doesn't send heartbeats fast enough](https://github.com/rabbitmq/rabbitmq-objc-client/issues/41) - [ ] [Customisable consumer hooks](https://github.com/rabbitmq/rabbitmq-objc-client/issues/71) - [ ] [basic.return support](https://github.com/rabbitmq/rabbitmq-objc-client/issues/72) @@ -90,9 +90,9 @@ this list. ```swift let q = ch.queue("myqueue") q.subscribe { m in - print("Received: \(m.content)") + print("Received: \(m.body)") } - q.publish("foo") + q.publish("foo".dataUsingEncoding(NSUTF8StringEncoding)!) ``` 1. Close the connection when done: diff --git a/RMQClient/RMQAllocatedChannel.m b/RMQClient/RMQAllocatedChannel.m index d10510d5..bee9277b 100644 --- a/RMQClient/RMQAllocatedChannel.m +++ b/RMQClient/RMQAllocatedChannel.m @@ -242,7 +242,7 @@ - (void)basicCancel:(NSString *)consumerTag { options:RMQBasicCancelNoOptions]]; } -- (void)basicPublish:(NSString *)message +- (void)basicPublish:(NSData *)body routingKey:(NSString *)routingKey exchange:(NSString *)exchange properties:(NSArray *)properties @@ -251,8 +251,7 @@ - (void)basicPublish:(NSString *)message exchange:[[RMQShortstr alloc] init:exchange] routingKey:[[RMQShortstr alloc] init:routingKey] options:options]; - NSData *contentBodyData = [message dataUsingEncoding:NSUTF8StringEncoding]; - RMQContentBody *contentBody = [[RMQContentBody alloc] initWithData:contentBodyData]; + RMQContentBody *contentBody = [[RMQContentBody alloc] initWithData:body]; NSData *bodyData = contentBody.amqEncoded; @@ -282,15 +281,13 @@ - (void)basicGet:(NSString *)queue options:options] completionHandler:^(RMQFrameset *frameset) { RMQBasicGetOk *getOk = (RMQBasicGetOk *)frameset.method; - NSString *messageContent = [[NSString alloc] initWithData:frameset.contentData - encoding:NSUTF8StringEncoding]; - RMQMessage *message = [[RMQMessage alloc] initWithContent:messageContent - consumerTag:@"" - deliveryTag:@(getOk.deliveryTag.integerValue) - redelivered:getOk.options & RMQBasicGetOkRedelivered - exchangeName:getOk.exchange.stringValue - routingKey:getOk.routingKey.stringValue - properties:frameset.contentHeader.properties]; + RMQMessage *message = [[RMQMessage alloc] initWithBody:frameset.contentData + consumerTag:@"" + deliveryTag:@(getOk.deliveryTag.integerValue) + redelivered:getOk.options & RMQBasicGetOkRedelivered + exchangeName:getOk.exchange.stringValue + routingKey:getOk.routingKey.stringValue + properties:frameset.contentHeader.properties]; userCompletionHandler(message); }]; } @@ -439,16 +436,15 @@ - (void)handleConfirmation:(RMQFrameset *)frameset { - (void)handleBasicDeliver:(RMQFrameset *)frameset { RMQBasicDeliver *deliver = (RMQBasicDeliver *)frameset.method; - NSString *content = [[NSString alloc] initWithData:frameset.contentData encoding:NSUTF8StringEncoding]; RMQConsumer *consumer = self.consumers[deliver.consumerTag.stringValue]; if (consumer) { - RMQMessage *message = [[RMQMessage alloc] initWithContent:content - consumerTag:deliver.consumerTag.stringValue - deliveryTag:@(deliver.deliveryTag.integerValue) - redelivered:deliver.options & RMQBasicDeliverRedelivered - exchangeName:deliver.exchange.stringValue - routingKey:deliver.routingKey.stringValue - properties:frameset.contentHeader.properties]; + RMQMessage *message = [[RMQMessage alloc] initWithBody:frameset.contentData + consumerTag:deliver.consumerTag.stringValue + deliveryTag:@(deliver.deliveryTag.integerValue) + redelivered:deliver.options & RMQBasicDeliverRedelivered + exchangeName:deliver.exchange.stringValue + routingKey:deliver.routingKey.stringValue + properties:frameset.contentHeader.properties]; consumer.handler(message); } } diff --git a/RMQClient/RMQChannel.h b/RMQClient/RMQChannel.h index 3883125e..433ba9ab 100644 --- a/RMQClient/RMQChannel.h +++ b/RMQClient/RMQChannel.h @@ -97,7 +97,7 @@ - (void)basicCancel:(nonnull NSString *)consumerTag; -- (void)basicPublish:(nonnull NSString *)message +- (void)basicPublish:(nonnull NSData *)body routingKey:(nonnull NSString *)routingKey exchange:(nonnull NSString *)exchange properties:(nonnull NSArray *)properties diff --git a/RMQClient/RMQExchange.h b/RMQClient/RMQExchange.h index fbf720a8..ce86e657 100644 --- a/RMQClient/RMQExchange.h +++ b/RMQClient/RMQExchange.h @@ -72,19 +72,19 @@ - (void)unbind:(RMQExchange *)source; - (void)delete:(RMQExchangeDeleteOptions)options; - (void)delete; -- (void)publish:(NSString *)message +- (void)publish:(NSData *)body routingKey:(NSString *)routingKey properties:(NSArray *> *)properties options:(RMQBasicPublishOptions)options; -- (void)publish:(NSString *)message +- (void)publish:(NSData *)body routingKey:(NSString *)key persistent:(BOOL)isPersistent options:(RMQBasicPublishOptions)options; -- (void)publish:(NSString *)message +- (void)publish:(NSData *)body routingKey:(NSString *)key persistent:(BOOL)isPersistent; -- (void)publish:(NSString *)message +- (void)publish:(NSData *)body routingKey:(NSString *)key; -- (void)publish:(NSString *)message; +- (void)publish:(NSData *)body; @end diff --git a/RMQClient/RMQExchange.m b/RMQClient/RMQExchange.m index 15f164dc..f4a8cfc8 100644 --- a/RMQClient/RMQExchange.m +++ b/RMQClient/RMQExchange.m @@ -106,18 +106,18 @@ - (void)delete { [self delete:RMQExchangeDeleteNoOptions]; } -- (void)publish:(NSString *)message +- (void)publish:(NSData *)body routingKey:(NSString *)routingKey properties:(NSArray *> *)properties options:(RMQBasicPublishOptions)options { - [self.channel basicPublish:message + [self.channel basicPublish:body routingKey:routingKey exchange:self.name properties:properties options:options]; } -- (void)publish:(NSString *)message +- (void)publish:(NSData *)body routingKey:(NSString *)key persistent:(BOOL)isPersistent options:(RMQBasicPublishOptions)options { @@ -125,31 +125,31 @@ - (void)publish:(NSString *)message if (isPersistent) { [properties addObject:[[RMQBasicDeliveryMode alloc] init:2]]; } - [self.channel basicPublish:message + [self.channel basicPublish:body routingKey:key exchange:self.name properties:properties options:options]; } -- (void)publish:(NSString *)message +- (void)publish:(NSData *)body routingKey:(NSString *)key persistent:(BOOL)isPersistent { - [self publish:message + [self publish:body routingKey:key persistent:isPersistent options:RMQBasicPublishNoOptions]; } -- (void)publish:(NSString *)message +- (void)publish:(NSData *)body routingKey:(NSString *)key { - [self publish:message + [self publish:body routingKey:key persistent:NO]; } -- (void)publish:(NSString *)message { - [self publish:message +- (void)publish:(NSData *)body { + [self publish:body routingKey:@""]; } diff --git a/RMQClient/RMQMessage.h b/RMQClient/RMQMessage.h index 76583b99..cb2b0b93 100644 --- a/RMQClient/RMQMessage.h +++ b/RMQClient/RMQMessage.h @@ -53,7 +53,7 @@ #import "RMQBasicProperties.h" @interface RMQMessage : RMQValue -@property (nonatomic, readonly) NSString *content; +@property (nonatomic, readonly) NSData *body; @property (nonatomic, readonly) NSString *consumerTag; @property (nonatomic, readonly) NSNumber *deliveryTag; @property (nonatomic, readonly) BOOL isRedelivered; @@ -61,13 +61,13 @@ @property (nonatomic, readonly) NSString *routingKey; @property (nonatomic, readonly) NSArray *properties; -- (instancetype)initWithContent:(NSString *)content - consumerTag:(NSString *)consumerTag - deliveryTag:(NSNumber *)deliveryTag - redelivered:(BOOL)isRedelivered - exchangeName:(NSString *)exchangeName - routingKey:(NSString *)routingKey - properties:(NSArray *> *)properties; +- (instancetype)initWithBody:(NSData *)body + consumerTag:(NSString *)consumerTag + deliveryTag:(NSNumber *)deliveryTag + redelivered:(BOOL)isRedelivered + exchangeName:(NSString *)exchangeName + routingKey:(NSString *)routingKey + properties:(NSArray *> *)properties; - (NSString *)appID; - (NSString *)contentType; diff --git a/RMQClient/RMQMessage.m b/RMQClient/RMQMessage.m index 05e1cc8a..1fee1561 100644 --- a/RMQClient/RMQMessage.m +++ b/RMQClient/RMQMessage.m @@ -52,7 +52,7 @@ #import "RMQMessage.h" @interface RMQMessage () -@property (nonatomic, readwrite) NSString *content; +@property (nonatomic, readwrite) NSData *body; @property (nonatomic, readwrite) NSString *consumerTag; @property (nonatomic, readwrite) NSNumber *deliveryTag; @property (nonatomic, readwrite) BOOL isRedelivered; @@ -63,16 +63,16 @@ @interface RMQMessage () @implementation RMQMessage -- (instancetype)initWithContent:(NSString *)content - consumerTag:(NSString *)consumerTag - deliveryTag:(NSNumber *)deliveryTag - redelivered:(BOOL)isRedelivered - exchangeName:(NSString *)exchangeName - routingKey:(NSString *)routingKey - properties:(NSArray *> *)properties { +- (instancetype)initWithBody:(NSData *)body + consumerTag:(NSString *)consumerTag + deliveryTag:(NSNumber *)deliveryTag + redelivered:(BOOL)isRedelivered + exchangeName:(NSString *)exchangeName + routingKey:(NSString *)routingKey + properties:(NSArray *> *)properties { self = [super init]; if (self) { - self.content = content; + self.body = body; self.consumerTag = consumerTag; self.deliveryTag = deliveryTag; self.isRedelivered = isRedelivered; diff --git a/RMQClient/RMQQueue.h b/RMQClient/RMQQueue.h index 55b7bc57..d6b17c51 100644 --- a/RMQClient/RMQQueue.h +++ b/RMQClient/RMQQueue.h @@ -75,15 +75,15 @@ - (void)unbind:(RMQExchange *)exchange; - (void)delete:(RMQQueueDeleteOptions)options; - (void)delete; -- (void)publish:(NSString *)message +- (void)publish:(NSData *)body properties:(NSArray *> *)properties options:(RMQBasicPublishOptions)options; -- (void)publish:(NSString *)message +- (void)publish:(NSData *)body persistent:(BOOL)isPersistent options:(RMQBasicPublishOptions)options; -- (void)publish:(NSString *)message +- (void)publish:(NSData *)body persistent:(BOOL)isPersistent; -- (void)publish:(NSString *)message; +- (void)publish:(NSData *)body; - (void)pop:(RMQConsumerDeliveryHandler)handler; - (RMQConsumer *)subscribe:(RMQConsumerDeliveryHandler)handler; - (RMQConsumer *)subscribe:(RMQBasicConsumeOptions)options diff --git a/RMQClient/RMQQueue.m b/RMQClient/RMQQueue.m index 7dc6ab87..a560d37f 100644 --- a/RMQClient/RMQQueue.m +++ b/RMQClient/RMQQueue.m @@ -104,37 +104,37 @@ - (void)delete { [self delete:RMQQueueDeleteNoOptions]; } -- (void)publish:(NSString *)message +- (void)publish:(NSData *)data properties:(NSArray *> *)properties options:(RMQBasicPublishOptions)options { - [self.channel basicPublish:message + [self.channel basicPublish:data routingKey:self.name exchange:@"" properties:properties options:options]; } -- (void)publish:(NSString *)message +- (void)publish:(NSData *)body persistent:(BOOL)isPersistent options:(RMQBasicPublishOptions)options { NSMutableArray *properties = [NSMutableArray new]; if (isPersistent) { [properties addObject:[[RMQBasicDeliveryMode alloc] init:2]]; } - [self.channel basicPublish:message + [self.channel basicPublish:body routingKey:self.name exchange:@"" properties:properties options:options]; } -- (void)publish:(NSString *)message +- (void)publish:(NSData *)body persistent:(BOOL)isPersistent { - [self publish:message persistent:isPersistent options:RMQBasicPublishNoOptions]; + [self publish:body persistent:isPersistent options:RMQBasicPublishNoOptions]; } -- (void)publish:(NSString *)message { - [self publish:message persistent:NO]; +- (void)publish:(NSData *)body { + [self publish:body persistent:NO]; } - (void)pop:(RMQConsumerDeliveryHandler)handler { diff --git a/RMQClientIntegrationTests/ConnectionRecoveryIntegrationTest.swift b/RMQClientIntegrationTests/ConnectionRecoveryIntegrationTest.swift index adbda23f..d38a3768 100644 --- a/RMQClientIntegrationTests/ConnectionRecoveryIntegrationTest.swift +++ b/RMQClientIntegrationTests/ConnectionRecoveryIntegrationTest.swift @@ -26,7 +26,7 @@ // Version 1.1 (the "License"); you may not use this file except in // compliance with the License. You may obtain a copy of the License at // https://www.mozilla.org/MPL/ -// +// // Software distributed under the License is distributed on an "AS IS" // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the // License for the specific language governing rights and limitations @@ -89,7 +89,7 @@ class ConnectionRecoveryIntegrationTest: XCTestCase { ch.confirmSelect() - ex1.publish("before close") + ex1.publish("before close".dataUsingEncoding(NSUTF8StringEncoding)) XCTAssertEqual(0, dispatch_semaphore_wait(consumerSemaphore, TestHelper.dispatchTimeFromNow(semaphoreTimeout)), "Timed out waiting for message") @@ -98,9 +98,9 @@ class ConnectionRecoveryIntegrationTest: XCTestCase { XCTAssert(TestHelper.pollUntil { delegate.recoveredConnection != nil }, "Didn't finish recovery") - q.publish("after close 1") + q.publish("after close 1".dataUsingEncoding(NSUTF8StringEncoding)) dispatch_semaphore_wait(consumerSemaphore, TestHelper.dispatchTimeFromNow(semaphoreTimeout)) - ex1.publish("after close 2") + ex1.publish("after close 2".dataUsingEncoding(NSUTF8StringEncoding)) dispatch_semaphore_wait(consumerSemaphore, TestHelper.dispatchTimeFromNow(semaphoreTimeout)) var acks: Set? @@ -111,7 +111,9 @@ class ConnectionRecoveryIntegrationTest: XCTestCase { dispatch_semaphore_signal(confirmSemaphore) } - XCTAssertEqual(["before close", "after close 1", "after close 2"], messages.map { $0.content }) + XCTAssertEqual("before close".dataUsingEncoding(NSUTF8StringEncoding), messages[0].body) + XCTAssertEqual("after close 1".dataUsingEncoding(NSUTF8StringEncoding), messages[1].body) + XCTAssertEqual("after close 2".dataUsingEncoding(NSUTF8StringEncoding), messages[2].body) XCTAssertEqual(0, dispatch_semaphore_wait(confirmSemaphore, TestHelper.dispatchTimeFromNow(semaphoreTimeout))) XCTAssert(acks!.union(nacks!).isSupersetOf([2, 3]), @@ -119,10 +121,10 @@ class ConnectionRecoveryIntegrationTest: XCTestCase { // test recovery of queue arguments - in this case, x-max-length consumer.cancel() - q.publish("4") - q.publish("5") - q.publish("6") - q.publish("7") + q.publish("4".dataUsingEncoding(NSUTF8StringEncoding)) + q.publish("5".dataUsingEncoding(NSUTF8StringEncoding)) + q.publish("6".dataUsingEncoding(NSUTF8StringEncoding)) + q.publish("7".dataUsingEncoding(NSUTF8StringEncoding)) var messagesPostCancel: [RMQMessage] = [] q.subscribe { m in @@ -133,7 +135,9 @@ class ConnectionRecoveryIntegrationTest: XCTestCase { for _ in 5...7 { XCTAssertEqual(0, dispatch_semaphore_wait(consumerSemaphore, TestHelper.dispatchTimeFromNow(semaphoreTimeout))) } - XCTAssertEqual(["5", "6", "7"], messagesPostCancel.map { $0.content }) + XCTAssertEqual("5".dataUsingEncoding(NSUTF8StringEncoding), messagesPostCancel[0].body) + XCTAssertEqual("6".dataUsingEncoding(NSUTF8StringEncoding), messagesPostCancel[1].body) + XCTAssertEqual("7".dataUsingEncoding(NSUTF8StringEncoding), messagesPostCancel[2].body) } func testReenablesConsumersOnEachRecoveryFromConnectionClose() { @@ -167,7 +171,7 @@ class ConnectionRecoveryIntegrationTest: XCTestCase { dispatch_semaphore_signal(semaphore) } - ex.publish("before close") + ex.publish("before close".dataUsingEncoding(NSUTF8StringEncoding)) XCTAssertEqual(0, dispatch_semaphore_wait(semaphore, TestHelper.dispatchTimeFromNow(semaphoreTimeout)), "Timed out waiting for message") @@ -177,24 +181,30 @@ class ConnectionRecoveryIntegrationTest: XCTestCase { "Didn't finish recovery the first time") delegate.recoveredConnection = nil - q.publish("after close 1") + q.publish("after close 1".dataUsingEncoding(NSUTF8StringEncoding)) dispatch_semaphore_wait(semaphore, TestHelper.dispatchTimeFromNow(semaphoreTimeout)) - ex.publish("after close 2") + ex.publish("after close 2".dataUsingEncoding(NSUTF8StringEncoding)) dispatch_semaphore_wait(semaphore, TestHelper.dispatchTimeFromNow(semaphoreTimeout)) - XCTAssertEqual(["before close", "after close 1", "after close 2"], messages.map { $0.content }) + XCTAssertEqual("before close".dataUsingEncoding(NSUTF8StringEncoding), messages[0].body) + XCTAssertEqual("after close 1".dataUsingEncoding(NSUTF8StringEncoding), messages[1].body) + XCTAssertEqual("after close 2".dataUsingEncoding(NSUTF8StringEncoding), messages[2].body) try! closeAllConnections() XCTAssert(TestHelper.pollUntil { delegate.recoveredConnection != nil }, "Didn't finish recovery the second time") - q.publish("after close 3") + q.publish("after close 3".dataUsingEncoding(NSUTF8StringEncoding)) dispatch_semaphore_wait(semaphore, TestHelper.dispatchTimeFromNow(semaphoreTimeout)) - ex.publish("after close 4") + ex.publish("after close 4".dataUsingEncoding(NSUTF8StringEncoding)) dispatch_semaphore_wait(semaphore, TestHelper.dispatchTimeFromNow(semaphoreTimeout)) - XCTAssertEqual(["before close", "after close 1", "after close 2", "after close 3", "after close 4"], messages.map { $0.content }) + XCTAssertEqual("before close".dataUsingEncoding(NSUTF8StringEncoding), messages[0].body) + XCTAssertEqual("after close 1".dataUsingEncoding(NSUTF8StringEncoding), messages[1].body) + XCTAssertEqual("after close 2".dataUsingEncoding(NSUTF8StringEncoding), messages[2].body) + XCTAssertEqual("after close 3".dataUsingEncoding(NSUTF8StringEncoding), messages[3].body) + XCTAssertEqual("after close 4".dataUsingEncoding(NSUTF8StringEncoding), messages[4].body) } private func connections() -> [RMQHTTPConnection] { diff --git a/RMQClientIntegrationTests/IntegrationTests.swift b/RMQClientIntegrationTests/IntegrationTests.swift index 49a113b5..b0cecb76 100644 --- a/RMQClientIntegrationTests/IntegrationTests.swift +++ b/RMQClientIntegrationTests/IntegrationTests.swift @@ -88,11 +88,13 @@ class IntegrationTests: XCTestCase { dst.bind(src) q.bind(dst) - src.publish(messageContent) + let body = messageContent.dataUsingEncoding(NSUTF8StringEncoding)! + + src.publish(body) let semaphore = dispatch_semaphore_create(0) let expected = RMQMessage( - content: messageContent, + body: body, consumerTag: "", deliveryTag: 1, redelivered: false, @@ -146,14 +148,16 @@ class IntegrationTests: XCTestCase { dispatch_semaphore_signal(semaphore) } - q.publish("my message") + let body = "my message".dataUsingEncoding(NSUTF8StringEncoding)! + + q.publish(body) XCTAssertEqual(0, dispatch_semaphore_wait(semaphore, TestHelper.dispatchTimeFromNow(10)), "Timed out waiting for message") XCTAssertEqual(1, delivered!.deliveryTag) - XCTAssertEqual("my message", delivered!.content) + XCTAssertEqual(body, delivered!.body) } func testMessageProperties() { @@ -202,7 +206,7 @@ class IntegrationTests: XCTestCase { RMQBasicCorrelationId("r-1"), RMQBasicMessageId("m-1"), ] - q.publish("a message", properties: props, options: []) + q.publish("a message".dataUsingEncoding(NSUTF8StringEncoding), properties: props, options: []) XCTAssertEqual(0, dispatch_semaphore_wait(semaphore, TestHelper.dispatchTimeFromNow(10)), @@ -249,7 +253,7 @@ class IntegrationTests: XCTestCase { } } - ch.defaultExchange().publish("my message", routingKey: q.name) + ch.defaultExchange().publish("my message".dataUsingEncoding(NSUTF8StringEncoding), routingKey: q.name) XCTAssertEqual(0, dispatch_semaphore_wait(semaphore, TestHelper.dispatchTimeFromNow(10)), @@ -300,7 +304,7 @@ class IntegrationTests: XCTestCase { let producingQueue = producingChannel.queue(consumingQueue.name, options: [.AutoDelete, .Exclusive]) for _ in 1...messageCount { - producingQueue.publish("hello") + producingQueue.publish("hello".dataUsingEncoding(NSUTF8StringEncoding)) } XCTAssertEqual(0, @@ -346,7 +350,7 @@ class IntegrationTests: XCTestCase { } for _ in 1...messageCount { - producingQueue.publish("hello") + producingQueue.publish("hello".dataUsingEncoding(NSUTF8StringEncoding)) } XCTAssertEqual( @@ -393,6 +397,6 @@ class IntegrationTests: XCTestCase { } private func causeServerChannelClose(ch: RMQChannel) { - ch.basicPublish("", routingKey: "a route that can't be found", exchange: "a non-existent exchange", properties: [], options: []) + ch.basicPublish("".dataUsingEncoding(NSUTF8StringEncoding)!, routingKey: "a route that can't be found", exchange: "a non-existent exchange", properties: [], options: []) } } diff --git a/RMQClientIntegrationTests/PublisherConfirmationIntegrationTest.swift b/RMQClientIntegrationTests/PublisherConfirmationIntegrationTest.swift index 5b281843..055a6f68 100644 --- a/RMQClientIntegrationTests/PublisherConfirmationIntegrationTest.swift +++ b/RMQClientIntegrationTests/PublisherConfirmationIntegrationTest.swift @@ -64,8 +64,8 @@ class PublisherConfirmationIntegrationTest: XCTestCase { let q = ch.queue("", options: [.AutoDelete, .Exclusive]) - q.publish("message a") - q.publish("message b") + q.publish("message a".dataUsingEncoding(NSUTF8StringEncoding)) + q.publish("message b".dataUsingEncoding(NSUTF8StringEncoding)) var acked: Set = [] var nacked: Set = [] diff --git a/RMQClientTests/ChannelSpy.swift b/RMQClientTests/ChannelSpy.swift index 65a0c369..d3ba5a71 100644 --- a/RMQClientTests/ChannelSpy.swift +++ b/RMQClientTests/ChannelSpy.swift @@ -61,7 +61,7 @@ var lastReceivedBasicGetOptions: RMQBasicGetOptions? var lastReceivedBasicGetCompletionHandler: RMQConsumerDeliveryHandler? - var lastReceivedBasicPublishMessage: String? + var lastReceivedBasicPublishMessage: NSData? var lastReceivedBasicPublishRoutingKey: String? var lastReceivedBasicPublishExchange: String? var lastReceivedBasicPublishProperties: Array? @@ -217,8 +217,8 @@ lastReceivedBasicCancelConsumerTag = consumerTag } - func basicPublish(message: String, routingKey: String, exchange: String, properties: [RMQValue], options: RMQBasicPublishOptions) { - lastReceivedBasicPublishMessage = message + func basicPublish(body: NSData, routingKey: String, exchange: String, properties: [RMQValue], options: RMQBasicPublishOptions) { + lastReceivedBasicPublishMessage = body lastReceivedBasicPublishRoutingKey = routingKey lastReceivedBasicPublishExchange = exchange lastReceivedBasicPublishProperties = properties diff --git a/RMQClientTests/ConsumeTest.swift b/RMQClientTests/ConsumeTest.swift index a0bc4b1f..7a436d30 100644 --- a/RMQClientTests/ConsumeTest.swift +++ b/RMQClientTests/ConsumeTest.swift @@ -90,7 +90,7 @@ class ConsumeTest: XCTestCase { exchange: "my-exchange", options: [.Redelivered] ) - let expectedMessage = RMQMessage(content: "Consumed!", + let expectedMessage = RMQMessage(body: "Consumed!".dataUsingEncoding(NSUTF8StringEncoding), consumerTag: "tag", deliveryTag: 456, redelivered: true, diff --git a/RMQClientTests/PublisherConfirmationTest.swift b/RMQClientTests/PublisherConfirmationTest.swift index b21b4581..cd6db969 100644 --- a/RMQClientTests/PublisherConfirmationTest.swift +++ b/RMQClientTests/PublisherConfirmationTest.swift @@ -92,7 +92,7 @@ class PublisherConfirmationTest: XCTestCase { let ch = ChannelHelper.makeChannel(1, confirmations: confirmations) XCTAssertEqual(0, confirmations.publicationCount) - ch.basicPublish("hi there", routingKey: "", exchange: "", properties: [], options: []) + ch.basicPublish("hi there".dataUsingEncoding(NSUTF8StringEncoding)!, routingKey: "", exchange: "", properties: [], options: []) XCTAssertEqual(1, confirmations.publicationCount) } diff --git a/RMQClientTests/RMQAllocatedChannelTest.swift b/RMQClientTests/RMQAllocatedChannelTest.swift index 3198d97d..e0fc74af 100644 --- a/RMQClientTests/RMQAllocatedChannelTest.swift +++ b/RMQClientTests/RMQAllocatedChannelTest.swift @@ -189,7 +189,7 @@ class RMQAllocatedChannelTest: XCTestCase { contentBodies: [RMQContentBody(data: "hello".dataUsingEncoding(NSUTF8StringEncoding)!)] ) let expectedMessage = RMQMessage( - content: "hello", + body: "hello".dataUsingEncoding(NSUTF8StringEncoding), consumerTag: "", deliveryTag: 1, redelivered: true, @@ -224,8 +224,8 @@ class RMQAllocatedChannelTest: XCTestCase { let deliverHeader2 = RMQContentHeader(classID: deliverMethod2.classID(), bodySize: 123, properties: []) let deliverBody2 = RMQContentBody(data: "A message for consumer 2".dataUsingEncoding(NSUTF8StringEncoding)!) let deliverFrameset2 = RMQFrameset(channelNumber: 999, method: deliverMethod2, contentHeader: deliverHeader2, contentBodies: [deliverBody2]) - let expectedMessage1 = RMQMessage(content: "A message for consumer 1", consumerTag: "tag1", deliveryTag: 1, redelivered: false, exchangeName: "", routingKey: "", properties: []) - let expectedMessage2 = RMQMessage(content: "A message for consumer 2", consumerTag: "tag2", deliveryTag: 1, redelivered: false, exchangeName: "", routingKey: "", properties: []) + let expectedMessage1 = RMQMessage(body: "A message for consumer 1".dataUsingEncoding(NSUTF8StringEncoding), consumerTag: "tag1", deliveryTag: 1, redelivered: false, exchangeName: "", routingKey: "", properties: []) + let expectedMessage2 = RMQMessage(body: "A message for consumer 2".dataUsingEncoding(NSUTF8StringEncoding), consumerTag: "tag2", deliveryTag: 1, redelivered: false, exchangeName: "", routingKey: "", properties: []) ch.activateWithDelegate(nil) @@ -256,7 +256,7 @@ class RMQAllocatedChannelTest: XCTestCase { func testBasicPublishSendsAsyncFrameset() { let dispatcher = DispatcherSpy() let ch = ChannelHelper.makeChannel(999, contentBodySize: 4, dispatcher: dispatcher) - let message = "my great message yo" + let message = "my great message yo".dataUsingEncoding(NSUTF8StringEncoding)! let notPersistent = RMQBasicDeliveryMode(1) let customContentType = RMQBasicContentType("my/content-type") let priorityZero = RMQBasicPriority(0) @@ -264,7 +264,7 @@ class RMQAllocatedChannelTest: XCTestCase { let expectedMethod = MethodFixtures.basicPublish("my.q", exchange: "", options: [.Mandatory]) let expectedHeader = RMQContentHeader( classID: 60, - bodySize: message.dataUsingEncoding(NSUTF8StringEncoding)!.length, + bodySize: message.length, properties: [notPersistent, customContentType, priorityZero] ) let expectedBodies = [ @@ -295,7 +295,7 @@ class RMQAllocatedChannelTest: XCTestCase { let ch = ChannelHelper.makeChannel(999, contentBodySize: 4, dispatcher: dispatcher) let props: [RMQValue] = [RMQBasicCorrelationId("my-correlation-id")] - ch.basicPublish("", routingKey: "", exchange: "", properties: props, options: []) + ch.basicPublish(NSData(), routingKey: "", exchange: "", properties: props, options: []) let expectedProperties: Set = Set(RMQBasicProperties.defaultProperties()).union(props) let header = dispatcher.lastAsyncFrameset!.contentHeader @@ -306,9 +306,9 @@ class RMQAllocatedChannelTest: XCTestCase { func testPublishWhenContentLengthIsMultipleOfFrameMax() { let dispatcher = DispatcherSpy() let ch = ChannelHelper.makeChannel(999, contentBodySize: 4, dispatcher: dispatcher) - let messageContent = "12345678" + let messageContent = "12345678".dataUsingEncoding(NSUTF8StringEncoding)! let expectedMethod = MethodFixtures.basicPublish("my.q", exchange: "", options: []) - let expectedBodyData = messageContent.dataUsingEncoding(NSUTF8StringEncoding)! + let expectedBodyData = messageContent let expectedHeader = RMQContentHeader( classID: 60, bodySize: expectedBodyData.length, diff --git a/RMQClientTests/RMQExchangeTest.swift b/RMQClientTests/RMQExchangeTest.swift index df6bf435..10af36f2 100644 --- a/RMQClientTests/RMQExchangeTest.swift +++ b/RMQClientTests/RMQExchangeTest.swift @@ -53,12 +53,14 @@ import XCTest class RMQExchangeTest: XCTestCase { + let body = "foo".dataUsingEncoding(NSUTF8StringEncoding)! + func testPublishCallsPublishOnChannel() { let ch = ChannelSpy(1) let ex = RMQExchange(name: "", type: "direct", options: [], channel: ch) - ex.publish("foo", routingKey: "my.q") + ex.publish(body, routingKey: "my.q") - XCTAssertEqual("foo", ch.lastReceivedBasicPublishMessage) + XCTAssertEqual(body, ch.lastReceivedBasicPublishMessage) XCTAssertEqual("my.q", ch.lastReceivedBasicPublishRoutingKey) XCTAssertEqual("", ch.lastReceivedBasicPublishExchange) XCTAssertEqual([], ch.lastReceivedBasicPublishProperties!) @@ -68,7 +70,7 @@ class RMQExchangeTest: XCTestCase { func testPublishWithoutRoutingKeyUsesEmptyString() { let ch = ChannelSpy(1) let ex = RMQExchange(name: "", type: "direct", options: [], channel: ch) - ex.publish("foo") + ex.publish(body) XCTAssertEqual("", ch.lastReceivedBasicPublishRoutingKey) } @@ -76,9 +78,9 @@ class RMQExchangeTest: XCTestCase { func testPublishWithPersistence() { let ch = ChannelSpy(1) let ex = RMQExchange(name: "some-ex", type: "direct", options: [], channel: ch) - ex.publish("foo", routingKey: "my.q", persistent: true) + ex.publish(body, routingKey: "my.q", persistent: true) - XCTAssertEqual("foo", ch.lastReceivedBasicPublishMessage) + XCTAssertEqual(body, ch.lastReceivedBasicPublishMessage) XCTAssertEqual("my.q", ch.lastReceivedBasicPublishRoutingKey) XCTAssertEqual("some-ex", ch.lastReceivedBasicPublishExchange) XCTAssertEqual([RMQBasicDeliveryMode(2)], ch.lastReceivedBasicPublishProperties!) @@ -106,12 +108,12 @@ class RMQExchangeTest: XCTestCase { BasicPropertyFixtures.exhaustiveHeaders() ] - ex.publish("{\"a\": \"message\"}", + ex.publish("{\"a\": \"message\"}".dataUsingEncoding(NSUTF8StringEncoding), routingKey: "some.queue", properties: properties, options: [.Mandatory]) - XCTAssertEqual("{\"a\": \"message\"}", channel.lastReceivedBasicPublishMessage) + XCTAssertEqual("{\"a\": \"message\"}".dataUsingEncoding(NSUTF8StringEncoding), channel.lastReceivedBasicPublishMessage) XCTAssertEqual("some.queue", channel.lastReceivedBasicPublishRoutingKey) XCTAssertEqual("some-ex", channel.lastReceivedBasicPublishExchange) XCTAssertEqual([.Mandatory], channel.lastReceivedBasicPublishOptions) @@ -121,9 +123,9 @@ class RMQExchangeTest: XCTestCase { func testPublishWithOptions() { let ch = ChannelSpy(1) let ex = RMQExchange(name: "some-ex", type: "direct", options: [], channel: ch) - ex.publish("foo", routingKey: "my.q", persistent: false, options: [.Mandatory]) + ex.publish(body, routingKey: "my.q", persistent: false, options: [.Mandatory]) - XCTAssertEqual("foo", ch.lastReceivedBasicPublishMessage) + XCTAssertEqual(body, ch.lastReceivedBasicPublishMessage) XCTAssertEqual("my.q", ch.lastReceivedBasicPublishRoutingKey) XCTAssertEqual("some-ex", ch.lastReceivedBasicPublishExchange) XCTAssertEqual([], ch.lastReceivedBasicPublishProperties!) diff --git a/RMQClientTests/RMQMessageTest.swift b/RMQClientTests/RMQMessageTest.swift index f63cbcc6..810cdcb6 100644 --- a/RMQClientTests/RMQMessageTest.swift +++ b/RMQClientTests/RMQMessageTest.swift @@ -66,7 +66,7 @@ class RMQMessageTest: XCTestCase { props.append(RMQBasicReplyTo("my.sender")) props.append(RMQBasicTimestamp(date)) - let m = RMQMessage(content: "my message", + let m = RMQMessage(body: "my message".dataUsingEncoding(NSUTF8StringEncoding), consumerTag: "ctag", deliveryTag: 1, redelivered: false, diff --git a/RMQClientTests/RMQQueueTest.swift b/RMQClientTests/RMQQueueTest.swift index 41accb6a..11566f92 100644 --- a/RMQClientTests/RMQQueueTest.swift +++ b/RMQClientTests/RMQQueueTest.swift @@ -52,13 +52,14 @@ import XCTest class RMQQueueTest: XCTestCase { + let body = "a message".dataUsingEncoding(NSUTF8StringEncoding)! func testPublishSendsBasicPublishToChannel() { let channel = ChannelSpy(42) let queue = QueueHelper.makeQueue(channel, name: "some.queue") - queue.publish("a message") + queue.publish(body) - XCTAssertEqual("a message", channel.lastReceivedBasicPublishMessage) + XCTAssertEqual(body, channel.lastReceivedBasicPublishMessage) XCTAssertEqual("some.queue", channel.lastReceivedBasicPublishRoutingKey) XCTAssertEqual("", channel.lastReceivedBasicPublishExchange) XCTAssertEqual([], channel.lastReceivedBasicPublishProperties!) @@ -69,9 +70,9 @@ class RMQQueueTest: XCTestCase { let channel = ChannelSpy(42) let queue = QueueHelper.makeQueue(channel, name: "some.queue") - queue.publish("a message", persistent: true) + queue.publish(body, persistent: true) - XCTAssertEqual("a message", channel.lastReceivedBasicPublishMessage) + XCTAssertEqual(body, channel.lastReceivedBasicPublishMessage) XCTAssertEqual("some.queue", channel.lastReceivedBasicPublishRoutingKey) XCTAssertEqual("", channel.lastReceivedBasicPublishExchange) XCTAssertEqual([RMQBasicDeliveryMode(2)], channel.lastReceivedBasicPublishProperties!) @@ -99,11 +100,11 @@ class RMQQueueTest: XCTestCase { BasicPropertyFixtures.exhaustiveHeaders() ] - queue.publish("{\"a\": \"message\"}", + queue.publish("{\"a\": \"message\"}".dataUsingEncoding(NSUTF8StringEncoding), properties: properties, options: [.Mandatory]) - XCTAssertEqual("{\"a\": \"message\"}", channel.lastReceivedBasicPublishMessage) + XCTAssertEqual("{\"a\": \"message\"}".dataUsingEncoding(NSUTF8StringEncoding), channel.lastReceivedBasicPublishMessage) XCTAssertEqual("some.queue", channel.lastReceivedBasicPublishRoutingKey) XCTAssertEqual("", channel.lastReceivedBasicPublishExchange) XCTAssertEqual([.Mandatory], channel.lastReceivedBasicPublishOptions) @@ -114,9 +115,9 @@ class RMQQueueTest: XCTestCase { let channel = ChannelSpy(42) let queue = QueueHelper.makeQueue(channel, name: "some.queue") - queue.publish("a message", persistent: false, options: [.Mandatory]) + queue.publish(body, persistent: false, options: [.Mandatory]) - XCTAssertEqual("a message", channel.lastReceivedBasicPublishMessage) + XCTAssertEqual(body, channel.lastReceivedBasicPublishMessage) XCTAssertEqual("some.queue", channel.lastReceivedBasicPublishRoutingKey) XCTAssertEqual("", channel.lastReceivedBasicPublishExchange) XCTAssertEqual([], channel.lastReceivedBasicPublishProperties!) @@ -124,7 +125,7 @@ class RMQQueueTest: XCTestCase { } func testPopDelegatesToChannelBasicGet() { - let stubbedMessage = RMQMessage(content: "hi there", consumerTag: "", deliveryTag: 123, redelivered: false, exchangeName: "", routingKey: "", properties: []) + let stubbedMessage = RMQMessage(body: body, consumerTag: "", deliveryTag: 123, redelivered: false, exchangeName: "", routingKey: "", properties: []) let channel = ChannelSpy(42) let queue = QueueHelper.makeQueue(channel, name: "great.queue") @@ -149,7 +150,7 @@ class RMQQueueTest: XCTestCase { handlerCalled = true } - let message = RMQMessage(content: "I have default options!", consumerTag: "", deliveryTag: 123, redelivered: false, exchangeName: "", routingKey: "", properties: []) + let message = RMQMessage(body: "I have default options!".dataUsingEncoding(NSUTF8StringEncoding), consumerTag: "", deliveryTag: 123, redelivered: false, exchangeName: "", routingKey: "", properties: []) channel.lastReceivedBasicConsumeBlock!(message) XCTAssert(handlerCalled) @@ -165,7 +166,7 @@ class RMQQueueTest: XCTestCase { handlerCalled = true } - let message = RMQMessage(content: "I have custom options!", consumerTag: "", deliveryTag: 123, redelivered: false, exchangeName: "", routingKey: "", properties: []) + let message = RMQMessage(body: "I have custom options!".dataUsingEncoding(NSUTF8StringEncoding), consumerTag: "", deliveryTag: 123, redelivered: false, exchangeName: "", routingKey: "", properties: []) channel.lastReceivedBasicConsumeBlock!(message) XCTAssert(handlerCalled) diff --git a/RMQClientTests/RMQUnallocatedChannelTest.swift b/RMQClientTests/RMQUnallocatedChannelTest.swift index 21f98451..db3aad50 100644 --- a/RMQClientTests/RMQUnallocatedChannelTest.swift +++ b/RMQClientTests/RMQUnallocatedChannelTest.swift @@ -69,7 +69,7 @@ class RMQUnallocatedChannelTest: XCTestCase { { ch.afterConfirmed { _ in } }, { ch.basicConsume("foo", options: []) { _ in } }, { ch.basicGet("foo", options: []) { _ in } }, - { ch.basicPublish("hi", routingKey: "yo", exchange: "hmm", properties: [], options: []) }, + { ch.basicPublish("hi".dataUsingEncoding(NSUTF8StringEncoding)!, routingKey: "yo", exchange: "hmm", properties: [], options: []) }, { ch.basicQos(2, global: false) }, { ch.blockingWaitOn(RMQConnectionStart.self) }, { ch.confirmSelect() },