|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the APNSwift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2022 the APNSwift project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of APNSwift project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import APNSCore |
| 16 | +import XCTest |
| 17 | + |
| 18 | +final class APNSComplicationNotificationEncodingTests: XCTestCase { |
| 19 | + func testEncode() throws { |
| 20 | + struct Payload: Encodable { |
| 21 | + let foo = "bar" |
| 22 | + } |
| 23 | + let notification = APNSComplicationNotification( |
| 24 | + expiration: .immediately, |
| 25 | + priority: .immediately, |
| 26 | + topic: "com.example.app.complication", |
| 27 | + payload: Payload(), |
| 28 | + apnsID: nil |
| 29 | + ) |
| 30 | + |
| 31 | + let encoder = JSONEncoder() |
| 32 | + let data = try encoder.encode(notification) |
| 33 | + |
| 34 | + let expectedJSONString = """ |
| 35 | + {"foo":"bar"} |
| 36 | + """ |
| 37 | + let jsonObject1 = try JSONSerialization.jsonObject(with: data) as! NSDictionary |
| 38 | + let jsonObject2 = try JSONSerialization.jsonObject(with: expectedJSONString.data(using: .utf8)!) as! NSDictionary |
| 39 | + XCTAssertEqual(jsonObject1, jsonObject2) |
| 40 | + } |
| 41 | + |
| 42 | + func testEncode_whenEmptyPayload() throws { |
| 43 | + let notification = APNSComplicationNotification( |
| 44 | + expiration: .immediately, |
| 45 | + priority: .immediately, |
| 46 | + appID: "com.example.app" |
| 47 | + ) |
| 48 | + |
| 49 | + let encoder = JSONEncoder() |
| 50 | + let data = try encoder.encode(notification) |
| 51 | + |
| 52 | + let expectedJSONString = """ |
| 53 | + {} |
| 54 | + """ |
| 55 | + let jsonObject1 = try JSONSerialization.jsonObject(with: data) as! NSDictionary |
| 56 | + let jsonObject2 = try JSONSerialization.jsonObject(with: expectedJSONString.data(using: .utf8)!) as! NSDictionary |
| 57 | + XCTAssertEqual(jsonObject1, jsonObject2) |
| 58 | + } |
| 59 | +} |
0 commit comments