Skip to content

Commit

Permalink
Uppercasing the excluded_device field (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
jguz-pubnub authored May 18, 2023
1 parent 049f5c5 commit 68e4347
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
12 changes: 12 additions & 0 deletions PubNub.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@
35FE941822EFCB7F0051C455 /* SessionStreamTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35FE941722EFCB7F0051C455 /* SessionStreamTests.swift */; };
35FE941B22EFE5400051C455 /* EventStreamTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35FE941A22EFE5400051C455 /* EventStreamTests.swift */; };
35FE941F22F0929A0051C455 /* RequestRetrierTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35FE941E22F0929A0051C455 /* RequestRetrierTests.swift */; };
3D9134972A1216F7000A5124 /* PubNubPushTargetTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D9134962A1216F7000A5124 /* PubNubPushTargetTests.swift */; };
79407BD2271D4CFA0032076C /* PubNubContractTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79407BBF271D4CFA0032076C /* PubNubContractTestCase.swift */; };
79407BD3271D4CFA0032076C /* PubNubContractTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79407BBF271D4CFA0032076C /* PubNubContractTestCase.swift */; };
79407BD4271D4CFA0032076C /* PubNubContractCucumberTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 79407BC0271D4CFA0032076C /* PubNubContractCucumberTest.m */; };
Expand Down Expand Up @@ -896,6 +897,7 @@
35FE941722EFCB7F0051C455 /* SessionStreamTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SessionStreamTests.swift; sourceTree = "<group>"; };
35FE941A22EFE5400051C455 /* EventStreamTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventStreamTests.swift; sourceTree = "<group>"; };
35FE941E22F0929A0051C455 /* RequestRetrierTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RequestRetrierTests.swift; sourceTree = "<group>"; };
3D9134962A1216F7000A5124 /* PubNubPushTargetTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PubNubPushTargetTests.swift; sourceTree = "<group>"; };
793079152667C63700F23B72 /* CODEOWNERS */ = {isa = PBXFileReference; lastKnownFileType = text; path = CODEOWNERS; sourceTree = "<group>"; };
793079172667C63700F23B72 /* validate-yml.js */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.javascript; path = "validate-yml.js"; sourceTree = "<group>"; };
793079182667C63700F23B72 /* validate-pubnub-yml.yml */ = {isa = PBXFileReference; lastKnownFileType = text.yaml; path = "validate-pubnub-yml.yml"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1874,6 +1876,14 @@
path = EndpointError;
sourceTree = "<group>";
};
3D9134952A12161A000A5124 /* Push */ = {
isa = PBXGroup;
children = (
3D9134962A1216F7000A5124 /* PubNubPushTargetTests.swift */,
);
path = Push;
sourceTree = "<group>";
};
3DBD7CDD58292DFFDF108B95 /* Pods */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -2026,6 +2036,7 @@
35458BA1230CB32F0085B502 /* Subscription */,
3580A5A422F14D6000B12E5E /* Networking */,
3580A5A622F14DB700B12E5E /* Events */,
3D9134952A12161A000A5124 /* Push */,
357AEB7F22E6940000C18250 /* Extensions */,
357AEB8022E6940800C18250 /* Helpers */,
35EA73F822B199FF00D97BF0 /* Supporting Files */,
Expand Down Expand Up @@ -3038,6 +3049,7 @@
3559977B23073D53000BCFD1 /* WeakBoxTests.swift in Sources */,
35CDFEB622E76DC200F3B9F2 /* URLQueryItem+PubNubTests.swift in Sources */,
3557CDF9237F4ABB004BBACC /* PresenceRouterTests.swift in Sources */,
3D9134972A1216F7000A5124 /* PubNubPushTargetTests.swift in Sources */,
35D8D4CB22EB84B4001B07D9 /* AtomicTests.swift in Sources */,
OBJ_49 /* PubNubTests.swift in Sources */,
3558068A230F4C99005CDD92 /* InstanceIdOperatorTests.swift in Sources */,
Expand Down
7 changes: 7 additions & 0 deletions Sources/PubNub/Models/PubNubAPNSPayload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ public struct PubNubPushTarget: Codable, Hashable {
case excludedDevices = "excluded_devices"
case topic
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.environment, forKey: .environment)
try container.encodeIfPresent(self.excludedDevices?.map { $0.uppercased() }, forKey: .excludedDevices)
try container.encode(self.topic, forKey: .topic)
}

public init(
topic: String,
Expand Down
66 changes: 66 additions & 0 deletions Tests/PubNubTests/Push/PubNubPushTargetTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// PubNubPushTargetTests.swift
//
// PubNub Real-time Cloud-Hosted Push API and Push Notification Client Frameworks
// Copyright © 2023 PubNub Inc.
// https://www.pubnub.com/
// https://www.pubnub.com/terms
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

@testable import PubNub
import XCTest

class PubNubPushTargetTests: XCTestCase {
func test_ExcludedDeviceTokensAreUppercased() {
let excludedDevices = [
"fafb34563ce",
"7654eghjkl"
]
let pushMessage = PubNubPushMessage(
apns: PubNubAPNSPayload(
aps: APSPayload(
alert: .object(.init(title: "Message")),
badge: 2,
sound: .string("default")
),
pubnub: [.init(
targets: [.init(topic: "com.pubnub.swift", environment: .production, excludedDevices: excludedDevices)],
collapseID: "SwiftSDK"
)],
payload: "Push Message from PubNub Swift SDK"
)
)

let encodedData = try? Constant.jsonEncoder.encode(pushMessage)
let encodedStr = String(data: encodedData ?? Data(), encoding: .utf8) ?? ""

// Retrieves a value for the "excluded_devices" key:
let regex = try! NSRegularExpression(pattern: "(?<=\"excluded_devices\":)(.*?)](=?)")
let regexMatch = regex.matches(in: encodedStr, range: NSRange(location: 0, length: encodedStr.count))[0]
let startIdx = encodedStr.index(encodedStr.startIndex, offsetBy: regexMatch.range.location)
let endIdx = encodedStr.index(startIdx, offsetBy: regexMatch.range.length - 1)

XCTAssertEqual(
String(encodedStr[startIdx...endIdx]),
excludedDevices.map { $0.uppercased() }.jsonStringify ?? ""
)
}
}

0 comments on commit 68e4347

Please sign in to comment.