FIRMessagingPubSubTest.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <XCTest/XCTest.h>
  17. #import "FIRMessagingPubSub.h"
  18. @interface FIRMessagingPubSubTest : XCTestCase
  19. @end
  20. @implementation FIRMessagingPubSubTest
  21. static NSString *const kTopicName = @"topic-Name";
  22. #pragma mark - topicMatchForSender tests
  23. /// Tests that an empty topic name is an invalid topic.
  24. - (void)testTopicMatchForEmptyTopicPrefix {
  25. XCTAssertFalse([FIRMessagingPubSub isValidTopicWithPrefix:@""]);
  26. }
  27. /// Tests that a topic with an invalid prefix is not a valid topic name.
  28. - (void)testTopicMatchWithInvalidTopicPrefix {
  29. XCTAssertFalse([FIRMessagingPubSub isValidTopicWithPrefix:@"/topics+abcdef/"]);
  30. }
  31. /// Tests that a topic with a valid prefix but invalid name is not a valid topic name.
  32. - (void)testTopicMatchWithValidTopicPrefixButInvalidName {
  33. XCTAssertFalse([FIRMessagingPubSub isValidTopicWithPrefix:@"/topics/aaaaaa/topics/lala"]);
  34. }
  35. /// Tests that multiple backslashes in topics is an invalid topic name.
  36. - (void)testTopicMatchForInvalidTopicPrefix_multipleBackslash {
  37. XCTAssertFalse([FIRMessagingPubSub isValidTopicWithPrefix:@"/topics//abc"]);
  38. }
  39. /// Tests a topic name with a valid prefix and name.
  40. - (void)testTopicMatchForValidTopicSender {
  41. NSString *topic = [NSString stringWithFormat:@"/topics/%@", kTopicName];
  42. XCTAssertTrue([FIRMessagingPubSub isValidTopicWithPrefix:topic]);
  43. }
  44. /// Tests topic prefix for topics with no prefix.
  45. - (void)testTopicHasNoTopicPrefix {
  46. XCTAssertFalse([FIRMessagingPubSub hasTopicsPrefix:@""]);
  47. }
  48. /// Tests topic prefix for valid prefix.
  49. - (void)testTopicHasValidToicsPrefix {
  50. XCTAssertTrue([FIRMessagingPubSub hasTopicsPrefix:@"/topics/"]);
  51. }
  52. /// Tests topic prefix wih no prefix.
  53. - (void)testAddTopicPrefix_withNoPrefix {
  54. NSString *topic = [FIRMessagingPubSub addPrefixToTopic:@""];
  55. XCTAssertTrue([FIRMessagingPubSub hasTopicsPrefix:topic]);
  56. XCTAssertFalse([FIRMessagingPubSub isValidTopicWithPrefix:topic]);
  57. }
  58. /// Tests adding the "/topics/" prefix for topic name which already has a prefix.
  59. - (void)testAddTopicPrefix_withPrefix {
  60. NSString *topic = [NSString stringWithFormat:@"/topics/%@", kTopicName];
  61. topic = [FIRMessagingPubSub addPrefixToTopic:topic];
  62. XCTAssertTrue([FIRMessagingPubSub hasTopicsPrefix:topic]);
  63. XCTAssertTrue([FIRMessagingPubSub isValidTopicWithPrefix:topic]);
  64. }
  65. - (void)testRemoveTopicPrefix {
  66. NSString *topic = [NSString stringWithFormat:@"/topics/%@", kTopicName];
  67. topic = [FIRMessagingPubSub removePrefixFromTopic:topic];
  68. XCTAssertEqualObjects(topic, kTopicName);
  69. // if the topic doesn't have the prefix, should return topic itself.
  70. topic = [FIRMessagingPubSub removePrefixFromTopic:kTopicName];
  71. XCTAssertEqualObjects(topic, kTopicName);
  72. }
  73. @end